首页 > 代码库 > Catel帮助手册-Catel.Core:(5)依赖注入(ServiceLocator and TypeFactory)

Catel帮助手册-Catel.Core:(5)依赖注入(ServiceLocator and TypeFactory)

1,简介

在Catel2.0之前,IOC容器内部使用的是Unity,然而,这就强制所有的用户在他的应用程序中使用Unity作为IOC容器,也需要这样划分库,从2.0以后,一个不同的技术被使用了,这个允许开发者可以使用他们自己悬着的IOC容器技术。

1.1在Ioc中不同的组件

在Catel中有许多不同的组件他们对于Ioc是非常重要的。

ServiceLocator
该组件用于注册所有的类型,这个事真正的Ioc容器

TypeFactory
用于创建类型的组件,使用IServiceLocator来获取需要视力的类型

DependencyResolver
IServiceLocator的轻量级实现,这个并不暴露注册方法,只是允许resolve类型

 

1.2 为任意对象获取组件

在每个对象中,可以使用默认属性来获取每个组件的实例,这在不同的scoping被使用时会造成问题,要确定为你工作的正确对象,需要使用如下的扩展代码。

using Catel.IoC; // Contains ObjectExtensions which allow use of below extension methods public class MyService{    public void SomeMethod()    {        // If you need to create a type with the current scope type factory        var typeFactory = this.GetTypeFactory();         // If you need to register a type with the current scope service locator        var serviceLocator = this.GetServiceLocator();         // If you need to resolve a type with the current scope and the type is not injected via dependency injection        var dependencyResolver = this.GetDependencyResolver();    }}

 

2.IOC组件的说明

2.1 ServiceLocator说明

ServiceLocator在Catel中作为服务容器。

内部使用TypeFactory作为服务的初始化。

Catel uses it‘s own ServiceLocator implementing the IServiceLocator to gather all services required by Catel.

例如, IPleaseWaitService和IUIVisualizerService为默认服务,在通常情况下,当第一个view model在初始之前,Catel已经注册了所有的立即可以用的默认服务,这个可以让开发者可以在view model被初始之前注册自己的实现的服务(例如,应用程序启动服务)。

    ServiceLocator可以被实例化,但是Catel会实例化一个进程,在同一个AppDomain中的所有对象之间共享,ServiceLocator可以通过ServiceLocator获取。

 注意:如果要知道类型如何被初始化和依赖注入的,可以查看后面的TypeFactory的说明。

2.2 注册一个类型

使用如下的代码注册一个指定的类型到ServiceLocator

 

ServiceLocator.Default.RegisterInstance<IPleaseWaitService>(pleaseWaitService);

2.3 注册一个晚绑定类型

ServiceLocator.Default.RegisterType<IPleaseWaitService>(x => new PleaseWaitService());

 

2.4 注册一个类型的实例

当对象第一次被resolve的时候,Catel使用TypeFactory或者Activator.CreateInstance来创建实例,但是有时候,一个服务的构造函数需要参数或者需要很长的时间来构造,在这种情况下,推荐人工来创建类型和注册实例

var pleaseWaitService = new PleaseWaitService();ServiceLocator.Default.RegisterInstance<IPleaseWaitService>(pleaseWaitService);

 

2.5 通过MissType事件来注册一个类型

当一个类型没有注册到ServiceLocato或者其他任何容器中,ServiceLocator会给一用户痛一个最后的注册信息。记录履历的事件是非常有用的(开发者可以通过履历知道那什么类型从IOC容器中丢失了),或者他能决定服务在后期必须被使用,

The ServiceLocator gives the end-developer a last-resort chance to register a type when it is not registered in the ServiceLocator or any of the external containers. This event is very useful for logging (then the developer in the log knows exactly what type is missing from the IoC container) or it can be used to determine at runtime in a very late stage what implementation of the service must be used. To register a type via the event, subscribe to the event and then use the following code:

private void OnMissingType(object sender, MissingTypeEventArgs e)

{

if (e.InterfaceType == typeof(IPleaseWaitService))

{

// Register an instance

e.ImplementingInstance = new PleaseWaitService();

// Or a type

e.ImplementingType = typeof(PleaseWaitService);

}

}

If both the ImplementingInstance and ImplementingType are filled, the ImplementingIntance will be used.

Resolving a type

To retrieve the implementation of a service, use the following code:

var pleaseWaitService = ServiceLocator.Default.ResolveType<IPleaseWaitService>();

Catel帮助手册-Catel.Core:(5)依赖注入(ServiceLocator and TypeFactory)