C#控制反转 - 获取实例化基类的所有类

时间:2015-07-19 02:03:07

标签: c# inversion-of-control

有一些我见过的程序,但我不确定我知道它叫什么。从本质上讲,他们将采取各种课程:

的Class1

的Class2

Class3的

这些类中的每一个都有一个变量,可能称为顺序或步骤编号:

[IClassBaseRunner]
public Class1 : IClassBase
{
    public int step{ get { return 10; } }
...

[IClassBaseRunner]
public Class1 : IClassBase
{
    public int step{ get { return 30; } }
...

[IClassBaseRunner]
public Class1 : IClassBase
{
    public int step{ get { return 20; } }
...

所有这些类都是IClassBase之类的接口的一部分。现在,他们可以做一些他们不必初始化任何类的东西,但是每个类都必须有一个属于类的声明的属性,如[IClassBaseRunner]。有了所有这些,它将运行所有组件而无需独立实例化。他们将基本上将每个类添加到列表中并按顺序运行这些步骤。如果您添加了第4步,那么您所做的就是在类的初始化之上添加一行,类似于命名空间中的行,如下所示:

[IClassBaseRunner]
public Class4 : IClassBase

我不确定它叫什么,但如果有人有任何信息或搜索什么,那就太好了。它与控制和属性的反转有关。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不能肯定地说明你所说明的确切模式,但它似乎至少属于基本战略模式。

基于您提及不需要实例化类或在包含新类时添加boostraping代码...这是一个很好的依赖注入库和基于约定的策略可以帮助。添加某种程序集扫描,您很少需要处理配置组件。

以下是使用Ninject和Ninject Conventions扩展(Nuget包ninject.extensions.conventions)如何实现此功能的示例。此示例不需要任何属性修饰器,但您可以根据需要创建和使用它们。

另外,Structure Map和MEF是您可能会尝试的其他流行的依赖注入库,看看哪种最适合您。

static IKernel kernel = new StandardKernel();
void Main()
{
    //
    // Automatic binding using 
    // Ninject.Extensions.Conventions
    // Generally, you only want to declare your
    // DI container once in the application lifetime
    // Expecially in web apps, you will also need to 
    // consider the scope of bound classes, such as:
    //   Transient, Thread, Singleton, or Request
    kernel.Bind(x=>x
        .FromThisAssembly()
        .SelectAllClasses()
        .BindAllInterfaces()
    );
    //
    // Now we can resolve the loader and run it
    // Compare this code to how it would look if you
    // manually instantiate all the dependencies and consider:
    // - How much additional code is there?
    // - How easy is it to perform unit tests on the various
    //   components (Mocking is useful here)?
    // - What is the effort if I need to swap out a service
    //   such as IMessageWriter?
    //
    // IMPORTANT: For example only. Do not use kernel.Get()
    // all over your code base.
    // This results in a ServiceLocator anti-pattern! 
    ProcessRunner runner = kernel.Get<ProcessRunner>();
    runner.Execute();
}

public interface IMessageWriter{
    void Write(string message);
}

public class MessageWriter : IMessageWriter
{
    public void Write(string message){
        Console.WriteLine ("MESSAGE: {0}", message);
    }
}

public interface IProcessStep {
    int Step{ get; }
    void Execute();
}

public class ProcessRunner
{
    private readonly IEnumerable<IProcessStep> steps;
    public ProcessRunner(IEnumerable<IProcessStep> steps)
    {
        this.steps = steps;     
    }

    public void Execute(){
        steps
            .OrderBy (o => o.Step)
            .ToList()
            .ForEach(i=>i.Execute());
    }
}
public class ProcessStep1 : IProcessStep
{
    private readonly IMessageWriter writer;
    public ProcessStep1(IMessageWriter writer)
    {
        this.writer = writer;       
    }
    public int Step { get { return 10; }}
    public void Execute(){
        writer.Write("Hello from step1!");      
    }
}
public class ProcessStep2 : IProcessStep
{
    private readonly IMessageWriter writer;
    public ProcessStep2(IMessageWriter writer)
    {
        this.writer = writer;       
    }
    public int Step { get { return 20; }}
    public void Execute(){
        writer.Write("Hello from step2!");  
    }
}
public class ProcessStep3 : IProcessStep
{
    private readonly IMessageWriter writer;
    public ProcessStep3(IMessageWriter writer)
    {
        this.writer = writer;       
    }
    public int Step { get { return 30; }}
    public void Execute(){
        writer.Write("Hello from step3!");  
    }
}

输出

MESSAGE: Hello from step1!
MESSAGE: Hello from step2!
MESSAGE: Hello from step3!