Should A repository call another repository? Or should a repository call a service layer?

后端 未结 2 1624
小鲜肉
小鲜肉 2020-12-16 19:54

I am trying to figure out how to tackle this problem. I have to insert some data into 2 tables lets call them Table A and Table B.

Table A has these columns
         


        
相关标签:
2条回答
  • 2020-12-16 20:22

    No - i cannot think of a reason for a repository to call another repository, nor another service. Their only concern should be persisting your entities and retrieving entities from a datastore. They should be ignorant to most aspects of your application except for the underlying domain.

    It sounds like you are assuming their should be a repository per table, which is incorrect. There should be a repository per aggregate root, and that repository should take care of storing data to all of the underlying, related tables. It is OK for the repository to have some logic pertaining to where or how to save the data, but it would be best for that to be encapsulated in a common area when possible.

    For example if you were to have person objects and needed to save to different tables according to gender, you could do this using inheritance ( if (person is Woman) save here... ) or properties on the object ( if (person.Gender == Gender.Female) save here... ) or Specifications ( if (FemaleSpecification.IsSatisfiedBy(person)) save here... ).

    0 讨论(0)
  • 2020-12-16 20:32

    The guard clause (param1 != "hi") should be in a higher layer, such as an application service layer.

    The service layer should coordinate the two repositories.

    0 讨论(0)
提交回复
热议问题