1

Attempting to create an MVC project using EF and the Model first approach.

In order to implement it in a way that the Web and data portions are loosely coupled I'm attempting to implement the repository pattern, but, after reading many articles I'm still trying to grasp what objects my Repository interface should return or how they should bind/map to the 'M' model in my MVC project.

Here's a very simplistic example of what I'm asking.

//Repository Interface
public interface IMemberRepository
{
    Member GetById(int id);
    IEnumerable<Member> FindByName(string name);
}

//Repository Interface Implementation
public class MemberRepository : IMemberRepository
{
    //My DB Context object created by EF
    private MyContainer context;

    public MemberRepository(MyContainer context)
    {
        this.context = context;
    }

    public Member GetById(int id)
    {
        return context.Members.SingleOrDefault(x => x.Id == id);
    }

    public IEnumerable<Member>  FindByName(string name)
    {
        return context.Members.Find(x => x.name == name);
    }

}

So using Ninject as my DI framework I could call this from my controller as follows:

public class GroupsController : Controller
{
    public ViewResult Find(string name)
    {
        IMemberRepository repo = 
          ObjectFactory.Instance.CreateInstance<IMemberRepository>();
        return repo.FindByName(name);
    }
 }

At this point I'm just not understanding how my Member object (from the EF model) is supposed to bind/map to my 'MVC' member model object. It seems I must be missing some sort of mapping layer or I'm just completely off track. If I were to reference my EF generated class directly it feels like I'm negating the point of using an interface and DI framework. Any advice or direction at this point would be appreciated.

1 Answers1

2

Your IMemberRepository interface and its implementation MemberRepository look right to me. That is how I structure my database code as well.

Moving to the MVC world, I would create view models which you populate from your data model. This will give you the flexibility of adding any attributes or additional properties that you need in your view.

This would be the workflow:

  1. Fetch object(s) from repository
  2. Populate view model objects with all the data from your repository object(s)
  3. Return the view model to your view from the controller
Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
  • So you're saying that in step 2 the controller would somehow populate my view model then return it? Wouldn't that mean then that my controllers would be tightly coupled to my Data model? I'm probably missing something. –  Feb 19 '13 at 17:18
  • @cdlong - You can do the mapping in your controller or you can do it in an intermediary layer. That's a decision for you to make. Regardless of what you choose, if you want to use view models, you have to do that mapping somewhere. – Justin Helgerson Feb 19 '13 at 20:38
  • Ek0nomik - thanks for the help. Think I'm finally starting to understand a bit more. –  Feb 19 '13 at 21:26