Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

c# - What are "ForwardedTypes" in the context of Castle Windsor component registration?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 { ... }

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>());

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...