1

I am using Castle Windsor 3.0 in .net 4.0 (c#) and I have the following situation:

namespace Root
{
    public interface IGenericService<T>
    {        
    }
    public class GenericService<T> : IGenericService<T>
    {        
    }
}    
namespace Root.A
{
    public class Something
    {        
    }
    //... Some other classes
}

What I want to do is have an implementation of IGenericService registered in the container for every class that is in the same namespace as "Something".

I can do it like this but I am wondering, is it possible using the Castle Windsor fluent configuration API?

private static void RegisterComponents(IWindsorContainer container)
{            
    // Get all the types in the same namespace as "Something"
    // I.e AllTypes.FromThisAssembly().InSameNamespaceAs<Something>()
    var type = typeof(Something);
    var types = type
        .Assembly
        .GetTypes()
        .Where(t => t.Namespace != null &&
                    t.Namespace.Equals(type.Namespace,
                                       StringComparison
                                           .InvariantCultureIgnoreCase));

    // For each of those types register a component GenericService<T>
    // with service IGenericService<T> where T is each type
    Type genericBaseType = typeof(GenericService<>);
    string assemblyName = genericBaseType.Assembly.GetName().FullName;
    string genericBaseName = genericBaseType.FullName;
    foreach (var t in types)
    {
        var ser = typeof(IGenericService<>);
        var serAssemblyName = ser.Assembly.GetName().FullName;
        string serviceFullName = string.Format(
               CultureInfo.InvariantCulture,
               "{0}[[{1}]], {2}",
               ser.FullName,
               t.AssemblyQualifiedName,
               serAssemblyName);
        var serviceType = Type.GetType(serviceFullName);


        string componentFullName = string.Format(
            CultureInfo.InvariantCulture,
            "{0}[[{1}]], {2}",
            genericBaseName,
            t.AssemblyQualifiedName,
            assemblyName);
        var componentType = Type.GetType(componentFullName);


        container.Register(Component
            .For(serviceType)
            .ImplementedBy(componentType)
            .LifestylePerWebRequest());
    }
}

2 Answers 2

0

As any other IoC container Castle Windsor registers types globally, which means if you register an implementation of say an interface, it has a scope for the whole application. You cannot limit it to a namespace.

However, there is a workaround for this situation, where you can register a named component, for example

container.Register(Component
    .For(serviceType)
    .Named("NamespaceA")     //<------HERE
    .ImplementedBy(componentType_NamespaceA) //one type
    .LifestylePerWebRequest());

container.Register(Component
    .For(serviceType)
    .Named("NamespaceB")     //<------HERE
    .ImplementedBy(componentType_NamespaceB)  //another type
    .LifestylePerWebRequest());

Now you can resolve the type that you need by the interface and name.

4
  • Thanks oleksii but I am not sure you have understood my question (I am sure this is my fault!). I understand what you are saying but I do want my implementations registered for the entire application. Say I have classes A,B and C what I want is the following globally registered components GenericService<A> : IGenericService<A>, GenericService<B> : IGenericService<B> and GenericService<C> : IGenericService<C>. This is what the RegisterComponents function does that I put at the end of the question. I only split the namespace to make it easy to get A, B and C. Does that make sense?
    – kmp
    Mar 6, 2012 at 9:15
  • To clarify, in your example the two components would be registered with the same service type but I want different service types.
    – kmp
    Mar 6, 2012 at 9:17
  • I am sure it is possible to do (but not sure if it will be flexible), you would need to locate the namespaces using reflections. However, I would use types from three namespaces and register them directly without using reflection. Because, say if you move types into different namespace and the implementation doesn't support this, it would break IoC registration, and as a consequence the whole app. I would recommend using something like: container.Register(Component.For(IGenericService<A>).ImplementedBy(GenericService<A>).LifestylePerWebRequest()); and then similar for other generic services.
    – oleksii
    Mar 6, 2012 at 10:30
  • Yeah, thanks, my example code does use reflection and works fine but I wanted to know if it was possible to do it in the windsor fluent API. I appreciate you pointing out the risks and I get what you're saying about doing it manually and how that would alleviate them but I am asking the question because just I want to know if it is possible (not necessarily if I should do it - incidentally I do have a very specific place to do this that is constrained enough and the job of manually doing all the registrations tedious, and risky if one is missed, enough that I think it is worth it).
    – kmp
    Mar 6, 2012 at 10:49
0

After a long while of searching I think the answer to this is that you can't and the code posted in the question is the only way to achieve this and, yes, it is something that is quite edge-casey and looking at the application holistically there may be a better design in most cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.