LinqToSQl 以及以集中方式从 linq 适应 Domain 对象

发布于 2024-07-10 14:28:58 字数 979 浏览 2 评论 0原文

想象一下下面的代码:

return from a in DBContext.Acts
       join artist in DBContext.Artists
       on a.ArtistID equals artist.ID into art
       from artist in art.DefaultIfEmpty()
       select new Shared.DO.Act {
                 ID = a.ID,
                 Name = a.Name,
                 Artist = new Shared.DO.Artist   {
                             ID = artist.ID,
                             Name = artist.Name
                          },
                 GigId = a.GigID
              };

如您所见,linqtosql 生成的 act 对象被改编为 Shared.DO.Act 对象。 作为此过程的一部分,linqtosql 生成的 Artist 对象被改编为 Shared.DO.Artist

在我的代码库的其他地方,我可能想要请求一个 Artist(见下文):

return from a in DBContext.Artists
       select new Shared.DO.Artist {
                 ID = artist.ID, 
                 Name = artist.Name
              },
              GigId = a.GigID
       };

这意味着 Artist 的适配代码现在出现在两个地方! 一旦获得艺术家以及加载表演时,

我应该如何集中此改编代码?

Imagine the following code:

return from a in DBContext.Acts
       join artist in DBContext.Artists
       on a.ArtistID equals artist.ID into art
       from artist in art.DefaultIfEmpty()
       select new Shared.DO.Act {
                 ID = a.ID,
                 Name = a.Name,
                 Artist = new Shared.DO.Artist   {
                             ID = artist.ID,
                             Name = artist.Name
                          },
                 GigId = a.GigID
              };

As you can see, the linqtosql generated act object is adapted into the Shared.DO.Act object. As part of this a linqtosql generated Artist object is adapted into a Shared.DO.Artist

Elsewhere in my code base, I may want to request an Artist (see below):

return from a in DBContext.Artists
       select new Shared.DO.Artist {
                 ID = artist.ID, 
                 Name = artist.Name
              },
              GigId = a.GigID
       };

This means the adption code for an Artist now appears in two places! Once when getting an artist and also when an act is loaded

How should I centralise this adaption code?

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

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

发布评论

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

评论(1

鹿港小镇 2024-07-17 14:28:58

我将使用带有艺术家字典的 Artists 类来完成此操作:

public class Artists
{
    // key should be whatever type artist.ID is
    private Dictionary<int, Shared.DO.Artist> _artistDictionary;

    public static Get(int id)
    {
        if (_artistDictionary == null) 
            _artistDictionary = new Dictionary<int, Shared.DO.Artist>();

        if (!_artistDictionary.ContainsKey(id))
        {
            var artist = from a in DBContext.Artists
                         on a.ID equals id
                         select new Shared.DO.Artist {
                             ID = a.ID, 
                             Name = a.Name
                         };

            _artistDictionary.Add(id, artist);
        }

        return _artistDictionary[id];
    }
}

I would do this using an Artists class with a dictionary of artists:

public class Artists
{
    // key should be whatever type artist.ID is
    private Dictionary<int, Shared.DO.Artist> _artistDictionary;

    public static Get(int id)
    {
        if (_artistDictionary == null) 
            _artistDictionary = new Dictionary<int, Shared.DO.Artist>();

        if (!_artistDictionary.ContainsKey(id))
        {
            var artist = from a in DBContext.Artists
                         on a.ID equals id
                         select new Shared.DO.Artist {
                             ID = a.ID, 
                             Name = a.Name
                         };

            _artistDictionary.Add(id, artist);
        }

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