天天看点

Unity Application Block 3月12 发布的版本

3月12日,Unity 又发布了正式发布之前的版本,这个版本提供了安装程序.并且提供了一个依赖注入在实现方式:Setter injection 的配置API。之前发布的版本,属性注入需要用[Dependency], 这种设计Unity就侵入到你的组件了。现在可以通过ConfiguringInjection。

例如StoplightPresenter依赖于Stoplight 和StoplightSchedule ,可以在属性打标签[Dependency],也可以去掉这个标记,然后在UnityContainer内通过配置API配置:

public class StoplightPresenter

{

        private Stoplight stoplight;

        private StoplightSchedule schedule;

        //[Dependency]

        public Stoplight Stoplight

        {

            get { return stoplight; }

            set { stoplight = value; }

        }

       // [Dependency]

        public StoplightSchedule Schedule

            get { return schedule; }

            set { schedule = value; }

        private IStoplightView view;

……

}

配置代码如下:

     IUnityContainer container = new UnityContainer()

                .AddNewExtension<SimpleEventBrokerExtension>()

                .RegisterType<ILogger, TraceLogger>()

                .RegisterType<IStoplightTimer, RealTimeTimer>();

     container.Configure<InjectedMembers>()

                .ConfigureInjectionFor<StopLight.Logic.Stoplight>(

                new InjectionProperty("Logger")

                );

                .ConfigureInjectionFor<StoplightPresenter>(

                new InjectionProperty("Stoplight"),

                new InjectionProperty("Schedule")

Unity Application Block虽然发展时间不长,主要是基于从企业类库2.0开始出现的ObjectBuilder发展而来,下一个微软Enterprise Library的版本V4——将预置支持依赖注入。依赖注入将通过容器以独立或作为库的一部分来提供。需要更深入的学习Unity Application Block,可看园子里的TerryLee、doriandeng和overred的相关文章:

TerryLee的Unity Application Block

依赖注入容器Unity Application Block(1):快速入门   

Enterprise Library 4.0中的依赖注入容器(Unity)预览

doriandeng的Unity

使用 Unity(一):Unity 应用程序块容器介绍

使用 Unity(二):配置 Unity 、读取配置信息和获取对象

使用 Unity Application Block(三):理解和使用依赖注入的键

Unity Feb 26 Weekly Drop

overred的 ① NET Framework

依赖注入容器Unity Application Block(2):Unity的春天   

顺便学习一下英语的还可以看:  

Unity Dependency Injection IoC Screencast

Unity IoC and ASP.NET MVC Framework - Dependency Injection of Controllers

Unity Nested Containers - IUnityParentContainer and CreateChildContainer

Unity IoC - February 26 Weekly Drop - LifetimeManagers TearDown Extensions and IDisposable

Using Unity and the ASP.NET MVC Preview 2

本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/73541,如需转载请自行联系原作者

上一篇: Tomcat连接池

继续阅读