tags:

views:

98

answers:

1

I am currently attempting to remove a number of .Resolve(s) in our code. I was moving along fine until I ran into a named registration and I have not been able to get Autofac resolve using the name. What am I missing to get the named registration injected into the constructor.

Registration

builder.RegisterType<CentralDataSessionFactory>().Named<IDataSessionFactory>("central").SingleInstance();
builder.RegisterType<ClientDataSessionFactory>().Named<IDataSessionFactory>("client").SingleInstance();
builder.RegisterType<CentralUnitOfWork>().As<ICentralUnitOfWork>().InstancePerDependency();
builder.RegisterType<ClientUnitOfWork>().As<IClientUnitOfWork>().InstancePerDependency();

Current class

public class CentralUnitOfWork : UnitOfWork, ICentralUnitOfWork
{
    protected override ISession CreateSession()
    {
        return IoCHelper.Resolve<IDataSessionFactory>("central").CreateSession();
    }
}

Would Like to Have

public class CentralUnitOfWork : UnitOfWork, ICentralUnitOfWork
{
    private readonly IDataSessionFactory _factory;
    public CentralUnitOfWork(IDataSessionFactory factory)
    {
        _factory = factory;
    }

    protected override ISession CreateSession()
    {
        return _factory.CreateSession();
    }
}
+1  A: 

Change the registration to do the resolve manually:

builder.Register(c => new CentralUnitOfWork(c.Resolve<IDataSessionFactory>("central")))
   .As<ICentralUnitOfWork>()
   .InstancePerDependency();
Peter Lillevold