Question about Repositories and their Save methods for domain objects

后端 未结 2 1445
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 23:17

I have a somewhat ridiculous question regarding DDD, Repository Patterns and ORM. In this example, I have 3 classes: Address, Company and

2条回答
  •  粉色の甜心
    2021-01-31 23:51

    From my recent experience of using the repository pattern I think you would benefit from using a generic repository, the now common IRepository of T. That way you wouldn't have to add repository methods like SavePerson(Person person). Instead you would have something like:

    IRepository personRepository = new Repository();
    Person realPerson = new Person();
    personRepository.SaveOrUpdate(realPerson);
    

    This method also lends itself well to Test Driven Development and Mocking.

    I feel the questions about behavior in your description would be concerns for the Domain, maybe you should have an AddCompany method in your Person class and change the Company property to

    public Company Company { get; private set; }
    

    My point is; model the domain without worrying about the how data will be persisted to the database. This is a concern for the service that will be using your domain model.

    Back to the Repository, have a look at this post for good explanation of IRepository over LinqToSql. Mike's blog has many other posts on Repositories. When you do come to choose an ORM I can recommend HHibernate over LinqToSql, the latter is now defunct and NHibernate has a great support community.

提交回复
热议问题