Are there any shortcuts in L2S Projection with lots of mismatched fields?
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 th开发者_如何转开发e fields in the L2S Data classes to whatever I need.
Sometimes the easiest answers are right in front of us.
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)
.
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
精彩评论