Linq to Sql DB 对象到域对象的映射和性能

发布于 2024-08-24 07:29:49 字数 1516 浏览 7 评论 0原文

我在尝试使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2024-08-31 07:29:50

使用AutoMapper。一旦您尝试过,您就不太可能看到如下代码:

new MyProduct
{
    Id = x.ProductId,
    Name = x.ProductName,
    Details = new MyProductDetail
    {
        Id = x.DBProductDetail.ProductDetailId,
        Description = x.DBProductDetail.ProductDetailDescription
    }
}

Use AutoMapper. Once you've tried it, it's unlikely you will ever see code like this:

new MyProduct
{
    Id = x.ProductId,
    Name = x.ProductName,
    Details = new MyProductDetail
    {
        Id = x.DBProductDetail.ProductDetailId,
        Description = x.DBProductDetail.ProductDetailDescription
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文