LinqToSQl 以及以集中方式从 linq 适应 Domain 对象
想象一下下面的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用带有艺术家字典的 Artists 类来完成此操作:
I would do this using an Artists class with a dictionary of artists: