views:

326

answers:

3

is there any tool or utility(mapper assembly) to construct business objects from entities(which are obtained from DB using linq -> sql , entity framework or whatever..)

in the absence of one , can anyone suggest the best way that can be accomplished rather can copy pasting the properties(what i'm doing right now) from the entity classes.?

thanks.

vijay

+3  A: 

You map to business objects by projecting. This works even if your POCO business objects have a different shape than your entities.

var q = from dataObject in Context.DataObjects
        select new BusinessObject
        {
            Name = dataObject.Name,
            RelatedObjectName = dataObject.RelatedObject.Name, // works even if RelatedObject is null
            DirectChildren = from c in dataObject.Children
                             select new ChildBusinessObject
                             {
                                 Name = c.Name
                                 // etc.
                             }
            GrandChildren = from c in dataObject.Children
                            from gc in c.Children
                            select new ChildBusinessObject
                            {
                                Name = c.Name
                                // etc.
                            },
            // etc.
        };
Craig Stuntz
that was great craig . can u suggest any tools.? to automate the same. its really tedious manually doing this for each entity.
vijaysylvester
I find LINQpad incredibly valuable for this, and it supports LINQ to Entities. It doesn't do it automatically, though. VS IntelliSense is generally enough for me, though; you end up typing very little of the actual query. I wouldn't want to do it entirely automatically because performance can be substantially better if you only select the related objects that you actually *need* for your business methods, rather than every property of the entity.
Craig Stuntz
+3  A: 

Automapper

TGnat
Automapper FTW for this.
mxmissile
AutoMapper won't load related entities in a single query. LINQ will.
Craig Stuntz
A: 

This is not directly answering your questions - but for less complicated projects I use this product http://www.devexpress.com/Products/NET/ORM/ (.NET Object-Relational Mapper Tool from DevExpress)

lstanczyk
What u have said is ORM tool . to get the data objects. i now need the business objects , to be constructed from data objects.
vijaysylvester