Linq to Sql DB 对象到域对象的映射和性能
我在尝试使 LINQ to SQL 查询和到域对象的映射 DRY 且不会产生多次往返数据库的成本时遇到问题。给出以下示例:
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
}
}
查询将与数据库进行一次往返。伟大的!然而,我看到的问题是,最终我还将有一个“GetProductDetails”方法,该方法也需要执行一些相同的“数据对象 -> 域对象”映射,与上面的非常相似。
为了减轻一些映射,我认为扩展部分数据对象类来为我进行映射可能是一个很酷的主意,如下所示:
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
};
}
}
很好!现在,我可以简单地重写 query1,如下所示:
var query1 = from x in db.DBProducts
select x.ToDomainObject();
这使得代码更加干燥且更具可读性。此外,需要执行相同类型映射的其他查询可以简单地使用 ToDomainObject() 方法进行映射。它有效,但需要付出代价。通过 Profiler 观察时,第一个查询将调用数据库一次,在必要时连接表。第二个查询未正确连接,因此对数据库进行了多次调用。有没有办法完成我想要做的事情:重构 LINQ to SQL 查询,以便到域对象的映射是 DRY (无代码重复)?
I'm having a problem trying to make my LINQ to SQL queries and the mapping to my domain objects DRY without incurring the cost of multiple round trips to the db. Given 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 query will make ONE round trip to the DB. Great! However, the problem I see with this is that eventually, I'll also have a 'GetProductDetails' method which will also need to do some of the SAME "data object -> domain object" mapping, very similar to that above.
To alleviate some of the mapping, I thought it might be a cool idea to extend the partial data object classes to do the mapping for me, like so:
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 could simply rewrite query1 as follows:
var query1 = from x in db.DBProducts
select x.ToDomainObject();
This makes the code more DRY and more readable. Additionally, other queries that need to do the same type of mapping can simply use the ToDomainObject() method for the mapping. It works, but with a cost. While watching via Profiler, the first query would call the db ONCE, joining tables where necessary. The second query doesn't join appropriately, thus making multiple calls to the DB. Is there a way to accomplish what I'm trying to do: refactor LINQ to SQL queries so that the mapping to domain objects is DRY (no code duplication)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用AutoMapper。一旦您尝试过,您就不太可能看到如下代码:
Use AutoMapper. Once you've tried it, it's unlikely you will ever see code like this: