Are Repositories Domain Model Objects?

来源:互联网 发布:nginx lbs lua 编辑:程序博客网 时间:2024/06/04 17:51

 I focus on DDD for a long time,and I am working on an open source project with DDD now.Recently,I am thinking about the repository.I reffered to DDD Sample Project,I noticed that: repositories are placed in "domian.model" package. I deliberated repository again, I think we should not regard it as a part of domain model! The reasons as below:
    1.From pure domain view,there is no "repository" conception.It looks like a "programming-specific"  mechanism more .For example: in the "domain.model.cargo" package of DDDSample,The cargo,delivery or itinerary,they are all looks good,because they are "real role" in domain model,howerver,the CargoRepository looks weird,  they may make domain experts confused!
    2.I also admit that: Repository make sense for domain model.we indeed need a role which represents a collection of domain objects and provides CRUD service.however,no matter how to emphasis this role's "domain property",it still have to get involved in data acess logic!If we regard this role as a part of domain medel,the data access logic may "pollute" domain model!For example: in DDDSample,designers place repository interface in domain.model package,however,designers themself are also aware of their hibernate implement classes should not be placed in "domain.model",so,they placed them in infrastructure package.Why interface and its implement class are so different that make them so estranged?They shouldn't be such relationship!The source of problem is like what I mentioned:Even though it is only an inerface in domain model,repositories have to deal with data access logic,this is their inherent responsbility!Interface can not conceal it.
   So,I think it is not suitable to place repositories in domain model.We all know:For creating a clean domain model,domain model object should not depend on any service or repository.Actually,repositories are only invoked by service when they want to persist aggregation root.
So,I think we should regard repositories as a special kind of domain object,It only works on aggregate roots,and invoked by service when aggregate roots need persistence.And finally,I think it is better if we place repositories in such package:"domain.repository" that stand "domain.model" and "domain.service" side by side!
Be careful:The package name is "domain.repository" not "dataaccess.repository" or "persistence.repository"!Repository is a sepical domain object like service!This is my opinion!