Dependency Injection
- 格式:docx
- 大小:107.69 KB
- 文档页数:10
Ioc英文为Inversion of Control,即反转模式,这里有著名的好莱坞理论:你呆着别动,到时我会找你。
后被Martin Fowler改名为Dependency Injection 依赖注射,也就是将类之间的关系通过第三方进行注射,不需要类自己去解决调用关系。
AInterface a = new AInterfaceImp();AInterfaceImp是接口AInterface的一个子类,Ioc模式可以延缓接口的实现,根据需要实现,有个比喻:接口如同空的模型套,在必要时,需要向模型套注射石膏,这样才能成为一个模型实体,因此,我们将人为控制接口的实现成为“注射”。
其实Ioc模式也是解决调用者和被调用者之间的一种关系,上述AInterface实现语句表明当前是在调用被调用者AInterfaceImp,由于被调用者名称写入了调用者的代码中,这产生了一个接口实现的原罪:彼此联系,调用者和被调用者有紧密联系,在UML中是用依赖Dependency 表示。
但是这种依赖在分离关注的思维下是不可忍耐的,必须切割,实现调用者和被调用者解耦,新的Ioc模式Dependency Injection 模式由此产生了,Dependency Injection模式是依赖注射的意思,也就是将依赖先剥离,然后在适当时候再注射进入。
在通常传统情况下,为了实现调用者和被调用者解耦,分离,一般是通过工厂模式实现的,下面将通过比较工厂模式和Ioc模式不同,加深理解Ioc模式。
工厂模式和Ioc假设有两个类B 和C:B作为调用者,C是被调用者,在B代码中存在对C的调用:实现comp实例有两种途径:单态工厂模式和Ioc。
工厂模式实现如下:特点:每次运行时,MyFactory可根据配置文件XML中定义的C子类实现,通过createInstanceOfC()生成C的具体实例。
使用Ioc依赖性注射( Dependency Injection )实现Picocontainer如下,B类如同通常POJO 类,如下:public class B{private C comp;public B(C comp){p = comp;}public void someMethod(){p.sayHello();}......}假设C接口/类有有一个具体实现CImp类。
java inject用法Java中的@Inject用法在Java中,@Inject是一个注解,用于标识依赖注入(Dependency Injection)的目标。
依赖注入是一种设计模式,通过将一个或多个依赖对象注入到目标对象中,实现对象之间的解耦和灵活性。
1. @Inject注解的使用@Inject注解可以用于构造函数、方法、字段和任何自定义注解中。
下面分别介绍在这些场景中的使用方法。
2. 在构造函数中使用@Inject@Inject注解可以用于构造函数,用于标识需要进行依赖注入的构造函数。
在依赖注入的过程中,被注入的依赖对象会自动通过构造函数来创建并传入目标对象。
javapublic class Example {private Dependency dependency;@Injectpublic Example(Dependency dependency) {this.dependency = dependency;}}上述代码中的Example类有一个Dependency类型的依赖对象,通过@Inject注解标识了构造函数,表示需要对该构造函数进行依赖注入。
3. 在方法中使用@Inject@Inject注解还可以用于方法,用于标识需要进行依赖注入的方法。
在依赖注入的过程中,被注入的依赖对象会自动通过方法调用来获取。
javapublic class Example {private Dependency dependency;@Injectpublic void setDependency(Dependency dependency) { this.dependency = dependency;}}上述代码中的Example类有一个Dependency类型的依赖对象,通过@Inject注解标识了setDependency方法,表示需要对该方法进行依赖注入。
4. 在字段中使用@Inject@Inject注解还可以用于字段,用于标识需要进行依赖注入的字段。
androidhilt用法Android Hilt 是一个用于依赖注入(Dependency Injection)的开源库,由 Google 提供和维护。
它是 Dagger2 的插件,旨在简化使用Dagger2 进行依赖注入的过程,提供更简洁、易于测试、易于维护的代码结构。
使用 Android Hilt,可以通过注解的形式轻松地将依赖注入到Android 组件(如 Activity、Fragment、Service)中,而无需手动处理依赖项的创建和管理。
下面将详细介绍 Android Hilt 的用法。
1.添加依赖首先,在项目的 build.gradle 文件中,添加以下依赖项:```groovydependencies```2. 添加 Hilt Application```kotlinclass MyApplication : Applicatio//...```注意,应该继承 Hilt 的 Application 类而不是原生的Application 类。
3.注入依赖项```kotlinlateinit var myDependency: MyDependency//...```在这个例子中,我们注入了一个名为 `MyDependency` 的依赖项。
当`MyActivity` 创建时,Hilt 会自动实例化并注入这个依赖项。
4.提供依赖项为了使 Hilt 能够创建和注入依赖项,我们需要告诉它如何提供这些依赖项。
我们可以通过两种方式来提供依赖项:```kotlin//...``````kotlinobject MyModulefun provideMyDependency(: MyDependencyreturn MyDependency}```5.绑定和限定符有时候,不同的类可能需要相同类型的依赖项,而我们又希望为它们提供不同的实现。
为了解决这个问题,我们可以使用 Hilt 的绑定和限定符功能。
控制反转(IoC)与依赖注入(DI)1.控制反转(Inversion of Control)与依赖注入(Dependency Injection) 控制反转即IoC (Inversion of Control),它把传统上由程序代码直接操控的对象的调用权交给容器,通过容器来实现对象组件的装配和管理。
所谓的“控制反转”概念就是对组件对象控制权的转移,从1.控制反转(Inversion of Control)与依赖注入(Dependency Injection)控制反转即IoC (Inversion of Control),它把传统上由程序代码直接操控的对象的调用权交给容器,通过容器来实现对象组件的装配和管理。
所谓的“控制反转”概念就是对组件对象控制权的转移,从程序代码本身转移到了外部容器。
IoC是一个很大的概念,可以用不同的方式来实现。
其主要实现方式有两种:<1>依赖查找(Dependency Lookup):容器提供回调接口和上下文环境给组件。
EJB和Apache Avalon 都使用这种方式。
<2>依赖注入(Dependency Injection):组件不做定位查询,只提供普通的Java方法让容器去决定依赖关系。
后者是时下最流行的IoC类型,其又有接口注入(Interface Injection),设值注入(Setter Injection)和构造子注入(Constructor Injection)三种方式。
图1 控制反转概念结构依赖注入之所以更流行是因为它是一种更可取的方式:让容器全权负责依赖查询,受管组件只需要暴露JavaBean的setter方法或者带参数的构造子或者接口,使容器可以在初始化时组装对象的依赖关系。
其与依赖查找方式相比,主要优势为:<1>查找定位操作与应用代码完全无关。
<2>不依赖于容器的API,可以很容易地在任何容器以外使用应用对象。
android hilt用法Android Hilt是谷歌推出的一种依赖注入(Dependency Injection)框架,用于简化Android应用中的依赖注入过程。
Hilt结合了Dagger框架和Android框架,提供了一种更简化的方式来管理和注入应用中的依赖关系。
使用Android Hilt可以通过以下步骤:1. 在项目的build.gradle文件中添加Hilt插件依赖:```groovyplugins {id 'com.android.application'id 'kotlin-android'id 'kotlin-kapt'id 'dagger.hilt.android.plugin'}```2. 在应用的Application类上添加@HiltAndroidApp注解,这个注解会自动生成一个继承自Application的子类,用于全局管理依赖注入:```kotlin@HiltAndroidAppclass MyApplication : Application() {// ...}```3. 在依赖需要注入的地方添加@AndroidEntryPoint注解,例如在Activity、Fragment、Service等类中:```kotlin@AndroidEntryPointclass MainActivity : AppCompatActivity() {@Injectlateinit var myDependency: MyDependency// ...}```4. 在需要注入的依赖类中添加@Singleton或@Scoped等Hilt 提供的注解,并使用@Inject注解依赖的构造函数或方法:```kotlin@Singletonclass MyDependency @Inject constructor() {// ...}```5. 在Module类中使用@InstallIn注解指定依赖的作用范围,例如@Singleton表示全局单例,@ActivityScoped表示在Activity范围内等:```kotlin@Module@InstallIn(SingletonComponent::class)class AppModule {// ...}```6. 在Module类中使用@Provides注解提供具体的依赖实例: ```kotlin@Module@InstallIn(SingletonComponent::class)class AppModule {@Provides@Singletonfun provideMyDependency(): MyDependency {return MyDependency()}}```以上就是使用Android Hilt的基本步骤和用法,Hilt会自动管理依赖的创建和注入过程,大大简化了在Android应用中的依赖注入步骤。
什么是Java的依赖注入有什么作用在Java 编程的世界里,依赖注入(Dependency Injection,简称DI)是一个重要的概念。
对于初学者来说,可能一开始会觉得有些抽象和难以理解,但其实它就像是给程序中的各个部分“送物资”,让它们能够顺利地完成自己的工作。
想象一下,我们正在构建一个复杂的系统,就像是建造一座大厦。
在这座大厦中,有各种各样的房间和设施,每个房间和设施都有自己特定的功能。
比如,有一个房间是负责处理数据的,另一个房间是负责显示界面的。
如果我们不使用依赖注入,那么每个房间都需要自己去获取它所需要的资源和工具。
这就好比每个房间都要自己去外面的市场购买所需的材料,不仅麻烦,而且容易出错。
而且,如果某个材料的获取方式发生了变化,那么每个房间都需要去修改自己获取材料的代码,这会导致大量的重复工作和代码的混乱。
那么,依赖注入是如何解决这个问题的呢?简单来说,依赖注入就是把对象所需要的外部资源,通过某种方式注入到对象内部,而不是让对象自己去获取。
举个例子,假设我们有一个`UserService` 类,它需要一个`UserRepository` 对象来访问数据库获取用户数据。
如果没有依赖注入,`UserService` 可能会这样创建`UserRepository` 对象:```javapublic class UserService {private UserRepository userRepository = new UserRepository();public void doSomething(){//使用 userRepository 进行操作}}```在上述代码中,`UserService` 自己创建了`UserRepository` 对象。
这样做的问题是,如果`UserRepository` 的创建方式发生了变化,或者我们想要使用不同的`UserRepository` 实现,那么就需要修改`UserService` 的代码。
.net 依赖注入详解依赖注入(Dependency Injection,简称DI)是一种设计模式,用于降低代码之间的耦合度。
在.NET中,依赖注入主要通过构造函数、属性或方法参数来实现。
下面我将为你详解.NET中的依赖注入。
1. 什么是依赖注入?依赖注入是一种编程范式,它允许将对象的创建和配置从应用程序代码中分离出来,从而降低对象之间的耦合度。
在依赖注入中,一个对象依赖于另一个对象(即被依赖的对象),而不是直接创建被依赖的对象。
相反,被依赖的对象由外部容器(如IoC容器)负责创建和注入。
2. 为什么使用依赖注入?使用依赖注入有以下几个优点:降低耦合度:通过将对象的创建和配置从应用程序代码中分离出来,可以降低对象之间的耦合度,使得代码更加模块化和可维护。
提高可测试性:由于对象的创建和配置由外部容器负责,因此可以更容易地对对象进行单元测试,而不需要关心对象的创建过程。
提高可扩展性:通过使用接口和抽象类,可以轻松地替换或添加新的实现,而不需要修改现有的代码。
3. 如何在.NET中使用依赖注入?在.NET中,可以使用以下几种方式实现依赖注入:构造函数注入:在类的构造函数中传入需要依赖的对象。
这是最常见的依赖注入方式。
csharppublic class MyClass{private readonly IMyDependency _dependency;public MyClass(IMyDependency dependency){_dependency = dependency;}}属性注入:通过属性的方式将需要依赖的对象注入到类中。
这种方式适用于只需要一个实例的情况。
csharppublic class MyClass{public IMyDependency MyDependency { get; set; }}方法参数注入:在方法的参数中传入需要依赖的对象。
这种方式适用于方法需要多个实例的情况。
一、Spring框架的优点都有什么?Spring是分层的架构,你可以选择使用你需要的层而不用管不需要的部分Spring是POJO编程,POJO编程使得可持续构建和可测试能力提高依赖注入和IoC使得JDBC操作简单化Spring是开源的免费的Spring使得对象管理集中化合简单化二、描述一下Spring中实现DI(Dependency Injection)的几种方式方式一:接口注入,在实际中得到了普遍应用,即使在IOC的概念尚未确立时,这样的方法也已经频繁出现在我们的代码中。
方式二:Type2 IoC: Setter injection对象创建之后,将被依赖对象通过set方法设置进去方式三:Type3 IoC: Constructor injection对象创建时,被依赖对象以构造方法参数的方式注入Spring的方式三、简述你对IoC(Inversion of Control)的理解一个类需要用到某个接口的方法,我们需要将类A和接口B的实现关联起来,最简单的方法是类A中创建一个对于接口B的实现C的实例,但这种方法显然两者的依赖(Dependency)太大了。
而IoC的方法是只在类A中定义好用于关联接口B的实现的方法,将类A,接口B和接口B的实现C放入IoC的容器(Container)中,通过一定的配置由容器(Container)来实现类A与接口B的实现C的关联。
四、Spring对多种ORM框架提供了很好的支持,简单描述在Spring中使用Hibernate的方法。
在context中定义DataSource,创建SessionFactoy,设置参数;DAO类继承HibernateDaoSupport,实现具体接口,从中获得HibernateTemplate进行具体操作。
在使用中如果遇到OpenSessionInView的问题,可以添加OpenSessionInViewFilter或OpenSessionInViewInterceptor五、请介绍一下Spring的事务管理spring提供了几个关于事务处理的类:TransactionDefinition //事务属性定义TranscationStatus //代表了当前的事务,可以提交,回滚。
依赖注入怎样实现有几种方式依赖注入(Dependency Injection,简称DI)是指通过外部将依赖对象传递给一个类,而不是在类内部创建依赖对象的方式。
它是面向对象编程中的一种设计模式,旨在降低类与类之间的耦合度,提高代码的可测试性和可维护性。
依赖注入有多种实现方式,下面将详细介绍其中的几种方式。
1. 构造函数注入(Constructor Injection):构造函数注入是最常见和推荐的依赖注入方式之一、通过在类的构造函数参数中声明需要依赖的对象,然后在类的实例化过程中将依赖对象传递给构造函数。
这种方式可确保依赖对象在类的创建时就被注入,并且在类的生命周期中都存在。
2. Setter方法注入(Setter Injection):Setter方法注入是另一种常见的依赖注入方式。
通过在类中定义与依赖对象对应的setter方法,在使用类的时候通过调用setter方法将依赖对象注入。
这种方式相较于构造函数注入更加灵活,可以在任何时候替换依赖对象,并且不需要修改类的构造函数。
3. 接口注入(Interface Injection):接口注入是一种相对较少使用的依赖注入方式。
它要求类实现一个特定的接口,在调用类的方法时,通过接口的方法将依赖对象注入。
这种方式相较于构造函数注入和setter方法注入更为灵活,但也增加了代码的复杂度和维护成本。
4. 注解注入(Annotation Injection):注解注入是一种基于注解的依赖注入方式。
通过在类或者类的成员上添加特定的注解来标识依赖对象,然后在运行时使用注解处理器来解析注解,并将依赖对象注入到相应的位置。
注解注入方式相较于前面几种方式更加灵活,对类的改动要求较小,但需要依赖于特定的注解框架。
除了上述几种依赖注入方式,还有一些其他的实现方式,如上下文注入、工厂方法注入等。
不同的依赖注入方式适用于不同的场景,选择合适的方式需要结合具体的业务需求和技术架构来决定。
理解Spring中的依赖注⼊和控制反转学习过Spring框架的⼈⼀定都会听过Spring的IoC(控制反转) 、DI(依赖注⼊)这两个概念,对于初学Spring的⼈来说,总觉得IoC 、DI这两个概念是模糊不清的,是很难理解的,今天和⼤家分享⽹上的⼀些技术⼤⽜们对Spring框架的IOC的理解以及谈谈我对Spring Ioc的理解。
IoC是什么Ioc—InversionofControl,即“控制反转”,不是什么技术,⽽是⼀种设计思想。
在Java开发中,Ioc意味着将你设计好的对象交给容器控制,⽽不是传统的在你的对象内部直接控制。
IoC,这是spring的核⼼,贯穿始终。
所谓IoC,对于spring框架来说,就是由spring来负责控制对象的⽣命周期和对象间的关系。
这是什么意思呢,举个简单的例⼦,我们是如何找⼥朋友的?常见的情况是,我们到处去看哪⾥有长得漂亮⾝材⼜好的mm,然后打听她们的兴趣爱好、qq号、电话号、ip号、iq号………,想办法认识她们,投其所好送其所要,然后嘿嘿……这个过程是复杂深奥的,我们必须⾃⼰设计和⾯对每个环节。
传统的程序开发也是如此,在⼀个对象中,如果要使⽤另外的对象,就必须得到它(⾃⼰new⼀个,或者从JNDI中查询⼀个),使⽤完之后还要将对象销毁(⽐如Connection等),对象始终会和其他的接⼝或类藕合起来。
如何理解好Ioc呢?理解好Ioc的关键是要明确“谁控制谁,控制什么,为何是反转(有反转就应该有正转了),哪些⽅⾯反转了”,那我们来深⼊分析⼀下:●谁控制谁,控制什么:传统JavaSE程序设计,我们直接在对象内部通过new进⾏创建对象,是程序主动去创建依赖对象;⽽IoC是有专门⼀个容器来创建这些对象,即由Ioc容器来控制对象的创建;谁控制谁?当然是IoC容器控制了对象;控制什么?那就是主要控制了外部资源获取(不只是对象包括⽐如⽂件等)。
●为何是反转,哪些⽅⾯反转了:有反转就有正转,传统应⽤程序是由我们⾃⼰在对象中主动控制去直接获取依赖对象,也就是正转;⽽反转则是由容器来帮忙创建及注⼊依赖对象;为何是反转?因为由容器帮我们查找及注⼊依赖对象,对象只是被动的接受依赖对象,所以是反转;哪些⽅⾯反转了?依赖对象的获取被反转了。
PRISM 4.0 TRAINING KIT Hands-On LabLab version: 1.0.0Last updated: 4/16/2013Contents OVERVIEW (3)EXERCISE 1 – CREATING AND REGISTERING THE SERVICE (4)Task 1 – Creating a Service (4)Task 2 –Registering the Service (4)EXERCISE 2 – CONSUMING A SERVICE (6)Task 1- Consuming the UIMessagesService (6)Task 2- Decoupling Modules (8)OverviewApplications based on the Prism Library are composite applications that potentially consist of many loosely coupled types and services. They need to interact to contribute content and receive notifications based on user actions. Because they are loosely coupled, they need a way to interact and communicate with one another to deliver the required business functionality.To tie together these various pieces, applications based on the Prism Library rely on a dependency injection container. Dependency injection containers reduce the dependency coupling between objects by providing a facility to instantiate instances of classes and manage their lifetime based on the configuration of the container. During the objects creation, the container injects any dependencies that the object requires into it. If those dependencies have not yet been created, the container creates and resolves their dependencies first. In some cases, the container itself is resolved as a dependency. For example, when using the Unity Application Block (Unity) as the container, modules have the container injected, so they can register their views and services with that container.Exercise 1 – Creating and registering the serviceIn this exercise you will learn how to create a service and register it in a dependency injection container, in this case Unity.Task 1 – Creating a Service1.Open the Prism.Workshop.slnsolution located in the \Dependency Injection\Exercise 1\Begin\folder.2.Add a new module project to the solution, named Workshop.UIMessagesModule.For moreinformation see the Modularity Hands-on-Lab located in \Modularity\Modularity-HOL.docx.Solution Structure after adding Workshop.UIMessagesModule module3.Create the UIMessagesService. Add a new UIMessagesService class to the project using thefollowing code.C#using System.Windows;namespace Workshop.UIMessagesModule.Services{public class UIMessagesService{public void ShowMessage(string message){// Show a messageMessageBox.Show(message);}}}Task 2 –Registering the Service1.Add the following references to the Workshop.UIMessagesModule project:◦Unity assembly, located at \Lib\Microsoft.Practices.Unity.Silverlight.dll.◦Prism assembly, located at \Lib\ Microsoft.Practices.Prism.dll.2.Add the following using statements to the UIMessagesModule class.e Property Injection to get an instance of the container. Add the following code to theUIMessagesModule class.C#/// <summary>/// DI Container./// Unity will inject an instance of the container. IUnityContainer isregistered in the container in the bootstrapper of the application./// </summary>[Dependency]public IUnityContainer Container { get; set; }4.Register the service in the container as a singleton when the module is initialized. To do this,use the Register method. In the UIMessageModule implement the initialize method asillustrated in the following code.C#public void Initialize(){// register the UIMessagesService as singletonthis.Container.RegisterType<UIMessagesService>(newContainerControlledLifetimeManager());}5.(Optional) Unity also allows the use of Constructor Injection for achieving the same results. Toget an instance of the Unity Container using Constructor Injection, remove the previous method and property, and add the following code in the UIMessagesModule class.private readonlyIUnityContainer container;/// <summary>/// Constructor for the UIMessagesModule./// Unity will inject an instance of the container. IUnityContainer isregistered in the container in the bootstrapper of the application./// </summary>/// <param name="container"></param>public UIMessagesModule(IUnityContainer container){this.container = container;}public void Initialize(){// register the UIMessagesService as singletonthis.container.RegisterType<UIMessagesService>(newContainerControlledLifetimeManager());}Exercise 2 – Consuming a ServiceIn this exercise, you will learn how to obtain a reference to a registered service and use it in your application.Task 1- Consuming the UIMessagesService1.Open the solution located in \Dependency Injection\Exercise 2\Begin\Prism.Workshop.sln2.In the Workshop.ModuleA, add a reference to the Workshop.UIMessagesService project. Thisreference will be removed in the last task.3.Add the following references to the Workshop.UIMessagesModule project:◦Unity assembly located at \Lib\Microsoft.Practices.Unity.Silverlight.dll.◦Prism assembly located at \Lib\ Microsoft.Practices.Prism.dll.4.In the ModuleA class add the following usingstatements:C#using System.Windows.Controls;using Microsoft.Practices.Prism.Modularity;using Microsoft.Practices.Prism.Regions;using Workshop.Infrastructure;e Constructor Injection to get an instance of the container. In the ModuleA class, add thefollowing code:C#public ModuleA(IRegionManagerregionManager, IUnityContainer container){this.regionManager = regionManager;}e the container to obtain an instance of the UIMessageService. To do this,use the Registermethod as follows:C#private readonlyUIMessagesServiceuiMessagesService;public ModuleA(IRegionManagerregionManager, IUnityContainer container){this.regionManager = regionManager;// Get an instance of the UIMessageServicethis.uiMessagesService = container.Resolve<UIMessagesService>();}7.To use the UIMessageService, add the following lines of code to the initialize the method.8.(Optional) Unity allows you to inject registered services directly, instead of using the container.To get an instance of the UIMessagesService using Constructor Injection replace the ModuleA constructor with the following code.C#/// <summary>/// Constructor for Module A/// </summary>/// <param name="regionManager">Unity will inject an instance of the container.IRegionManager is registered in the container in the bootstrapper of theapplication.</param>/// <param name="uiMessagesService">Unity will inject an instance of thecontainer. The UIMessagesService is registered by the UIMessagesModule.</param> public ModuleA(IRegionManagerregionManager, UIMessagesServiceuiMessagesService){this.regionManager = regionManager;// Get an instance of the UIMessageServicethis.uiMessagesService = uiMessagesService;}Message Shown using the UIMessageServiceTask 2- Decoupling ModulesModuleA module has a reference to UIMessageModule module. To decouple these modules perform the following steps.1.Create a new infrastructure project named Workshop.InfrastructureSolution structure adding an infrastructure project.2.Add a new interface named IUIMessageService using the following code.namespace Workshop.Infrastructure{public interface IUIMessagesService{void ShowMessage(string message);}}3.In the Workshop.UIMessageService project, add a reference to the Workshop.Infrastructureproject.4.Make the UIMessageService implement the IUIMessageService interface. In theUIMessageService class add bold code:C#using System.Windows;using Workshop.Infrastructure;namespace Workshop.UIMessagesModule.Services{public class UIMessagesService: IUIMessagesService{public void ShowMessage(string message){// Show a messageMessageBox.Show(message);}}}5.Register the service using the interface. On the Initialize method of theUIMessagesModule replace the service registration line as follows.6.In the Workshop.ModuleA project, add a reference to the Workshop.Infrastructure project.7.Make the ModuleA have a dependency on the service interface instead of the concrete class.To do this, change the theModuleA constructor as follows:using Microsoft.Practices.Prism.Regions;using Workshop.UIMessagesModule.Services;using Workshop.Infrastructure;namespace Workshop.ModuleA{public class ModuleA : IModule{private readonlyIRegionManagerregionManager;private readonlyIUIMessagesServiceuiMessagesService;/// <summary>/// Constructor for Module A/// </summary>/// <param name="regionManager">Unity will inject an instance of the container. IRegionManager is registered in the container in the bootstrapper of the application.</param>/// <param name="uiMessagesService">Unity will inject an instance of the container. The UIMessagesService is registered by theUIMessagesModule.</param>public ModuleA(IRegionManagerregionManage r,IUIMessagesServiceuiMessagesService){this.regionManager = regionManager;// Get an instance of the UIMessageServicethis.uiMessagesService = uiMessagesService;}…}8.Make ModuleA have a dependency on the UIMessagesService, by adding the following code tothe ModuleCatalog.xaml.XAML<Modularity:ModuleInfo Ref="Workshop.ModuleA.xap" ModuleName="ModuleA"ModuleType="Workshop.ModuleA.ModuleA, Workshop.ModuleA, Version=1.0.0.0" > <Modularity:ModuleInfo.DependsOn><sys:String>UIMessagesModule</sys:String></Modularity:ModuleInfo.DependsOn></Modularity:ModuleInfo>9.On the Workshop.ModuleA project, remove the reference to Workshop.UIMesssagesService.。