I had a problem trying to execute my LINQ to SQL queries and map to my DRY domain objects without resorting to the cost of a few hits in db. In this example:
var query1 = from x in db.DBProducts
select new MyProduct
{
Id = x.ProductId,
Name = x.ProductName,
Details = new MyProductDetail
{
Id = x.DBProductDetail.ProductDetailId,
Description = x.DBProductDetail.ProductDetailDescription
}
}
The request will make ONE round trip in the database. Big! However, the problem that I see in this is that in the end I will also have a GetProductDetails method, which will also have to execute some of the SAME objects "object object → domain object", very similar to what indicated above.
To facilitate some of the comparisons, I thought it would be a cool idea to extend the partial data object classes to make a comparison for me:
public partial class DBProduct
{
MyProduct ToDomainObject()
{
return new MyProduct
{
Id = this.ProductId,
Name = this.ProductName,
Details = this.DBProductDetails.ToDomainObject()
};
}
}
public partial class DBProductDetail
{
MyProductDetail ToDomainObject()
{
return new MyProductDetail
{
Id = this.ProductDetailId,
Description = this.ProductDetailDescription
};
}
}
Nice! Now I can simply rewrite query1 as follows:
var query1 = from x in db.DBProducts
select x.ToDomainObject();
. , , , ToDomainObject() . , . Profiler db ONCE, . , , . , : LINQ to SQL, DRY ( )?