MVC5+Unity4.0注入依赖学习

来源:互联网 发布:淘宝网毛衣货源 编辑:程序博客网 时间:2024/06/11 05:56

概念: 
Unity是一个轻量级的可扩展的依赖注入容器,支持构造函数,属性和方法调用注入。Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问题。构建一个成功应用程序的关键是实现非常松散的耦合设计。松散耦合的应用程序更灵活,更易于维护。这样的程序也更容易在开发期间进行测试。你可以模拟对象,具有较强的具体依赖关系的垫片(轻量级模拟实现),如数据库连接,网络连接,ERP连接,和丰富的用户界面组件。例如,处理客户信息的对象可能依赖于其他对象访问的数据存储,验证信息,并检查该用户是否被授权执行更新。依赖注入技术,可确保客户类正确实例化和填充所有这些对象,尤其是在依赖可能是抽象的 。

更多知识:Unity

步骤: 
新建MVC项目,添加Nuget包,如截图中的2个: 

缺少第二个将不能使用属性依赖的方式进行注入!!! 
Unity.Mvc包安装后,会在Mvc项目出现下面2个文件: 

接下来编写测试代码;

接口层代码:

public interface IA    {        void fuck();    }    public interface IB    {    }    public interface IC    {    }    public interface ID    {    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

下面是接口实现:

public class A : IA    {        public IB B { get; set; }        [Dependency]        public IC C { get; set; }        public ID D { get; set; }        [InjectionMethod]        public void Initalize(ID d)        {            this.D = d;        }        public void fuck()        {            ;        }    }    public class B : IB    {    }    public class C : IC    {    }    public class D : ID    {    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在App_Start目录下的UnityConfig文件中,注册类型映射关系

public static void RegisterTypes(IUnityContainer container)        {            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.            // container.LoadConfiguration();            // TODO: Register your types here            // container.RegisterType<IProductRepository, ProductRepository>();            container.RegisterType<IA, A>();            container.RegisterType<IB, B>();            container.RegisterType<IC, C>();            container.RegisterType<ID, D>();        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

以后每增加一种映射关系,都要在这里注册,当然你也可以通过别的方式实现注册:如配置文件等。

准备工作OK了,可以直接在controller中使用了

public class UserController : Controller    {        [Dependency]        public IA _A { get; set; }        public UserController()        {        }        public ActionResult Index()        {            return View();        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

调试时就会发现,_A不是null,而是一个new A();


在自己学习过程中,出现了几种错误:

列表内容

1、Resolution of the dependency failed, type = “IA”, name = “(none)”. 
这个是因为IA的映射关系没有注册;

2、Resolution of the dependency failed, type = “HomeController”, name = “(none)”. 
这个是因为没有引用Unity.Mvc包

0 0