views:

17

answers:

2

Help! My fingers are falling off from typing so much.

I have a lot of objects that have sane names to them. The Database names are not so sane, and i'm stuck defining my property names in all my projections.

For example:

from f in foo select new MyClass() {MyID = f.ID, MyName = f.f, MyTime = f.t}

Etc.. now, multiply this by hundreds or even thousands of business object methods that materialize data into various classes with mismatched field names and and dozens of properties in most fields andit's a lot of typing.

So, i'm wondering if there is any way (maybe via Attributes or something else) that allows you to define a default mapping for the class so that even if the fields mismatch I can simply say:

from f in foo select new MyClass()

Any solutions? Or am I stuck typing my fingers off?

EDIT:

Upon further reflection (pun semi-intended), I realize that this is precisely what L2S is for, and I can rename the fields in the L2S Data classes to whatever I need.

Sometimes the easiest answers are right in front of us.

+2  A: 

Well, one obvious option is to go to the DBML designer and change the names of the properties in the generated classes. They don't have to be the same as the ones in the database.

Just go into the designer, click on a property and change the Name part. (The Source property is the database column name.) Rebuild the project, and the names will have changed.

Alternatively, if you're always transforming from one source into the same type, create a method in a partial class for the source data type which transforms into the target one - or vice versa. So you could write a Foo.ToMyClass() method, or MyClass.FromFoo(Foo foo).

Jon Skeet
Yes, I came to that conclusion on my own. Funny how we sort of just accept the defaults the designer gives us in so many cases.
Mystere Man
+1  A: 

Just one other suggestion - AutoMapper would allow you to register mappings once and then just call a shared method to map from one object to another

Basiclife
There is so much great technology out there that gets lost in the shuffle. I have never heard of automapper, but it looks very cool. Thanks for the heads up.
Mystere Man
I gave you the answer because you need it more than Jon does, and your solution actually is a better solution to the question I asked, even though Jon's solution is a better solution to what I need ;)
Mystere Man
Thanks very much :)
Basiclife