Registering concrete types that implement common interfaces with Autofac
Consider the following structure as a registration object with Autofac 3.0.0:
class Something
{
public int Result { get; set; }
}
class SomethingGood : Something
{
private int _good;
public int GoodResult {
get { return _good + Result; }
set { _good = value; }
}
}
interface IDo<in T> where T : Something
{
int Calculate( T input );
}
class MakeSomethingGood : IDo<SomethingGood>
{
public int Calculate( SomethingGood input ) {
return input.GoodResult;
}
}
class ControlSomething
{
private readonly IDo<Something> _doer;
public ControlSomething( IDo<Something> doer ) {
_doer = doer;
}
public void Show() {
Console.WriteLine( _doer.Calculate( new Something { Result = 5 } ) );
}
}
I'm trying to register a specific type of MakeSomethingGood and then resolve it using a contravariant interface.
var builder = new ContainerBuilder();
builder.Register( c => new MakeSomethingGood() ).As<IDo<SomethingGood>>();
builder.Register( c => new ControlSomething( c.Resolve<IDo<Something>>() ) ).AsSelf();
var container = builder.Build();
var controller = container.Resolve<ControlSomething>();
... and Resolve
fails because no components were found forIDo<Something>
What am I doing wrong?
thank
source to share
You are registering IDo<SomethingGood>
and trying to resolve IDo<Something>
. How is this ever supposed to work? For this to work, IDo<T>
it should be defined as covariant: IDo<out T>
.
Since it is IDo<in T>
defined as contravariant (using a keyword in
), you cannot simply assign IDo<SomethingGood>
to IDo<Something>
. This won't compile in C #:
IDo<SomethingGood> good = new MakeSomethingGood();
// Won't compile
IDo<Something> some = good;
And that's why Autofac can't solve it, even with ContravariantRegistrationSource
.
source to share