zoukankan      html  css  js  c++  java
  • Microsoft.Practices.Unity mvc controller 注入

    上一文 Microsoft.Practices.Unity 通用类

    MVC3对于依赖注入提供更好的支持。我们可以使用- IDependencyResolver 和 IControllerActivator 来实现对controller的注入。
    IDependencyResolver公开两个方法 - GetService的GetServices.The GetService方法解决了单独注册的服务,
    支持任意对象的创建,GetServices解决注册多个服务。
    IDependencyResolver接口的实现应该委托给底层的依赖注入容器提供注册服务请求的类型。
    当有没有注册的服务请求的类型,ASP.NET MVC框架预计这个接口的实现返回GetService为空,并从GetServices返回空集合。
    让我们以统一提供依赖注入工作IDependencyResolver intreface派生创建一个自定义的依赖解析器类。

    这一节直接从Application_Start说起

    实现IDependencyResolver接口

        public class UnityDependencyResolver : IDependencyResolver
        {
            IUnityContainer container;
            public UnityDependencyResolver(IUnityContainer container)
            {
                this.container = container;
            }
    
            public object GetService(Type serviceType)
            {
                try
                {
                    return container.Resolve(serviceType);
                }
                catch
                {
                    return null;
                }
            }
    
            public IEnumerable<object> GetServices(Type serviceType)
            {
                try
                {
                    return container.ResolveAll(serviceType);
                }
                catch
                {
                    return new List<object>();
                }
            }
        }
    View Code
     
    读取.container配置文件,注册依赖关系
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                //BundleTable.EnableOptimizations = true;
    
                IUnityContainer container = GetUnityContainer();
                DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    
            }
    
            private IUnityContainer GetUnityContainer()
            { 
                Type type = typeof(TestInterface.EmptyInterface);
                string name = type.Assembly.GetName().Name;
                string configName = (Path.GetDirectoryName(type.Assembly.CodeBase) +
                    @"" + name + ".container").Substring(6);
                return ObjectContainer.LoadContainer(configName); 
            }
        }
    View Code

    就这些,然后f5一下,看结果

        public class testController : Controller
        {
            //
            // GET: /test/
            [Dependency]
            public EmptyInterface e { get; set; }
    
            public ActionResult Index()
            {
                return View();
            }
    
            public string testUnity()
            {
                //EmptyInterface e = ObjectContainer.CreateObject<EmptyInterface>();
                return e.sayHello();
                //return "testUnity";
            }
        }
    View Code

    原理上的东西,暂时讲不太清楚,先记下怎么用,慢慢理解

     
     
     
     
     
     
     
     
     
    作者:zc
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/jmzs/p/4930204.html
Copyright © 2011-2022 走看看