Using Autofac in Tiered Architecture

I'm new to the DI / IoC concept and would like to use Autofac in a three-layer ASP.NET Webforms application.

  • User Interface Level: ASP.NET Web site.
  • BLL: The business logic layer that invokes repositories on DAL.
  • DAL: .EDMX file (Entity Model) and ObjectContext with repository classes that abstract CRUD operations for each object.
  • Entities: POCO objects. Persistence Ignorant. Created by Microsoft ADO.Net POCO Entity Generator.

I asked a more general question here . Basically, I would like to create an obejctcontext for the HttpContext in my DAL. But I do not want to add a link to the DAL in the user interface or directly access the HttpContext in the DAL. I assume that IoC tools appear here. The answer to my previous question is a very good example of using Windsor Castle. I would like to use Autofac as my IoC tool and don't know how to achieve this. (How to access the DAL in application_start to register a component until I want to reference it in my user interface, what are the correct links to the possibility of using the DAL component in BLL with Autofac, Should I register BLL as a component with Autofac too)

Sorry that people do not ask an explicit question and do not ask some kind of working example. But I am very unfamiliar with the whole concept of IoC, and I do not think that I can achieve its use in my current time-limited project.

+4
source share
1 answer

Autofac Modules is the technique you are looking for: http://code.google.com/p/autofac/wiki/StructuringWithModules

Configuration related to module groups, for example. your DAL types and can be loaded into the application via Web.config: http://code.google.com/p/autofac/wiki/XmlConfiguration#Modules

This avoids the need for any hard links between your web application and DAL.

If you want to register DAL components for each request, use the InstancePerLifetimeScope() exchange modifier. This will work just like InstancePerHttpRequest() , unless you set up a lifecycle hierarchy in your application (unlikely.)

Good luck!

Nick

+6
source

Source: https://habr.com/ru/post/1335341/


All Articles