gpt4 book ai didi

c# - 温莎城堡 : Inject child dependencies via method after dependency resolves

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:52 25 4
gpt4 key购买 nike

我有以下工作单元。它的组成允许我通过我的 IOC 添加存储库,然后根据类型访问我的服务中的这些存储库——而不必在每次出现新存储库时重写工作单元。根据我所做的研究,为此使用 Activator 不足以担心性能影响。

public class DbContextUnitOfWork : IDisposable
{
public DbContextUnitOfWork(DbContext dbContext))
{
// Set DbContext
}

public void AddRepository<T>() where T : IRepository
{
// Activate T and inject DbContext
// Add to singleton list
}

public T GetRepository<T>() where T : IRepository
{
// If T exists on list return T
}

// Other methods such as save, dispose, etc.
}

我的问题是,如何配置 Windsor,以便在它完成解析 DBContextUnitOfWork 后,我可以为我需要添加的每个存储库显式调用 AddRepository 方法?

最佳答案

您可能正在添加存储库,因为稍后会需要它们。通常的方法是向 Windsor 注册存储库类型,并要求 Windsor 在那个时解析存储库,而不是使用激活器预先创建它们。

关于c# - 温莎城堡 : Inject child dependencies via method after dependency resolves,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12433380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com