什么是" ForwardedTypes"在温莎城堡组件注册的情况下? [英] What are "ForwardedTypes" in the context of Castle Windsor component registration?

查看:138
本文介绍了什么是" ForwardedTypes"在温莎城堡组件注册的情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为题说,真的!他们会怎么做?

As the subject says, really! What do they do?

推荐答案

转发类型让你有一个单一的实现来实现多个服务,对于一个具体的例子说我们有两个接口,用于与某种树节点工作:

Forwarded types allow you to have more than one service implemented by a single implementation, for a concrete example say we have two interfaces for working with tree nodes of some sort:

public interface INodeAlterationProvider { ... }
public interface IChildNodeListProvider { ... }

和各种组件需要在一个或两个这些接口的依赖。然而,在实施每一个这些接口的你会发现,他们是一个很大的共享功能,并希望将实现合并成一个单一类的一些其他功能一起​​说这样的:

And various components take a dependency on one or both of those interfaces. However in implementing each of those interfaces you discover that their is a lot of shared functionality and want to merge the implementations into a single class along with some other features say like:

public class NodeFactory : INodeAlterationProvider, IChildNodeListProvider { ... }

你可以注册NodeFactory,一两个实例,因为他们实现每个服务:

You could register two instances of NodeFactory, one for each service they implement:

container.Register(Component.For<INodeAlterationProvider>().ImplementedBy<NodeFactory>());
container.Register(Component.For<IChildNodeListProvider>().ImplementedBy<NodeFactory>());



但是,这可能意味着NodeFactory两个单实例存在 - 不理想,尤其是如果它是昂贵的构建 - 并且可以使调试等更难理解,尤其是如果有正在实施两个以上的接口

But this could potentially mean two singleton instances of NodeFactory exist - not ideal, especially if it's costly to construct - and can make debugging etc. harder to understand, especially if there was more than two interfaces being implemented.

这就是转发类型的一步,让您在多个转发相同的实施服务,这里就是这样做的例子:

This is where forwarded types step in, allowing you to forward multiple services to the same implementation, here's an example of doing that:

container.Register(Component.For<INodeAlterationProvider>().Forward<IChildNodeListProvider>().ImplementedBy<NodeFactory>());

请注意:这里显示的组件注册代码只能在主干可用

Note: the component registration code shown here is only available on trunk.

这篇关于什么是&QUOT; ForwardedTypes&QUOT;在温莎城堡组件注册的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆