3

I'm working on a web application project using ASP.NET MVC3 and database in SQL Server. There is also a mobile application that uses the data from the same database via REST services. Here are some of my application's layers:

  • Model - ADO.NET Data Model, using Entity Framework

  • Data Access Layer - Repositories with queries to retrive data from database

  • Web application - MVC3 project, using repositories, loose coupling using Structure Map and DI, database context gets disposed at the end of the HttpRequest

  • Core - another layer between DAL and Service Layer, uses Repositories and exposes data to Service Layer. Sort of Business Logic Layer.

  • Service Layer - REST services, knows about Core layer but not about DAL. Maps the data to DTOs and exposes to the client

The problem I've got with such application architecture is loose coupling on the Service Layer. Service Layer has reference to Core layer. Core layer has reference to Data Access Layer and uses its repositories. Repositories do not have a default constructor though. They expect 1 parameter and its database object context (disposable object).

Using repositories directly on my website is not a problem. I'm using Structure Map and DI makes it loosely coupled. Each context gets disposed at the end of the HttpRequest.

The problem is that Service Layer and Core layer. I'd like to have loose coupling there as well but not sure how to achieve it? How to inject data context into those and make sure it gets disposed at certain moment? I'd like to hear some suggestions on how to put it all together.

Nebojsa Veron
  • 1,545
  • 3
  • 19
  • 36

2 Answers2

5

Service Layer has reference to Core layer.

That's fine.

Core layer has reference to Data Access Layer and uses its repositories.

That ain't fine.

Your "Core" should be your domain, with business rules and logic. It should not have any dependencies.

Start from the bottom of the stack:

  1. Repo - no dependencies on other layers.
  2. Services - dependency on Core and Repo.
  3. Core - no dependencies on other layers.
  4. Web - dependant on everything.

This is how we do it. We use a combination of interface-driven programming and dependency injection to handle the loose coupling.

Example flow:

  1. HTTP Request comes in (API, web tier, etc)
  2. Controller found. DI container sees container has dependancy on ISomethingService and resolves it, including any further down dependencies (service, repo, etc).
  3. Controller calls method on ISomethingService.
  4. ISomethingService implementation (chosen by DI) calls method on ISomeRepo.
  5. ISomeRepo implementation (chosen by DI) calls EF/DB, returns "data-object" to service.
  6. Service maps "data-object" to "Core" object and returns to controller.

The instantiation of these objects should be handled by your DI container. The only thing missing from the above which we use is a "Unit of Work", which essentially wraps the EF context.

RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • I've got my Model in another project so Repositories have dependency on that layer. Repositories are generated from Model.edmx using T4 template. I also have no problems with DI and loose coupling on the Web, it works perfectly and EF context gets disposed aat the end of Request. I'd also like to mention I'm not using services to get the data on the web, but Repositories directly. Services will be used for mobile applications only. I'm not sure how to achieve loose coupling on the service side, since Service instantiates a Repository which needs EF Context as a parameter in its constructor. – Nebojsa Veron Jul 12 '11 at 08:30
  • @Nebo - well does the Repo take a EF Context, or an interface? It should take a interface, e.g a `IUnitOfWork`. Under the hood, you would implement a `EntityFrameworkUnitOfWork` which wraps the EF context - that's how you get the loose coupling. Only your Repository should reference the EF assembly. – RPM1984 Jul 12 '11 at 09:30
  • This is one of my repositories: http://screencast.com/t/IVHzTVIx. This is one of managers in Core (between Repo and Service): http://screencast.com/t/qCgoVTlGLZy. And this is the service (no constructor taking UserManager here because I've got problems hosting it): http://screencast.com/t/Is3zpkrVP. ServiceHost only supports class service types so I cannot host an Interface but a class. – Nebojsa Veron Jul 12 '11 at 14:03
  • I got what you said and now have DI working nice. I have this line of code to make it work tho: `ObjectFactory.Configure(x => x.For().Use(() => new Entities()));`. I am using StructureMap but I'm not sure my DataContext will get disposed? Since this is not an MVC application I can't make it dispose at the end of HttpRequest. How to make it dispose? – Nebojsa Veron Jul 12 '11 at 16:48
  • @Nebo - try using `For().HybridHttpOrThreadLocalScoped().Use(() => new Entities());`. I also use structuremap and this is what i use. The question says your working on an MVC 3 application - but now youre saying your not? – RPM1984 Jul 12 '11 at 23:52
  • @Nebo - looking at your pictures, your not using interfaces for dependencies, that's your problem. Your Repository should take an interface, not the Entities. The interface could be a `IUnitOfWork`, or another class which wraps the EF context. Same deal with your manager - `IRepository` or `IActivityRepository`, not `ActivityRepository`. Your not using the real benefits of DI if your not programming via interfaces. – RPM1984 Jul 12 '11 at 23:54
  • My Web is MVC3 and I've got no problems with that, but with WCF rest services. Thanks for the Scope solution. Same question as below, why have Repository interfaces? Each repository have specific methods to retrive data, not all methods are needed in each. My practice was to write partial repository classes with different queries in each. Working with interfaces means declaring all the methods each interface will implement, no matter if it needs it. An alternative is having different interface for each repo (IActivityRepository as you said). – Nebojsa Veron Jul 13 '11 at 00:02
  • @Nebo - you should have a abstract class called `GenericRepository`, which implements the core EF functionality (query, save, delete, etc), then specific repositories that inherit from this, and provide **additional** functionality. Anyway it's up to you - this is all a matter of preference. Not sure how much else i can help. – RPM1984 Jul 13 '11 at 03:56
1
public ServiceLayerClass()
{
    private ICoreLayerClass coreLayerClass;

    public ServiceLayerClass(ICoreLayerClass coreLayerClass)
    {
        this.coreLayerClass = coreLayerClass;
    }

    public void DoSomeWork()
    {
        coreLayerClass.DoSomeWork();
    }
}

public CoreLayerClass()
{
    private ISomeRepository someRepository;

    public CoreLayerClass(ISomeRepository someRepository)
    {
        someRepository = this.someRepository;
    }

    public void DoSomeWork()
    {
        someRepository.DoSomeWork();
    }
}

public SomeRepository()
{
    public SomeRepository(IUnitOfWork uow)
    {
    }

    public void DoSomeWork()
    {
        //do some work
    }
}

Notes: UnitOfWork would ideally be created per HttpContext. ie., your datacontext will begin its life at the beginning Request and will get disposed at the end. You will use only one per request.

ravi
  • 949
  • 11
  • 22
  • This is the way I've got it now. Just not sure how to set up DI to resolve all the dependencies, from Service to Repository, and how to make sure IUnitOfWork (data context) gets disposed. – Nebojsa Veron Jul 12 '11 at 08:34
  • I'm trying to make my DataContext created per HttpContext, but since this is a web service and not a MVC applicaition (where it gets disposed at the end of request), I get a StructureMap unknown error if I set up lifecycle to HttpContextScoped for my DataContext. – Nebojsa Veron Jul 12 '11 at 16:52
  • Is this a WCF Rest or ASP.Net MVC Rest? – ravi Jul 12 '11 at 23:21
  • It is WCF rest service. Another question is why have Repository interfaces? Each repository have specific methods to retrive data, not all methods are needed in each. – Nebojsa Veron Jul 12 '11 at 23:27
  • sure, each repository will have its own methods. But having interfaces helps with unit testing as you can inject a different implantation in certain unit testing scenarios. – ravi Jul 13 '11 at 00:54
  • Regarding DataContext and StructureMap, you are better of raising a question specifically mentioning this keywords and you can get some StructureMap expert's attention. – ravi Jul 13 '11 at 00:56