I have the following scenario, dependency injection happen outside of the class definitions given below, hence I don't have access to the builder object inside the classes. Problem is DbContext is mapped to two classes because I have two different databases I need to work with. In Order to create a SomeClass, need to instantiate MyRepository and MyOtherRepository, but to do that need 2 different DbContexts. That's where things fail as Autofac only takes the last mapping where the DbContext is mapped to MyOtherContext. Any help to resolve this problem is really appreciated, Thank you! Please let me know if more info is needed.
public class DependencyResolver : IDependencyResolver
{
builder.RegisterType<MyContext>().As<DbContext>().InstancePerLifetimeScope();
builder.RegisterType<MyOtherContext>().As<DbContext>().InstancePerLifetimeScope();
builder.RegisterType<RepositoryManager.MyRepository>().As<Types.Repository.IMyRepository>().InstancePerDependency();
builder.RegisterType<RepositoryManager.MyOtherRepository>().As<Types.Repository.IMyOtherRepository>().InstancePerDependency();
}
Class Definitions below
public class SomeClass
{
public SomeClass(IMyRepository myRepository, IMyOtherRepository myOtherRepository)
{
// Code
}
}
public class MyContext : DbContext
{
// Code
}
public class MyOtherContext : DbContext
{
// Code
}
public class MyRepository : IRepository
{
public MyRepository (DbContext context) : base(context)
{
// Code
}
}
public class MyOtherRepository : IRepository
{
public MyOtherRepository (DbContext context) : base(context)
{
// Code
}
}