views:

29

answers:

1

I have a problem that I can't seem to resolve. I'm sure it's simple, but I honestly cannot figure it out. I have a simple type that I reuse on multiple controllers. The problem that I'm faced with is that it's the SAME type with different configurations. I need different instances of this type to be used on different controllers. I've dumbed it down as simple as I can think to try and explain it...

Consider the following:

interface ISimpleType 
{ 
    string Value { get; } 
}

class ConcreteType : ISimpleType 
{
    private readonly string _value;

    public ConcreteType(string value) 
    { 
        _value = value; 
    } 

    public string Value { get { return _value; } }

}

In my web.config, I've defined two instances of the same type with their own id's:

<component id="concrete.one" 
    service="MyApp.ISimpleType, MyApp" 
    type="MyApp.ConcreteType, MyApp">
  <parameters>
    <value>ONE</value>
  </parameters>
</component>

<component id="concrete.two" 
    service="MyApp.ISimpleType, MyApp" 
    type="MyApp.ConcreteType, MyApp">
  <parameters>
    <value>TWO</value>
  </parameters>
</component>

Now on my MVC controller, I want to be able to accept an ISimpleType interface as the parameter, but have it use the correct ConcreteType instance depending on the parameter name:

public class FirstController : BaseController 
{ 
    public FirstController(ISimpleType firstType) : base(firstType) { ... }
}

public class SecondController : BaseController 
{
    public SecondController(ISimpleType secondType) : base(secondType) { ... }
}

I'd like to register all instances of ISimpleType named "firstType" to use the Windsor component "concrete.one", and all ISimpleTypes named "secondType" to use the Windsor component "concrete.two".

The only alternative that I can see is to NOT pass this as a parameter and simply initialize it in the constructor manually.

public FirstController()
{
    base.SimpleType = WindsorServiceFactory.Create<ISimpleType>("concrete.one");
}

Is there a better way?

Thanks in advance!

+1  A: 

This is easily done via service overrides.

Krzysztof Koźmic