1

If DbContext is in the DAL then the generic type arguments of the DbSets cannot be the BLL classes (domain model). What are the best practice ways to separate these layers? An extra model in the DAL? Interfaces?

3 Answers 3

3

If you're doing DDD, I believe the repository (at least the interface for it) is part of your business / domain layer. Your implementation of the repository will be a separate assembly which would have to reference that business / domain layer. So your DAL knows about your business objects, but not the other way around. To do dependency injection, you'll probably have in your DAL layer something that configures your container to use Repository for your IRepository interface. If you need a unit of work patter, your interface would likely have to be part of the business layer as well. Again your implementation will be in your DAL and the DAL would configure the DI container appropriately. This is actually one of the things I dislike about the repository pattern, as you either need to ensure your users of your interface correctly manage the IUnitOfWork, or you need something to wrap the repository which does so.

In a traditional n-layer architecture, things are a bit different. In that case your business layer can talk to the DAL, and I've normally built the DAL to have DTOs which represent a row of data in the database. The business layer will then use these DTOs to hydrate the business objects (or if you're using something like CSLA.Net, the business objects know how to hydrate themselves).

Either way there shouldn't be a situation where you end up with a circular reference.

2
  • Thanks. Clear and concise but I am still processing it. So for DDD putting IRepository (and IUnitOfWork too?) in the BLL works but then I need to tell BLL that IUnitOfWork is currently DbContext, etc. That is dependency injection? A separate assembly? Can you explain?
    – Benjamin
    Apr 10, 2013 at 19:34
  • @Benjamin I've updated my answer to include your new questions.
    – Andy
    Apr 11, 2013 at 15:18
1

I usually consider the domain model as a separate layer.
If we look at the classic MVC paradaigm, then the model is used by both the View and the Controller.
No reason why it shouldn't be used by the DAL as well.

The Model, however, will not reference the DAL; all operations against the data store will be done by the controller.

So the general flow of things would be-
user interacts with the View View invokes a method on the Controller Controller uses the DAL to retrieve Model objects Controller invokes methods on Model objects, saves them (using DAL) if necessary, and returns an answer to the View

3
  • Are you making an analogy between MVC and the BLL-DAL relationship? I think the M in MVC is a distinct presentation model, not the BLL domain model. I know it can be all the same but I am wondering about best practices for decoupling layers.
    – Benjamin
    Apr 10, 2013 at 18:47
  • 1
    M is definitely model, and nothing to do with presentation; see here codinghorror.com/blog/2008/05/… (the writer put the data access into the model layer, which is also allowed)
    – J. Ed
    Apr 10, 2013 at 19:05
  • I don't understand where the BLL and DAL are in your scenario.
    – Benjamin
    Apr 10, 2013 at 19:50
0

Your BLL or Domain Layer should not worry about data access technical details, BLL shold be technology independent. If you want to stick with Entity framework you should generate POCO entities and move them to seperate layer, this way you can avoid circualr references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.