C# – How to use Castle Windsor – Fluent Interface to register a generic interfaces

ccastlecastle-windsorfluent-interfaceioc-container

Castle Windsor just came out with a Fluent interface for registering components as an alternative to using XML in a config file. How do I use this Fluent interface to register a Generic interface?

To illustrate, I have:

public interface IFoo<T,U>
{    
  public T IToo();   
  public U ISeeU(); 
}

Which is implemented by some class called Foo. Now, if I want to register this, I do something like…

var _container = new WindsorContainer();
_container.Register(...);

How do I proceed in registering with this? The procedure for doing the non-generic interface does not work.

Best Answer

Someting like this?

   container.Register(AllTypes.FromAssemblyContaining<YourClass>()
        .BasedOn(typeof(IFoo<,>))
        .WithService.AllInterfaces()
        .Configure(c => c.LifeStyle.Transient));

interface

 public interface IFoo<T, U>
{
    T IToo();
    U ISeeU();
}
Related Topic