Print Page | Close Window

Shared sub-models

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3240
Printed Date: 24-Apr-2024 at 2:23am


Topic: Shared sub-models
Posted By: chuckc
Subject: Shared sub-models
Date 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.





Replies:
Posted By: sbelini
Date 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.
i.e. 
public User Repository.GetUser(Attorney attorney) { … }
 
You can find more information about the repository pattern at http://msdn.microsoft.com/en-us/library/ff649690.aspx - http://msdn.microsoft.com/en-us/library/ff649690.aspx .
 
Regards,
   Silvio.



Print Page | Close Window