将 Linq-to-Sql 实体映射到自定义域实体

发布于 2024-07-21 04:46:05 字数 542 浏览 3 评论 0原文

如何将 Linq-to-Sql 生成的实体 (DTO) 映射到我的域实体? 问题是我无法映射关联,因为它们不是同一类型。 DTO 使用 EntitySet 和 EntityRef,我的域实体使用 IList 和 T。

我看过一些博客文章:Ian Cooper 的 architecting-linq-to-sql-applications-part-5 和 digital_ruminations linq-to-sql-poco-support 但它们不符合我的需求。 我喜欢某种通用转换器类来处理映射。

现在我做了这样的事情:

public IList<Entities.Customer> GetAll()
    {
        try
        {
            return _custConverter.Convert(base.GetEntities());
        }

但是 Convert 方法只转换基本属性而不转换关联。 有什么想法我可以如何以最好的方式做到这一点?

How could I map my Linq-to-Sql generated entities (DTO’s) to my domain entities? The problem is that I can’t map the associations because they are not of the same type. The DTO’s uses EntitySet and EntityRef and my domain entities uses IList and T.

I’ve looked at some blog post: Ian Cooper's architecting-linq-to-sql-applications-part-5 and digital_ruminations linq-to-sql-poco-support but they don’t fit my needs. I like some kind of generic converter class to handle the mapping.

Now I do something like this:

public IList<Entities.Customer> GetAll()
    {
        try
        {
            return _custConverter.Convert(base.GetEntities());
        }

But the Convert method only converts the basic properties not the associations.
Any ideas how I can do this the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

反话 2024-07-28 04:46:05

您可能需要查看 AutoMapper。 它在自动映射属性方面做得很好,开箱即用,并支持广泛的自定义,例如自定义转换器,我认为它可以用于从实体集中创建列表。


更新:

You might want to look into AutoMapper. It does a great job of mapping properties automatically out of the box and supports extensive customization, such as custom converters, which I think could be used to make Lists out of your EntitySets.


Update:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文