Struggling to handle aggregates and aggregate roots when it comes to persisting. For example, I currently have a repository for each aggregate/table, but this is causing some issues in my service class.
Current JournalService
public class JournalService
{
public JournalService(IUnitOfWork uow, IJournalRepository journalRepo)
{
this.uow = uow;
this.journalRepo = journalRepo;
}
public void AddEntry(string journalName, Entry entry)
{
journalEntryRepo.AddEntry(journalName, entry);
// there is no need for me to even have a journal class or any other domain class since repositories handle it all
uow.Save();
}
private readonly IUnitOfWork uow;
private readonly IJournalRepository journalRepo;
}
In the above example (what I'm currently using), it seems that there is no point to even have domain classes such as Journal. I could just handle everything by accessing the repositories. This feels very weird to me.. I almost feel that there is no domain.
Upon further research it seems that a repository should handle an aggregate root; and return the entire root that you then operate on. The issue I'm having with this proposed solution is, do I just call update on the aggregate root even though I've added objects (e.g. to a list)? Please see below:
Proposed New JournalService
public class JournalService
{
public JournalService(IUnitOfWork uow, IJournalRepository journalRepo)
{
this.uow = uow;
this.journalRepo = journalRepo;
}
public void AddEntry(string journalName, Entry entry)
{
Journal journal = journalRepo.FindById(journalName);
journal.AddEntry(entry);
journalRepo.Update(journal); ///// am I handling this correctly here?
uow.Save();
}
private readonly IUnitOfWork uow;
private readonly IJournalRepository journalRepo;
}
// simple CRUD for repo
public interface IJournalRepository
{
Journal Create();
Journal FindById(string name);
void Update(Journal journal);
void Remove(Journal journal);
}
With the new solution, am I handling this correctly? Then I assume the IJournalRepository needs to handle figuring out what entries were added and then add them to the correct table? Although, this seems like a lot of extra work...