3

If I want to combine using repositorys per entity and Viewmodels per view how does it work out?

Any website tips I could look up? Maby someone could give an easy example?

Thanks

Best Regards!

Obsivus
  • 8,231
  • 13
  • 52
  • 97
  • 1
    Ideally, your repositories should not be "per entity" as you put it. Martin Fowler defines repositories to act on the "aggregate root"; in other words, a repository should perfrom CRUD operations that makes sense in a business logic sense (i.e. not a 1-to-1 mapping to tables in your database). If you're overall business logic/database is relatively small, you might want to weigh whether using the repsoitory pattern is even worth it; EF might fulfill all of you're data access needs w/out warranting a full-blown repository pattern. – Didaxis Feb 19 '12 at 19:06

3 Answers3

3

I like the following structure (from the famous Steven Sanderson's Pro ASP.NET MVC series):

Domain Project (Business Logic):

  • Abstract Folder (repository interfaces)
  • Concrete Folder (repository implementations)
  • Entities (EF generated classes)

Web UI Project (MVC Web App):

  • Models (view models)
  • Views
  • Controlers
  • etc, you get the point

The main thing is you're separating your business logic (which should house your repositories) from your Web UI (the MVC project)

In this scenario, your Controller classes reference the domain layer and use DI/IoC to call up the correct instance of the repository.

Example controller class:

namespace MyMvcProject
{
    using System.Whatever;
    using MyDomainLayer;

    public class MyController : Controller
    {
        private readonly IMyRepository _myRepository;

        public MyController(IMyRepository myRepository)
        {
            // Resolved using your favorite DI/IoC Container:
            this._myRepository = myRepository;
        }

        public ActionResult DoSomething()
        {
            var stuff = _myRepository.GetStuff();
            return View(stuff);
        }
    }
}
jim tollan
  • 22,305
  • 4
  • 49
  • 63
Didaxis
  • 8,486
  • 7
  • 52
  • 89
  • ErOx, i just changed the private myRepository to _myRepository for clarity – jim tollan Feb 19 '12 at 18:48
  • No biggie, that's why I typically use the **this** keyword for that since I don't usually prefix my private vars with an underscore. Your edit is clearer for instructional purposes; thank you – Didaxis Feb 19 '12 at 18:51
2

Use AutoMapper to copy data from entities to models and vice-versa. This will reduce a lot of 'plumbing' code you will have to write otherwise.

Nouman Bhatti
  • 1,341
  • 6
  • 28
  • 54
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0

I'm not a professional developer but I think Steve Sanderson's model is not the right model for some projects because you are working in your views against the model directly. What happen if you want to show only a few properties and not all of them? Your full model is traveling to the view.

I think your views must work against viewmodel classes and not directly the model coming from orm (trough repository, etc.)

The only problem that I'm finding is the mapping process between model to viewmodel and viewmodel to model. Let me explain...

I was trying to do this mapping with automap, the direction between model -> viewmodel works fine, but in the other direction (viewmodel to model) I'm not finding the way to do it automatically because the viewmodel, normally does not own all the properties that model have and if you do an automap to model object a lot of properties are empty. Finally you need to make always some manual mappings.

Ideas for this situation may be welcome. Thanks

Jose
  • 95
  • 7