zoukankan      html  css  js  c++  java
  • Autofac 的构造函数注入方式

    介绍

    该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入。注入方式通过构造函数。

    在编写 aufofac 的依赖注入代码之前先准备一些基础类。

    基础类

    public class UserInfo
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    }
    public interface IRepository<T>
    {
        void Add( T item );
    
        void Modifty( T item );
    
        List<T> Find( Expression<Func<T, bool>> predicate = null );
    }
    public interface IUserRepository:IRepository<UserInfo>
    {
    
    }
    public class UserRepository : IUserRepository
    {
        private static List<UserInfo> _users;
    
        public UserRepository()
        {
            _users = new List<UserInfo>();
        }
    
        public void Add( UserInfo item )
        {
            _users.Add( item );
        }
    
        public List<UserInfo> Find( Expression<Func<UserInfo, bool>> predicate = null )
        {
            if ( predicate == null ) return _users;
    
            return _users.Where( predicate.Compile() ).ToList();
        }
    
        public void Modifty( UserInfo item )
        {
            var user = _users.Find( u => u.Id == item.Id );
    
            if ( user == null ) return;
    
            user.Name = item.Name;
        }
    }
    public interface IAppService<T>
    {
        void Add( T item );
    
        void Modify( T item );
    
        List<T> Find( Expression<Func<T, bool>> predicate = null );
    }
    public interface IUserAppService:IAppService<UserInfo>
    {
    
    }
    public class UserAppService : IUserAppService
    {
        private readonly IUserRepository _userRepository;
    
        public UserAppService(IUserRepository userRepository)
        {
            this._userRepository = userRepository;
        }
    
        public void Add( UserInfo item )
        {
            this._userRepository.Add( item );
        }
    
        public List<UserInfo> Find( Expression<Func<UserInfo, bool>> predicate = null )
        {
            return this._userRepository.Find( predicate );
        }
    
        public void Modify( UserInfo item )
        {
            this._userRepository.Modifty( item );
        }
    }
    public class HomeController : Controller
    {
        private IUserAppService _userAppService;
    
        public HomeController(IUserAppService userAppService)
        {
            this._userAppService = userAppService;
        }
    
        public ActionResult Index()
        {
            ViewBag.Message = "Home-Index";
            this._userAppService.Add( new UserInfo { Id = 1, Name = "JRoger" } );
            ViewBag.Users = this._userAppService.Find();
    
            return View("~/Views/Home/Index.cshtml");
        }
    }

    构造函数注入代码

    虽然以上代码没有注释,但觉得结构已经非常清楚了,不用再过多的解释。 但有一点需要注意一下,在 HomeController 类和 UserAppService 类的构造函数中都有传进来的实例对象(我已经用橘红色标记了)。这也是使用构造函数注入的其中一个关键点。
    下面来看一下实现依赖注入的核心代码怎么调用:

    private void _InitIoC()
    {
        var builder = new ContainerBuilder();
    
        builder.RegisterControllers( typeof( MvcApplication ).Assembly );
    
        builder.RegisterAssemblyTypes( typeof( MvcApplication ).Assembly )
            .Where( t => (t.Name.EndsWith( "Repository" ) || t.Name.EndsWith("AppService")) && !t.IsAbstract )
            //.InstancePerDependency()    //每次都创建一个对象。
            //.SingleInstance()   //每次都是同一个对象。
            //.InstancePerLifetimeScope()     //同一个生命周期生成的对象是同一个。
            .InstancePerRequest()   //单个 Web/HTTP/API 请求的范围内的组件共享一个实例。仅可用于支持每个请求的依赖关系的整合(如MVC,Web API,Web Forms等)。
            .AsImplementedInterfaces();
    
        var container = builder.Build();
        var resolver = new AutofacDependencyResolver( container );
    
        DependencyResolver.SetResolver( resolver );
    }

    其它:

    Autofac.dll 版本 3.4.0.0
    Autofac.Integration.Mvc.dll 版本 3.3.4.215 

  • 相关阅读:
    Anglarjs 工具方法
    AngularJs $scope 里面的$apply 方法和$watch方法
    CentOS 下tomcat安装
    Phonegap Android 项目使用Cordova
    Phonegap 原生控件(Android)与html混合
    Phonegap 通信原理
    Phonegap 开发环境搭建
    Phonegap 通知 Notification
    Phonegap 工作原理
    Angularjs MVC
  • 原文地址:https://www.cnblogs.com/jroger/p/4734699.html
Copyright © 2011-2022 走看看