Autofac and autofac Chinese documents
I have written so many articles before Autofac. In fact, this article is intended for today's use of Autofac in MVC and WebApi.
I. directory structure
Let's take a look at my directory structure and build a very simple architecture: IOC (web), IBLL, BLL, IDAL, DAL, Helper
public interface ITestBll{ void Say(List<string> msg);}
I want to implement the Say method.
Public class TestBll: ITestBll {public void Say (List <string> msg) {msg. add ("method to enter TestBll Say"); var dal = IocContainer. create <ITestDal> (); dal. say (msg );}}
ITestDal and TestDal are the same as the two above, but the layers are different. I will not post them here.
II. Implementation
Call a registration method at the end of Application_Start.
protected void MvcInit(){ IocContainer.RegisterMvc(); IocContainer.RegisterWebApi(); IocContainer.RegisterTypes(System.Reflection.Assembly.Load("BLL").GetTypes()); IocContainer.RegisterTypes(System.Reflection.Assembly.Load("DAL").GetTypes()); IocContainer.Build();}
Mvc and api in asp.net all have their own set of ioc methods, which can be omitted here. However, this is for use here.
In the helper class library, some encapsulation is added to the autofac implementation.
Public class IocContainer {private static ContainerBuilder builder; private static IContainer container; static IocContainer () {builder = new ContainerBuilder () ;}# region registration interface public static void RegisterTypeInstancePerLifetimeScope <T> () {builder. registerType <T> (). instanceperlifetimesency ();} public static void RegisterTypeInstancePerDependency <T> () {builder. registerType <T> (). instancePerDependency ();} public static void RegisterTypeSingleInstance <T> () {builder. registerType <T> (). singleInstance ();} /// <summary> /// registration interface /// </summary> /// <typeparam name = "T"> Implementation type </typeparam> /// <typeparam name = "IT"> interface type </typeparam> public static void RegisterType <T, IT> () {builder. registerType <T> (). as <IT> ();} /// <summary> /// automatic assembly interface /// </summary> /// <param name = "types"> </param> public static void RegisterTypes (params type [] types) {builder. registerTypes (types ). asImplementedInterfaces ();} public static void RegisterType <T, IT> (string name) {builder. registerType <T> (). named <IT> (name);} public static void RegisterType <T, IT> (int key) {builder. registerType <T> (). keyed <IT> (key) ;}# endregion # region Build public static void Build () {container = builder. build (); DependencyResolver. setResolver (new AutofacDependencyResolver (container); // DependencyResolver. setResolver (new AutofacWebApiDependencyResolver (container); GlobalConfiguration. configuration. dependencyResolver = new AutofacWebApiDependencyResolver (container);} # endregion # register Mvc public static void RegisterMvc () {builder. registerControllers (Assembly. getCallingAssembly ();} # endregion # region registration WebApi public static void RegisterWebApi () {builder. registerApiControllers (Assembly. getCallingAssembly () ;}# endregion # region Create object public static T Create <T> () {return container. resolve <T> ();} public static T Create <T> (string name) {return container. resolveNamed <T> (name);} public static T Create <T> (int key) {return container. resolveKeyed <T> (key);} public static object Create (Type t) {return container. resolve (t) ;}# endregion}
After completing the above steps, you can use the ioc method in Controller, BLL, and DAL to obtain the desired class instance.