Search code examples
asp.net-mvc-3ddd-repositories

MVC-3 Project Structure


I have the following for a project structure, these are all seperate projects, I was told to do it that way so not my choice.

CORE 
  --Self Explanitory
DATA 
  --Contains EF 4.1 EDMX, POCO's Generic Repository Interface
DATAMapping
  --Contains Generic Repository
Services
  -- Contains nothing at the moment
MVC 3 Application
  -- Self Explanitory

Here is my question. I have been reading that it is best practice to keep the controllers on a diet and that models / viewmodels should be dumb therefore introducing the service layer part of my project structure. The actual question now; Is this a good approach or am I creating way too much work for myself?

So if I want to say have some CRUD ops on products or categories or any of the other entities, the repository should be instantiated from the service layer / Business Logic Layer?

Some input please??


Solution

  • Personally I have my service layer referencing only generic and abstract repositories for CRUD operations. For example a service layer constructor might look like this:

    public class MyService: IMyService
    {
        private readonly IFooRepository _fooRepo;
        private readonly IBarRepository _barRepo;
    
        public MyService(IFooRepository fooRepo, IBarRepository barRepo)
        {
            _fooRepo = fooRepo;
            _barRepo = barRepo;
        }
    
        public OutputModel SomeBusinessMethod(InputModel input)
        {
            // ... use CRUD methods on _fooRepo and _barRepo to define a business operation
        }
    }
    

    and the controller will simply take an IMyService into his constructor and use the business operation.

    Then everything will be wired by the dependency injection framework of your choice.