views:

80

answers:

2

After too much thought, I've decided to use Linq To SQL as a DAL for my project, but keep the business objects as POCO objects.

This will give some flexibility because the database schema is old and have some problems that can not be solved, because backward compatibility.

I'm thinking about making some methods to retrieve or complete the LINQ objects into the POCO objects using Reflection. This methods must try to fill all the properties with the same names.

The question:
Do you know a better way to do the mapping?

Clarifications:
1. I don't want to use NHibernate, Entities, etc.
2. I know that reflection is slow

A: 

Create a static class "Copier". The static constructor for that class can reflect on T and U, and use DynamicMethod to emit code (loop through the properties on each, see which ones line up). That way the reflection price is paid once. From there, it'll be a normal delegate invoke, which should be plenty fast.

The problem you may run into is that simply copying fields of the same name becomes somewhat limiting when things don't match up perfectly. You might look into using reflection and generate code that does the conversions for you, then fixing them up by hand.

MichaelGG
Oh the angle brackets got erased. That should be Copier "of T, U".
MichaelGG
+2  A: 

You may want to look into something like Jimmy Bogard's AutoMapper, which does pretty much what you're talking about. He has it loaded up on CodePlex and GoogleCode, if you want to take a look at it.

Eric King