转 Autofac怎么依赖注入ASP.NET MVC5类的静态方法

之前我有介绍过怎么在ASP.NET mvc5中实现的Controller的依赖注入。一般是通过Contrller的构造函数的参数或者属性来注入,但是这有一个共同点就是调用这个类的方法一般都是实例方法,也就是要实例化这个类才能调用它的方法。但是如果它是一个静态方法我们又该怎么办呢?其实也很简单,下面我们就通过一个写日志的组件来介绍怎么实现ASP.NET MVC5类的静态方法的依赖注入。因为考虑到很多地方都要用到写日志这个方法,而且我们又不想每次调用都需要new一个对象,所以把调用方法封装成一些静态方法。

DependencyRegistrar.cs

using Autofac;
using Autofac.Integration.Mvc;
using Lanhu.Services;
using Lanhu.Services.MetionNowOrder;
using Lanhu.Services.Member;
using System.Web.Mvc;
 
namespace Lanhu.Admin
{
 
    public class DependencyRegistrar
    {
        public static void RegisterDependencies()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
 
            builder.RegisterType<Log_UserLogonService>().As<ILog_UserLogon>().InstancePerLifetimeScope();
            builder.RegisterType<Log_UserOperateService>().As<ILog_UserOperate>().InstancePerLifetimeScope();
           //autofac 注册依赖
           IContainer container = builder.Build();
           DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
      }
  }
 
}

 

在App_Start事件中调用上面的会依赖注入代码:

using Lanhu.Core;
using System;
using System.Collections.Generic;
using System.linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
 
namespace Lanhu.Admin
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
 
            DependencyRegistrar.RegisterDependencies();
           
        }
    }
}

 

LogFacade.cs:

using Lanhu.Model;
using Lanhu.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
 
namespace Lanhu.Admin.Infrastructure
{
    public class LogFacade
    {
        public static bool AddLogonLog(MLog_UserLogon model)
        {
            var service = DependencyResolver.Current.GetService<ILog_UserLogon>();
            return service.Insert(model);
        }
 
        public static bool AddOperateLog(string info,bool isError=false)
        {
            var service = DependencyResolver.Current.GetService<ILog_UserOperate>();
            if (isError)
                model.ErrorMsg = info;
            else
                model.Msg = info;
            return service.Insert(model);
        }
 
        public static bool AddOperateLog(MLog_UserOperate model)
        {
            var service = DependencyResolver.Current.GetService<ILog_UserOperate>();
            return service.Insert(model);
        }
    }
}

 

上面封装了三个静态方法,在方法里面通过DependencyResolver.Current.GetService从Ioc容器中获取相应类型的依赖对象。这个LogFacade使用外观模式,让调用显得很简单。

转自:https://www.lanhusoft.com/Article/366.html

posted @ 2018-09-27 17:28  杜子烟  阅读(357)  评论(0编辑  收藏  举报