首页 > 代码库 > Autofac - MVC/WebApi中的应用

Autofac - MVC/WebApi中的应用

参考页面:

http://www.yuanjiaocheng.net/ASPNET-CORE/asp-net-core-overview.html

http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html

http://www.yuanjiaocheng.net/ASPNET-CORE/newproject.html

http://www.yuanjiaocheng.net/webapi/web-api-gaisu.html

http://www.yuanjiaocheng.net/webapi/create-web-api-proj.html

Autofac前面写了那么多篇, 其实就是为了今天这一篇, Autofac在MVC和WebApi中的应用.

一、目录结构

先看一下我的目录结构吧, 搭了个非常简单的架构, IOC(web), IBLL, BLL, IDAL, DAL, Helper

技术分享

public interface ITestBll
{
    void Say(List<string> msg);
}

 我里面都是为了实现Say方法的.

public class TestBll : ITestBll
{
    public void Say(List<string> msg)
    {
        msg.Add("进入TestBll中的方法Say");
        var dal =   IocContainer.Create<ITestDal>();
        dal.Say(msg);
    }
}

ITestDal , TestDal 和上面这两个内容其实是一样的, 只是分层不一样. 这里就不贴了.

 

二、实现

在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, api 在asp.net中, 都是有自己的一套 ioc 方法的, 这里其实也可以不写. 不过这里就是为了在这里面的使用来说的.

在helper类库中, 对autofac具体实现, 加了一些封装.

public class IocContainer
{
    private static ContainerBuilder builder;

    private static IContainer container;

    static IocContainer()
    {
        builder = new ContainerBuilder();
    }

    #region 注册接口
    public static void RegisterTypeInstancePerLifetimeScope<T>()
    {
        builder.RegisterType<T>().InstancePerLifetimeScope();
    }
    public static void RegisterTypeInstancePerDependency<T>()
    {
        builder.RegisterType<T>().InstancePerDependency();
    }
    public static void RegisterTypeSingleInstance<T>()
    {
        builder.RegisterType<T>().SingleInstance();
    }

    /// <summary>
    /// 注册接口
    /// </summary>
    /// <typeparam name="T">实现类型</typeparam>
    /// <typeparam name="IT">接口类型</typeparam>
    public static void RegisterType<T, IT>()
    {
        builder.RegisterType<T>().As<IT>();
    }
    /// <summary>
    /// 自动装配接口
    /// </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

    #region 注册Mvc
    public static void RegisterMvc()
    {
        builder.RegisterControllers(Assembly.GetCallingAssembly());
    }
    #endregion

    #region 注册WebApi
    public static void RegisterWebApi()
    {
        builder.RegisterApiControllers(Assembly.GetCallingAssembly());
    }
    #endregion

    #region 创建对象
    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
}

做完上面的这些, 就可以在Controller, BLL, DAL中, 通过ioc的方式, 来获取想要获取到的类的实例

 

Autofac - MVC/WebApi中的应用