New Posts New Posts RSS Feed: Shared sub-models
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Shared sub-models

 Post Reply Post Reply
chuckc View Drop Down

Joined: 27-Feb-2010
Posts: 54
Post Options Post Options   Quote chuckc Quote  Post ReplyReply Direct Link To This Post Topic: Shared sub-models
    Posted: 29-Jan-2012 at 3:22pm

Given a simple example scenario where we have two databases that share some common tables, such as:

Publishing DB: Author, Book, User, Permission, PermissionGroup

Law DB: Attorney, CourtCase, Plaintiff, User, Permission, PermissionGroup

We want an assembly that manages user permissions which only need access to the User, Permission and PermissionGroup tables.  We would like this User focused assembly to be common and shared in two separate Law and Publishing projects.  This seems to indicate three separate models:

1. User model containing User, Permission, PermissionGroup

2. Publishing model containing just Author and Book (without User, etc.).

3. Law model containing just Attorney and CourtCase (without User, etc.)

I understand that multiple models used in a single EntityManager cannot contain duplicate entities, so the Law and Publishing models could not contain User entities directly.

This implies a need to manually code navigation properties in each of the Publishing and Law models to support navigation operations like Attorney.User.Permissions.

So, given the above, what are the best approach(s) to take?  Does Code First (or Code Second) offer significant advantages?  Could I perhaps leverage PostSharp to inject User properties on any entity class that has a UserID property as opposed to manual coding?

Or would it be more practical to just include User entities in the Publishing and Law models directly and use a separate EntityManager when working with User entities in the shared User assembly?  That implies updates to three models when something changes with User, but maybe that is still the cleanest approach.

Thanks for any and all thoughts.

Back to Top
sbelini View Drop Down

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 01-Feb-2012 at 9:05am
Hi Chuck,
You will indeed need to have two EntityManagers (one for Law and one for Publishing) if you want to share the User model.
Note that when coding the navigation properties, that could be done only in one direction or you will have a circular reference error.
Code First wouldn't have any advantage and the only difference is that you'd be coding the navigation properties in the entity in Code First and in the partial class otherwise.
Also, injecting the properties using PostSharp won’t be that useful because you would want to reference the properties in code at compile time. (we use PostSharp to inject behaviors that are discovered at runtime)
I'd suggest to implement a repository pattern. The repository would include methods to fetch entities across models.
public User Repository.GetUser(Attorney attorney) { … }
You can find more information about the repository pattern at
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down