views:

572

answers:

1

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

+6  A: 

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.

Bittercoder
Thanks - that explains what it is. However, based on that, it smells like a shortcut to not doing a "proper" refactoring to merge the two services...?
Peter Mounce
Could well be a smell in the example I provided, I did come up with it off the cuff... a more likely example might be something like an IExtendedLoggerFactory which extends ILoggerFactory and is implemented by a single class which satisfies both interfaces.
Bittercoder
Ah; yeah, ok, I can see that. I was actually at Mike Hadlow's talk on IoC/Windsor last night and found out some things about trunk there, too :)
Peter Mounce