簡體   English   中英

使用autofac注冊多個dbcontext,其中構建器不可用於在注冊后解析類

[英]Register multiple dbcontext using autofac where builder is not available to resolve the classes after registration

我有以下場景,依賴注入發生在下面給出的類定義之外,因此我無法訪問類中的構建器對象。 問題是DbContext被映射到兩個類,因為我需要使用兩個不同的數據庫。 為了創建SomeClass,需要實例化MyRepository和MyOtherRepository,但要做到這一點需要2個不同的DbContexts。 事情就失敗了,因為Autofac只采用DbContext映射到MyOtherContext的最后一個映射。 非常感謝您解決此問題的任何幫助,謝謝! 如果需要更多信息,請告訴我。

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();
}

下面的類定義

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
    }
}

試試這個答案 基本上您正在尋找Autofac的命名和鍵控服務功能。 我更喜歡用屬性方法解決

所以如果你注冊這樣的上下文:

var builder = new ContainerBuilder();                
        builder
            .RegisterType<MyContext>()
            .As<DbContext>()
            .WithMetadata("EFContext", "My");
        builder
            .RegisterType<MyRepository>()
            .AsImplementedInterfaces();

        builder
            .RegisterType<MyOtherContext>()
            .As<DbContext>()
            .WithMetadata("EFContext", "Other");
        builder
            .RegisterType<MyOtherRepository>()
            .AsImplementedInterfaces();

您應該能夠將它們與(非強類型)元數據一起使用:

public class SomeClass
{
    private readonly IMyRepository _myRepository;
    private readonly IMyOtherRepository _myOtherRepository;

    public SomeClass(IMyRepository myRepository, IMyOtherRepository myOtherRepository)
    {
        _myRepository = myRepository;
        _myOtherRepository = myOtherRepository;
    }

    public string SayHello()
    {
        return $"Hello from {_myRepository.ContextName()}  and {_myOtherRepository.ContextName()}";
    }
}

public interface IMyOtherRepository
{
    string ContextName();
}

public interface IMyRepository
{
    string ContextName();
}

public class MyContext : DbContext
{
    // Code
}

public class MyOtherContext : DbContext
{
    // Code
}

public class MyRepository : IMyRepository
{
    private readonly IEnumerable<Meta<DbContext>> _context;

    public MyRepository(IEnumerable<Meta<DbContext>> context)
    {
        _context = context;
        // Code
    }

    public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("My")).Value.GetType().Name;
}

public class MyOtherRepository : IMyOtherRepository
{
    private readonly IEnumerable<Meta<DbContext>> _context;

    public MyOtherRepository(IEnumerable<Meta<DbContext>> context)
    {
        this._context = context;
        // Code
    }

    public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("Other")).Value.GetType().Name;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM