Domanda

I am architechting my application on the lines of Repository pattern, Aggregate root and Unit of work. I am using Entity Framework5 as ORM.

I am in a situation where I don't know how to proceed with adding new entities that are aggregate roots and have a foreign key relation to entities that are not aggregate roots.

I am not sure if I made sense with my previous statement, but let me put it in a example here.

Person Table in Db (This is an aggregate root in my application)

  1. Name
  2. Address Line 1
  3. CountryID

Countries Table (This is not an aggregate root as I might never need to query them independently)

  1. CountryID
  2. Country Name

In my application I have PersonsRepository which implements a generic Repository where T is a Aggregate root.

Now when I create a new person in my code I need to add Country to the Person Object's Navigation Property. If I create a new Country Object and assign it to Person's Country property and try to save Person object the EF throws error. I can't query the Countries table because it is not my Aggregate root.

Well, this is not my actual scenario but this is what i am trying to overcome. How should I proceed from here?

One thought that comes to my mind is to create a generic read-only repository that will be used for querying the DB and not modifying it, Is it a good way to proceed or is there something wrong I am doing.

Thanks in advance for your reply and for reading long post.

È stato utile?

Soluzione

Aggregate roots are not just things that you may not need to query directly. By your reasoning you would have to create the same country multiple times as you can't query to see if the country exits (i.e. the country is a weak table which requires Person to exist).

Simply put agreggate roots are objects which can be identified without anything else. A country may exist without a person while an order line can't be identified without an order. You might want to read this article: http://dddcommunity.org/library/vernon_2011

As for entity framework, check my implementation of repository/uow: http://blog.gauffin.org/2013/01/repository-pattern-done-right/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top