Enterprise Library - Unity Application Block 学习手册(最新版) Part 4

it2022-05-05  98

本文介绍Enterprise Library – Unity Application Block依赖注入模块的一些基本概念和使用方法。本文由 http://blog.entlib.com 开源ASP.NET博客平台小组根据EntLib Documentation编译提供,欢迎交流。   使用Unity ,最基本步骤有三步: 1. 建立容器;2. 将接口与类的映射注册到容器中;3. 从容器中解析出正确的对象。   Unity容器使用类型或者名称来识别注册或者类型映射,类型一般为接口或者一个类(通常为基类),实体类实现接口或者继承基类。Unity容器通过调用Resolve或者ResolveAll方法,返回正确的对象类型。当同一个类型有多个映射时,可以通过不同的名称来区分这些映射,同时通过代码指定使用哪一个映射。   “依赖注入”,一个实体依赖另一个实体,但在早期不体现这种依赖关系,而是把这种依赖关系提取出来,在后期注入回去。     下面介绍如何使用依赖注入映射类型和映射。   接口类型作为依赖识别符(Interface Types as Dependency Identifiers 作为一个示例,如下代码使用RegisterType和Resolve方法,传入一个接口类型名称,接口命名为IMyService,并指定容器返回一个CustomerService类的实例(该类实现了IMyService接口)。其中,IMyService类型为注册类型,请求IMyService类型将返回CustomerService对象实例。代码使用容器方法的泛型重载。 IUnityContainer myContainer = new UnityContainer(); myContainer.RegisterType<IMyService, CustomerService>(); IMyService myServiceInstance = myContainer.Resolve<IMyService>();   此外,你也可以使用该方法的非泛型重载,如下代码获得相同结果。 IUnityContainer myContainer = new UnityContainer(); myContainer.RegisterType(typeof(IMyService), typeof(CustomerService)); IMyService myServiceInstance = (IMyService)myContainer.Resolve(typeof(IMyService));   组合依赖识别符和注册名称(Combining Dependency Identifiers and Registration Names 如果相同的类型需要注册多次,可以指定不同的名称来区分每一个映射。接着通过制定名称和已注册的类型来获取合适的实例。下面的代码演示了如何为同一个接口类注册两次映射,然后Unity容器根据传入给Resolve方法的类型和名称,返回恰当的对象类型。 // Create container and register types IUnityContainer myContainer = new UnityContainer(); myContainer.RegisterType<IMyService, DataService>("Data"); myContainer.RegisterType<IMyService, LoggingService>("Logging");   // Retrieve an instance of each type IMyService myDataService = myContainer.Resolve<IMyService>("Data"); IMyService myLoggingService = myContainer.Resolve<IMyService>("Logging");   已存在对象实例的依赖识别符和BuildUp 方法(Dependency Identifier for Existing Objects and the BuildUp Method Unity 容器提供了RegisterInstance 方法,允许注册依赖注入映射 – 返回对单一已存在对象实例的引用。这一方法接收一个类型参数(对象接口或类型)和一个已存在的对象实例。当然,如果对同一类型有多个注册,还需要提供不同的命名进行区分。 下面的代码演示如何使用RegisterInstance 方法注册实现了IMyService接口的、已存在的对象实例。下面的代码同时使用了泛型和非泛型的容器方法。 DataService myDataService = new DataService(); EmailService myEmailService = new EmailService(); LoggingService myLoggingService = new LoggingService();   // Create container and register existing object instance IUnityContainer myContainer = new UnityContainer();   myContainer.RegisterInstance<IMyService>(myDataService); myContainer.RegisterInstance<IMyService>("Email", myEmailService); myContainer.RegisterInstance(typeof(IMyService), "Logging", myLoggingService);   // Retrieve an instance of each type IMyService theDataService = myContainer.Resolve<IMyService>(); IMyService theEmailService = myContainer.Resolve<IMyService>("Email"); IMyService theLoggingService  = (IMyService)myContainer.Resolve(typeof(IMyService), "Logging");   BuildUp方法一个接口或对象类型做法注册标识符,还接收一个已存在对象,并及时应用依赖注入机制。下面的代码演示如何使用BuildUp 方法,为已存在对象实例myDataService和myLoggingService应用依赖注入,这些对象实例实现了IMyService 接口。下面的演示同时使用了Unity容器的泛型和非泛型方法。 IMyService myDataService = new DataService(); IMyService myLoggingService = new LoggingService(); IMyService builtupDataService = myContainer.BuildUp<IMyService>( myDataService); IMyService builtupLoggingService   = (IMyService)myContainer.BuildUp(typeof(IMyService), myLoggingService);   通过依赖标识符检索所有已注册类型(Resolving a List of All Registered Types by Dependency Identifiers 可以通过使用容器的ResolveAll 方法,返回指定类型的所有注册对象列表。该方法接收接口或者类型参数,返回IEnumerable对象实例,包含了所有该类型已注册对象的引用(不含默认映射)。ResolveAll方法返回的列表中仅仅包含命名的实例注册。   下面的代码演示如何检索指定注册类型的所有已注册类型。 // Create container and register types using a name for each one IUnityContainer myContainer = new UnityContainer(); myContainer.RegisterType<IMyService, DefaultService>(); myContainer.RegisterType<IMyService, DataService>("Data"); myContainer.RegisterType<IMyService, LoggingService>("Logging");   // Retrieve a list of non-default types registered for IMyService // List will only contain the types DataService and LoggingService IEnumerable<IMyService> serviceList = myContainer.ResolveAll<IMyService>();   需要注意的是,如果你希望使用ResolveAll方法返回所有映射的类型,必须在注册类型映射时,同时传入注册类型和一个名称字符串。也就是,必须使用RegisterType和 RegisterInstance方法的重载,传入一个名称(字符串)和依赖类型。这是Unity 的内置设计,要么使用命名的映射,要么仅仅使用默认的映射。   http://www.entlib.com专业ASP.NET电子商务平台小组,欢迎你继续访问Unity Application Block学习手册。   参考文档: Unity Application Block Hands-On Labs for Enterprise Library

转载于:https://www.cnblogs.com/vibratea/archive/2010/09/15/1826710.html

相关资源:各显卡算力对照表!

最新回复(0)