C# 统一层次构造函数注入

C# 统一层次构造函数注入,c#,unity-container,C#,Unity Container,我很难理解Unity是如何解析层次结构树下对象的引用的,以及如何注入相关的构造函数 目前,我的容器配置如下: container .RegisterType<IDataContextAsync, Zeus>("ZeusLive", new PerRequestLifetimeManager(), new InjectionConstructor("ZeusLive")) .RegisterType<IDataContextAsync, Z

我很难理解Unity是如何解析层次结构树下对象的引用的,以及如何注入相关的构造函数

目前,我的容器配置如下:

    container
        .RegisterType<IDataContextAsync, Zeus>("ZeusLive", new PerRequestLifetimeManager(), new InjectionConstructor("ZeusLive"))
        .RegisterType<IDataContextAsync, Zeus>("Zeus", new PerRequestLifetimeManager(), new InjectionConstructor("Zeus"))
        .RegisterType<IUnitOfWorkAsync, UnitOfWork>("ZeusUnitOfWork", new InjectionConstructor(new ResolvedParameter<IDataContextAsync>("Zeus")))
        .RegisterType<IRepositoryAsync<cd_Job>, Repository<cd_Job>>(new InjectionConstructor(new ResolvedParameter<IUnitOfWorkAsync>("ZeusUnitOfWork"), new ResolvedParameter<IDataContextAsync>("Zeus")));
其中存储库类的构造函数如下所示:

public Repository(IDataContextAsync context, IUnitOfWorkAsync unitOfWork)
{
    _context = context;
    _unitOfWork = unitOfWork;

    // Temporarily for FakeDbContext, Unit Test and Fakes
    var dbContext = context as DbContext;

    if (dbContext != null)
    {
        _dbSet = dbContext.Set<TEntity>();
    }
    else
    {
        var fakeContext = context as FakeDbContext;

        if (fakeContext != null)
        {
            _dbSet = fakeContext.Set<TEntity>();
        }
    }
}
公共存储库(IDataContextAsync上下文,IUnitOfWorkAsync unitOfWork)
{
_上下文=上下文;
_unitOfWork=unitOfWork;
//临时用于FakeDbContext、单元测试和Fakes
var dbContext=上下文作为dbContext;
if(dbContext!=null)
{
_dbSet=dbContext.Set();
}
其他的
{
var fakeContext=作为FakeDbContext的上下文;
if(fakeContext!=null)
{
_dbSet=fakeContext.Set();
}
}
}

我怀疑我的问题是我不完全理解分层引用是如何解决的,我的错误是显而易见的,但我仍在学习,因此希望有一些指针

您需要按照构造函数中定义的顺序为
注入构造函数
提供参数

构造函数的定义如下:

public Repository(IDataContextAsync context, IUnitOfWorkAsync unitOfWork)
{
}
.RegisterType<IRepositoryAsync<cd_Job>, Repository<cd_Job>>(
    new InjectionConstructor(
        new ResolvedParameter<IDataContextAsync>("Zeus"),
        new ResolvedParameter<IUnitOfWorkAsync>("ZeusUnitOfWork")))
所以你需要像这样注册它:

public Repository(IDataContextAsync context, IUnitOfWorkAsync unitOfWork)
{
}
.RegisterType<IRepositoryAsync<cd_Job>, Repository<cd_Job>>(
    new InjectionConstructor(
        new ResolvedParameter<IDataContextAsync>("Zeus"),
        new ResolvedParameter<IUnitOfWorkAsync>("ZeusUnitOfWork")))
Unity足够聪明,可以实现
存储库
有一个构造函数,它接受
IDataContextAsync
IUnitOfWorkAsync
,并为它们提供实例,因为他知道如何解决它们。这些接口的默认注册将分别解析为类
Zeus
(使用构造函数参数Zeus,可能是连接字符串?)和
UnitOfWork
的实例


希望有帮助

谢谢,现在一切正常-我可以发誓我已经试过了:)你的额外信息也非常有用。非常感谢
container
    .RegisterType<IDataContextAsync, Zeus>("ZeusLive", new PerRequestLifetimeManager(), new InjectionConstructor("ZeusLive"))
    .RegisterType<IDataContextAsync, Zeus>(new PerRequestLifetimeManager(), new InjectionConstructor("Zeus"))
    .RegisterType<IUnitOfWorkAsync, UnitOfWork>()
    .RegisterType<IRepositoryAsync<cd_Job>, Repository<cd_Job>>();