ASP.NET MVC 4 – Integrating Entity Framework 5 and SimpleMembership into Repository and Unit Of Work Pattern

asp.net-mvc-4entity-framework

I have a ASP.NET MVC4 project in which I would like to use the SimpleMembership. The application has a Person table that holds relevant information about users in the system.

I would like a link between the currently logged in user and the Person information. So I have made a foreign key in the Person table to the UserId column of UserProfile table (from SimpleMembership).

I use Entity Framework 5 as ORM and have implemented a generic repository to handle CRUD methods and so on.

Now I am at a point where I want to implement user profile creation. The creation consists of 2 parts parts:

  • login: username and password (to be stored in SimpleMembership tables)
  • personal information (to be stored in Person table)

In order to create a user I guess I need to:

  1. Save user information through SimpleMembership
  2. Save personal information together with userId from step 1 through my PersonRepository

I would like some kind of UserRepository that could save SimpleMembership data but I do not know if it's the right approach?
Is there a better approach?

Best Answer

I always keep repositories so there a is a one to one relationship between a repository and a table in a database. And repositories do not know about other repositories. If you need to handle transactions between repositories I would implement a Unit of Work design pattern and on top of that a Service design pattern that handles the domain logic around the transactions.

Another option is to modify the UserProfile in SimpleMembership to contain the extra information that you have in the Person table. UserProfile was designed to be customized and you can read about customizing it here.