想象一下以下代码:
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
在我的代码库中,我可能想要一位艺术家(见下文):
return from a in DBContext.Artists
select new Shared.DO.Artist {
ID = artist.ID,
Name = artist.Name
},
GigId = a.GigID
};
这意味着艺术家的广告代码现在出现在两个地方!一旦获得艺术家以及何时加载行为
我应该如何集中这个适应代码?
答案 0 :(得分:0)
我会使用带有艺术家词典的艺术家课程来做到这一点:
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];
}
}