Search code examples
domain-driven-designddd-repositories

could model contains repository reference?


Could my domain model reference my repository interfaces? or better yet, containing my repositories definitions?

I ask because I was thinking about what I read on the limbo of internet that says that a model shouldn't know about its persistence, but I think if the repository exists due the model, I mean, to handle specific needs of its model, why not the model project define it?

Is it a problem/ bad design if I do the following?

public interface ISomethingThatNeedToBeAprovedRepository
{
   void Save(SomethingThatNeedToBeAproved somethingThatNeedToBeAproved);
}

public class SomethingThatNeedToBeAproved
{
    public int Status {get;set;}
    public string Reason {get;set;}

    public ISomethingThatNeedToBeAprovedRepository Repository{ get; set;}

    public void Aprove(status, reason)
    {
        DoSomeAsserts();
        Status = status;
        Reason = reason;

        Repository.save(this);
    }

}

Solution

  • Yes, it can!

    At given point you need to persist your aggregate so a repository is needed. I use to take IRepository inside my domain model, while infratructure details of Repository in another project.