Understanding Domain Driven Design
I have been trying to understand DDD for few weeks now. It开发者_如何学编程s very confusing. I don't understand how I organize my projects. I have lot of questions on UnitOfWork, Repository, Associations and the list goes on...
Lets take a simple example.
Album and Tracks
Album: AlbumId, Name, ListOf Tracks
Tracks: TrackId, Name
Should i expose Tracks as a IList/IEnumerabe property on Album ? If that how do i add an album ? OR should i expose a ReadOnlyCollection of Tracks and expose a AddTrack method?
How do I load Tracks for Album [assuming lazy loading]? should the getter check for null and then use a repository to load the tracks if need be?
How do we organize the assemblies. Like what does each assembly have? Model.dll - does it only have the domain entities? Where do the repositories go? Interfaces and implementations both. Can i define IAlbumRepository in Model.dll? Infrastructure.dll : what should this have?
Where is unit of work defined? How do repository and unit of work communicate? Or should they? For example. If I need to add multiple tracks to album, again should this be defined as AddTrack on Album OR should there a method in the repository? Regardless of where the method is, how do I implement unit of work here?
Should the UI use Infrastructure..dll or should there be ServiceLayer?
Do my questions make sense?
Regards
Question 1, I suggest a structure like this:
public class Album
{
public int AlbumId { get; set; }
/// <summary>
/// Readonly, foreach, public
/// </summary>
public IEnumerable<Track> Tracks
{
get { return TrackList; }
}
/// <summary>
/// Protected for repository/ORM
/// </summary>
protected IList<Track> TrackList { get; set; }
public void AddTrack(Track track)
{
//Here you can put additional logic
TrackList.Add(track);
}
public void RemoveTrack(Track track)
{
//Here you can put additional logic
TrackList.Remove(track);
}
}
public class Track
{
}
Write a public IEnumerable property for tracks to allow readonly access and for cycles.
The protected property holds tracks and can be used by the ORM.
Write methods to Add and Remove tracks to album. In these methods you can put additional logic.
精彩评论