DI-Container:如何将配置传递给对象

tan*_*ius 5 dependency-injection inversion-of-control

有时我的课程需要获取一些构建信息。我不是在谈论对其他对象(将被注入)的引用,而是在谈论(例如)包含唯一信息的字符串:

// Scoped as singleton!
class Repository
{
    public Repository( InjectedObject injectedObject, string path ) { ... }
}
Run Code Online (Sandbox Code Playgroud)

你如何注入这个字符串?一种可能性是编写一个Init()方法并避免注入字符串:

class Repository
{
    public Repository( InjectedObject injectedObject ) { ... }
    public void Init( string path ) { ... }
}
Run Code Online (Sandbox Code Playgroud)

另一种可能性是将信息包装到一个可以注入的对象中:

class InjectedRepositoryPath
{
    public InjectedRepositoryPath( string path ) { ... }
    public string Path { get; private set; }
}

class Repository
{
    public Repository( InjectedObject injectedObject, InjectedRepositoryPath path ) { ... }
}
Run Code Online (Sandbox Code Playgroud)

这样我就必须InjectedRepositoryPath在 DI-Container 初始化期间创建一个实例并注册该实例。但我需要为每个类似的类提供这样一个独特的配置对象。

当然,我可以解析 aRepositryFactory而不是Repository对象,因此工厂会询问我路径:

class RepositoryFactory
{
    Repository Create( string path ) { ... }
}
Run Code Online (Sandbox Code Playgroud)

但同样,这是一个仅用于单例对象的工厂...
或者,最后,由于将从配置文件中提取路径,因此我可以跳过传递字符串并在构造函数中读取配置(这可能不一样)最佳,但可能):

class Repository
{
    public Repository( InjectedObject injectedObject )
    {
        // Read the path from app's config
    }
}
Run Code Online (Sandbox Code Playgroud)

你最喜欢的方法是什么?对于非单例类,您必须使用恕我直言Init()或工厂解决方案,但是单例范围的对象呢?

Mar*_*ann 2

我不喜欢让 DI 容器决定我的 API 设计。容器应符合正确的设计,而不是相反。

DI 友好的方式设计您的类,但不要对 DI 容器做出让步。如果需要连接字符串,则通过构造函数获取字符串:

public class Repository : IRepository
{
    public Repository(string path) { //... }
}
Run Code Online (Sandbox Code Playgroud)

许多 DI 容器可以处理原始值。举个例子,这是使用 Windsor 的一种方法:

container.Register(Component.For<IRepository>()
    .ImplementedBy<Repository>()
    .DependsOn( new { path = "myPath" } ));
Run Code Online (Sandbox Code Playgroud)

但是,如果您选择的容器无法处理原始参数,您始终可以使用Repository知道如何查找字符串的实现进行装饰:

public class ConfiguredRepository : IRepository
{
    private readonly Repository decoratedRepository;

    public ConfiguredRepository()
    {
        string path = // get the path from config, or whereever appropriate
        this.decoratedRepository = new Repository(path);
    }

    // Implement the rest of IRepository by
    // delegating to this.decoratedRepository
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以简单地告诉容器映射IRepositoryConfiguredRepository,同时仍然保持核心存储库实现干净。