Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository pattern aggregate and aggregate root with Entity Framework 4.0

I have a question on implementing the repository pattern with my data model. I have searched online and looked into a lot of posts but I do not find any answer which clears my doubts. Basically our domain model goes like below we have a client object which has a lot of child objects and some child objects in turn will have a child objects and at any point of time these child objects without their parent objects are not needed and does not make any sense in the application.

client
 --> client zip codes
 --> client phone history
 --> client medical history
 --> client direct services
      --> client direct service assessments
      --> client direct service risk reductions
 --> client housing
      --> client housing landlord

and it goes like this. So from the above representation I have some aggregates and the root is the client object so I was thinking of creating a repository at the aggregate root level which is the client. My question is how do I handle the other aggregates. Can anyone please provide me some ideas on this.

Thanks, Ajay.

like image 988
ajay Avatar asked Apr 20 '11 21:04

ajay


People also ask

Is repository pattern needed with entity Framework?

No, the repository/unit-of-work pattern (shortened to Rep/UoW) isn't useful with EF Core. EF Core already implements a Rep/UoW pattern, so layering another Rep/UoW pattern on top of EF Core isn't helpful.

What is an aggregate root in the context of repository pattern?

In the Context of a Repository the Aggregate Root is an Entity with no parent Entity. It contains zero, One or Many Child Entities whose existence is dependent upon the Parent for it's identity. That's a One To Many relationship in a Repository. Those Child Entities are plain Aggregates.

What is aggregate root entity?

An aggregate is a collection of one or more related entities (and possibly value objects). Each aggregate has a single root entity, referred to as the aggregate root. The aggregate root is responsible for controlling access to all of the members of its aggregate.

What is entity Framework repository pattern?

The Repository Pattern allows us to create an abstraction layer between the data access layer and the business logic layer of an application. So, this Data Access Pattern offers a more loosely coupled approach to data access.


1 Answers

In general, it is the repository's responsibility to prepare the aggregate root for use by the rest of the program. So you should query the repository for a client, and it should return a full client object complete with zip codes, phone history, medical history, etc. The idea of an aggregate root is that no outside code should have to worry about getting clients without those other aggregates available.

Looking at it another way, since you are creating a client repository and client is the aggregate root, it is the client repository's job to compose all subobjects, even if they are themselves aggregates.

like image 138
Domenic Avatar answered Sep 27 '22 02:09

Domenic