views:

84

answers:

0

Hi,

I'm new to Castle Windsor, so I'm not sure what the best practise is in cases like the following: I have a service interface (ILoader), that is implemented by multiple Loader classes. I also have an implementation of ILoader that's a decorator:

public interface ILoader { }
public class LoaderImpl0 : ILoader { }
public class LoaderImpl1 : ILoader { }
public class CachedLoader : ILoader
{
    public CachedLoader(ILoader inner) { }
}

For each LoaderImpl... class, I want to register a CachedLoader in my container, that decorates a LoaderImpl... instance.

Here's my first rubbish attempt:

container.Register
(
    Component.For<LoaderImpl0>().Named("loader0"),
    Component.For<LoaderImpl1>().Named("loader1"),
    Component
    .For<ILoader>( )
    .ImplementedBy<CachedLoader>( )
    .ServiceOverrides( ServiceOverride.ForKey( "inner" ).Eq( "loader0" ) )
    .Named("cachedLoader0"),
    Component
    .For<ILoader>()
    .ImplementedBy<CachedLoader>()
    .ServiceOverrides(ServiceOverride.ForKey("inner").Eq("loader1"))
    .Named("cachedLoader1")
);

This just seems wrong, on 2 counts:

  • container.ResolveAll<ILoader>() returns all 4 registered loaders (even though I haven't registered the LoaderImpl... classes with the ILoader service).
  • It seems like a very clumsy way to define what is a very common nested relationship - I don't really care about the LoaderImpl... classes, they should really be anonymous.

Can someone enlighten me where I'm going wrong?