LinqToSQl and adapts from linq objects to objects in a centralized way
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 object is adapting to the Shared.DO.Act object. Within this object created by linqtosql Artist adapts to Shared.DO.Artist
Elsewhere in my codebase, I can query the executor (see below):
return from a in DBContext.Artists
select new Shared.DO.Artist {
ID = artist.ID,
Name = artist.Name
},
GigId = a.GigID
};
This means that the access code for the artist is now displayed in two places! Once upon receipt of the performer, as well as upon downloading the act
How do I centralize this customization code?
0
source to share
1 answer
I would do this using an artist class with an artist dictionary:
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];
}
}
0
source to share