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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…