Note that there are some explanatory texts on larger screens.

plurals
  1. POFactories, services, repository in DDD
    primarykey
    data
    text
    <p>I have some questions regarding <strong>factories</strong>, <strong>repositories</strong> and <strong>services</strong> in <strong>DDD</strong>. I have the following entities: Folder, file, FileData.</p> <p>In my opinion the "Folder" is an <strong>aggregate root</strong> and should have the responsibility of creating the File and FileData object. </p> <p>So my first question is should I use a <strong>factory</strong> to create this aggreate or is it up to the <strong>repository</strong>? At this time I have 2 repositories, one for Folder and another for File, but it seems to me I should merge them together. The following code snippet, shows my <strong>Folder Repository</strong>, which is located in my infrastructure layer: </p> <pre><code>public class FolderRepository : IFolderRepository { #region Fields private readonly IFolderContext _context; private readonly IUnitOfWork _unitOfWork; #endregion #region Constructor public FolderRepository(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; _context = _unitOfWork.Context as IFolderContext; } #endregion public IUnitOfWork UnitOfWork { get { return _unitOfWork; } } public IQueryable&lt;Folder&gt; All { get { return _context.Folders; } } public Folder Find(Guid id) { return _context.Folders.Find(id); } public void InsertGraph(Folder entity) { _context.Folders.Add(entity); } public void InsertOrUpdate(Folder entity) { if (entity.Id == Guid.Empty) { _context.SetAdd(entity); } else { _context.SetModified(entity); } } public bool Delete(Guid id) { var folder = this.Find(id) ?? _context.Folders.Find(id); _context.Folders.Remove(folder); return folder == null; } public int AmountOfFilesIncluded(Folder folder) { throw new NotImplementedException(); //return folder.Files.Count(); } public void Dispose() { _context.Dispose(); } } </code></pre> <p>Next I have created a service in my <strong>application layer</strong>, this is called "IoService". I have my doubts about the location of the service. Should it be moved to the <strong>domain layer</strong>?</p> <pre><code>public class IoService : IIoService { #region Fields private readonly IFolderRepository _folderRepository; private readonly IFileRepository _fileRepository; private readonly IUserReferenceRepository _userReferenceRepository; #endregion #region Constructor public IoService(IFolderRepository folderRepository, IFileRepository fileRepository, IUserReferenceRepository userReferenceRepository) { if(folderRepository == null) throw new NullReferenceException("folderRepository"); if(fileRepository == null) throw new NullReferenceException("fileRepository"); if (userReferenceRepository == null) throw new NullReferenceException("userReferenceRepository"); _folderRepository = folderRepository; _fileRepository = fileRepository; _userReferenceRepository = userReferenceRepository; } #endregion #region Folder Methods /// &lt;summary&gt; /// Create a new 'Folder' /// &lt;/summary&gt; /// &lt;param name="userReference"&gt;&lt;/param&gt; /// &lt;param name="name"&gt;&lt;/param&gt; /// &lt;param name="parentFolder"&gt;&lt;/param&gt; /// &lt;param name="userIds"&gt;The given users represent who have access to the folder&lt;/param&gt; /// &lt;param name="keywords"&gt;&lt;/param&gt; /// &lt;param name="share"&gt;&lt;/param&gt; public void AddFolder(UserReference userReference, string name, Folder parentFolder = null, IList&lt;Guid&gt; userIds = null, IEnumerable&lt;string&gt; keywords = null, bool share = false) { var userReferenceList = new List&lt;UserReference&gt; { userReference }; if (userIds != null &amp;&amp; userIds.Any()) { userReferenceList.AddRange(userIds.Select(id =&gt; _userReferenceRepository.Find(id))); } var folder = new Folder { Name = name, ParentFolder = parentFolder, Shared = share, Deleted = false, CreatedBy = userReference, UserReferences = userReferenceList }; if (keywords != null) { folder.Keywords = keywords.Select(keyword =&gt; new Keyword { Folder = folder, Type = "web", Value = keyword, }).ToList(); } //insert into repository _folderRepository.InsertOrUpdate(folder); //save _folderRepository.UnitOfWork.Save(); } /// &lt;summary&gt; /// Get 'Folder' by it's id /// &lt;/summary&gt; /// &lt;param name="id"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public Folder GetFolder(Guid id) { return _folderRepository.Find(id); } #endregion #region File Methods /// &lt;summary&gt; /// Add a new 'File' /// &lt;/summary&gt; /// &lt;param name="userReference"&gt;&lt;/param&gt; /// &lt;param name="folder"&gt;&lt;/param&gt; /// &lt;param name="data"&gt;&lt;/param&gt; /// &lt;param name="name"&gt;&lt;/param&gt; /// &lt;param name="title"&gt;&lt;/param&gt; /// &lt;param name="keywords"&gt;&lt;/param&gt; /// &lt;param name="shared"&gt;&lt;/param&gt; public void AddFile(UserReference userReference, Folder folder, FileData data, string name, string title = "", IEnumerable&lt;string&gt; keywords = null, bool shared = false) { var file = new File { Name = name, Folder = folder, FileData = data, CreatedBy = userReference, Type = data.Type }; if (keywords != null) { file.Keywords = keywords.Select(keyword =&gt; new Keyword { File = file, Type = "web", Value = keyword, }).ToList(); } folder.Files.Add(file); folder.Updated = DateTime.UtcNow; _folderRepository.InsertOrUpdate(folder); //save _folderRepository.UnitOfWork.Save(); } /// &lt;summary&gt; /// Get 'File' by it's id /// &lt;/summary&gt; /// &lt;param name="id"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public File GetFile(Guid id) { return _fileRepository.Find(id); } #endregion } </code></pre> <p>To summarize: Should I use the service for creating the folder object. Or should the service just use a <strong>factory</strong>, which have the responsibility of creating the object and send the created object to the <strong>repository</strong>? What about <strong>dependency injection</strong> in the service, should I inject my services from the UI layer with IOC containers like <strong>Unity</strong> or should I just hardcode the dependencies in the service?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload