如何在嵌套 类 中的方法上应用 DI

How to apply DI on methods in nested classes

我正在尝试重构一些旧代码以实现(利用)依赖注入模式。

如何在不同的 class 方法中正确地新建 class?

给出以下最小示例:

class Select {
    List<Where> Wheres = new List<Where>();

    public Select()
    {
    }

    public Select Where(string field, string condition, object value) 
    {
        this.Wheres.Add(new Where(field, condition)); //How to resolve this?
        return this;
    }

    public object DoSomething() 
    {
        //...
    }

}

class Where {
    string _Field;
    string _Condition;
    object _value;

    public Where(string field, string condition, object value)
    {
        _Field = field;
        _Condition = condition;
        _value = value;
    }

    public object DoSomething() 
    {
        //...
    }
}

在 Where-Method 中调用 Resovle() 是最佳实践吗? 还是我应该考虑一种完全不同的方法?

在谷歌上进一步搜索这个主题后,我在 StackExchange 上找到了这个帖子:
Link

所以基本上,Autofac(或许多其他 IoC)能够自动实现一个可以作为 Func 传递的工厂。 StackExchange 上的答案提供了 3 个链接,其中包含 Autofac、Ninject 和 Castle Windsor.

的代码示例