3

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.

2
  • And what error EF throws? Feb 10, 2013 at 20:24
  • It throws "Unable to cast Person object to type Country" and I think that is perfectly valid, why should I be creating a new Country Object when it is a foreign key in db. It works perfectly fine If I fetch Country using the datacontext and assign it to the Country Property of Person object when creating it. Feb 10, 2013 at 23:45

1 Answer 1

4

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/

2
  • Thanks for the reply jgauffin, that clears my understanding about aggregates. Feb 12, 2013 at 14:08
  • 1
    The repository article: 'A more realistic approach is that you in UserRepository.GetUsersGroupOnSomeComplexQuery() uses ADO.NET directly while you in UserRepository.Create() uses Entity Framework.' Why didn't I ever consider that, cheers! May 5, 2017 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.