Repository Pattern, POCO, and Business Entities

First an observation about Asp.net MVC project template

I must say that there is a tiny misunderstanding with Visual Studio's Asp.net MVC project template. And that is the Model folder. People not knowing MVC pattern would automatically relate this to data model and not MVC application/presentation model. This is fine for simple applications where we don't distinguish between the two but not for anything else.

Let's continue to my answer

When I write business level applications I separate my solution into 4 projects (at least):

  • presentation tier - Asp.net MVC application but I remove Model folder and have all my views as strong type views to avoid magic strings as much as possible
  • service tier - business logic processes
  • data tier - data model ie. EF4 and repositories that access this model
  • objects tier - this project actually has POCOs that are used for inter-layer communication and any interfaces used by various layers (think of IoC)

My request process usually looks very clean and works this way:

  1. When a request is made my Asp.net MVC controller action validates data (POCO objects), does whatever is necessary for the presentation tier before calling into services.
  2. Service is called that does whatever business process logic requires and normally calls repository to do something with data.
  3. Repository manipulates data in the data model and then creates POCOs from results that will be returned to service layer.
  4. Service layer receives POCOs does additional logic if needed and returns them back to presentation.
  5. Presentation (controller) decides which view to display and provides model for that particular view and returns it. Of course instead of a view it can be any other result as well.

Advantage of using separate MVC model classes in Objects project (you couldn't put them in the Model folder because of circular project reference) is that I can have presentation optimised classes. Or better said: I have business process centric interface instead of data centric.

Let's explain this with an example: Take for instance user registration view. It can't be strong typed to data model's User entity. Why? Because it has two inputs for password. So I can have an application/presentation model class called UserRegistration even though there's nothing similar in data model. Its validation works completely differently compared to data model's User entity. If I'd have my user registration done without strong type I'd have to make my controller action with all parameters of every single field. These wouldn't be automatically validated which would mean that I can have a larger bug surface. Someone might hurry into writing code but forget about certain aspects of validation.

Strong type views returning strong types back at server are the safest way of getting rid of all kinds of obscure bugs that are usually discovered by users especially if you don't do any methodical testing on your project (which is somewhere between 75-90% chance).


I was in a very similar place as the OP sometime ago, so I'll expand on Roberts answer with some code of how I structured my asp.net mvc app after learning about the repository pattern.

So your project is QandA

You'll have a class library project called QandA.data, here is where you will create your edmx file and all your entity framework classes. Then you have a repository for each entity like this:

public interface IRepository<T>
{
    T Save(T entity);
    void Delete(T entity);
    IQueryable<T> GetAll();
    T GetById(int id);
}

You could then have either a factory or use Dependency Injection to get the actually repositories. So:

class QuestionRepo : IRepository<Question>
{
 //call xxxEntites and get/save/delete yourentities here.
}
static class RepositoryFactory
{
 public static IRepository<Question> GetQuestionRepo()
 {
  return new QuestionRepo();
 }
}

Then down in your calling code (in your asp.net project) you have

IRepository<Question> qRepo = RepositoryFactory.GetQuestionRepo();
Question q =  qRepo.GetById(1);

Now the advantage of doing the above is, your calling code has no idea how the entities are coming through, so you could create a mock repository to test your app out.

static class RepositoryFactory
{
 public static IRepository<Question> GetQuestionRepo()
 {
  return new FakeQuestionRepo();
  //create your own fake repo with some fixed fake data.
 }
}

Now you're calling code does not change at all if you throw it a fake or real repository.

Also, what robert talks about in his question is a ViewModel. So you would not make a strongly typed page of type Question. So you have

class QuestionForm
{
 public string Title
 public string QuestionContent
}

You're page is going to be of type QuestionForm but in your create controller, you will take the data from the question form, fill it in your Question entity and then send it off via the repository.

[HttpPost]
public ActionResult Create(QuestionForm quesfrm)
{
 IRepository<Question> qRepo = RepositoryFactory.GetQuestionRepo();
 Question ques = new Question {
 AskedDate = DateTime.Now,
 Title = quesfrm.Title,
 Content = QuestionContent
 }
  qRepo.Save(ques);
}

Robert mentions one of the reasons why you would do this, there are a few other reasons and you can read up more on view models on SO. Also check out the code for nerddinner

You may want to see these SO questions:
Should repositories implement IQueryable<T>?
Repository pattern: One repository class for each entity?

I hope it helped.