开发者

using Autofac in a multi-layered architecture

开发者 https://www.devze.com 2023-02-03 18:53 出处:网络
I\'m fairly new to the DI/IoC concept and would like to use Autofac in a 3-layered ASP.NET Webforms application.

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

  • UI layer: An ASP.NET webforms website.
  • BLL: Business logic layer which calls the repositories on DAL.
  • DAL: .EDMX file (Entity Model) and ObjectContext with Repository classes which abstract the CRUD operations for each entity.
  • Entities: The POCO Entities. Persistence Ignorant. Generated by Microsoft's ADO.Net POCO Entity Generator.

I have asked a more general question here. Basically, I'd like to create an obejctcontext per HttpContext in my DAL. But i don't want to add a reference to DAL in UI or access to HttpContext in DAL directly. I guess this is where IoC tools come to play. The answer to my previous question is a very good example of using Windsor Castle. I'd like to use Autofac as my IoC tool and Don't know how to achieve this. (How to access DAL in application_start to register the component while I don't want to reference it in my UI, what are the proper references to be able to use DAL component in BLL with Autofac, Should I register BLL as a component with Autofac too)

Sorry folks for not providing an explicit question and requesting a kind of working exam开发者_开发问答ple, But I'm very unfamiliar to the whole IoC concept and I don't think I can achieve it to use in my current time-limited project.


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

A module groups related configuration, e.g. your DAL types, and can be loaded into an app via Web.config: http://code.google.com/p/autofac/wiki/XmlConfiguration#Modules

This will avoid the need for any hard references between your web app and DAL.

If you want to register DAL components per-request, use the InstancePerLifetimeScope() sharing modifier. This will work the same way as InstancePerHttpRequest() unless you customise the lifetime hierarchy in your app (unlikely.)

Best of luck with it!

Nick

0

精彩评论

暂无评论...
验证码 换一张
取 消