Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 726
Peter Mounce Avatar asked Oct 13 '08 11:10

Peter Mounce


1 Answers

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.

like image 149
Bittercoder Avatar answered Nov 10 '22 15:11

Bittercoder