如何将多个演示者注入主要演示者?

时间:2014-11-20 22:12:07

标签: c# design-patterns dependency-injection mvp

根据这个answer,我可以将依赖项注入构造函数。但是,如果让我说我有10个分主持人&#39 ;?我应该通过构造函数注入所有这些吗?在这种情况下使用的推荐设计模式(工厂方法,外观等)是什么?我很感激您是否可以使用NUnit和NSubstitute提供示例代码来测试它?提前谢谢!

public void PresenterMain(IViewMain view
, IServiceMain service
, IAddNewJobPresenter addNewJobPresenter
, IClientManagementPresenter clientManagementPresenter
, IJobBatchesPresenter jobBatchesPresenter
, IReportsPresenter reportPresenter...etc)

1 个答案:

答案 0 :(得分:1)

鉴于您的消费者需要使用10个演示者实例,有几个选项。以下是设置场景的一些基本框架:

public interface IPresenter
{
    void Present();
}

public interface IConsumer
{
    void DoSomething();
}

public class SomeConsumer : IConsumer
{
    private readonly IPresenter presenter;

    public SomeConsumer(IPresenter presenter)
    {
        if (presenter == null)
            throw new ArgumentNullException("presenter");
        this.presenter = presenter;
    }

    public void DoSomething()
    {
        this.presenter.Present();
    }
}

您可以使用composite pattern,然后您可以在列表中订阅演示者。

public class Presenter1 : IPresenter
{
    public void Present()
    {
        // Do something here
    }
}

public class Presenter2 : IPresenter
{
    public void Present()
    {
        // Do something here
    }
}

public class Presenter3 : IPresenter
{
    public void Present()
    {
        // Do something here
    }
}

public class CompositePresenter : IPresenter
{
    private readonly IPresenter[] presenters;

    public CompositePresenter(IPresenter[] presenters)
    {
        if (presenters == null)
            throw new ArgumentNullException("presenters");
        this.presenters = presenters;
    }

    public void Present()
    {
        // Do nothing except delegate the call to the nested
        // instances. You may need to do some extra work to deal
        // with multiple return values, like add up the values
        // or decide which value works best for the scenario.
        foreach (var presenter in this.presenters)
        {
            presenter.Present();
        }
    }
}

然后将它连接起来:

var presenter1 = new Presenter1();
var presenter2 = new Presenter2();
var presenter3 = new Presenter3();
var compositePresenter = new CompositePresenter(new IPresenter[] {
    presenter1,
    presenter2,
    presenter3
});

var consumer = new SomeConsumer(compositePresenter);

或者您可以使用decorator pattern

public class Presenter1 : IPresenter
{
    public Presenter1(IPresenter innerPresenter)
    {
        if (innerPresenter == null)
            throw new ArgumentNullException("innerPresenter");
        this.innerPresenter = innerPresenter;
    }

    public void Present()
    {
        // Do something here

        // You could make this call conditional
        this.innerPresenter.Present();

        // Or do something here
    }
}

public class Presenter2 : IPresenter
{
    public Presenter2(IPresenter innerPresenter)
    {
        if (innerPresenter == null)
            throw new ArgumentNullException("innerPresenter");
        this.innerPresenter = innerPresenter;
    }

    public void Present()
    {
        // Do something here

        // You could make this call conditional
        this.innerPresenter.Present();

        // Or do something here
    }
}

public class Presenter3 : IPresenter
{
    public Presenter3(IPresenter innerPresenter)
    {
        if (innerPresenter == null)
            throw new ArgumentNullException("innerPresenter");
        this.innerPresenter = innerPresenter;
    }

    public void Present()
    {
        // Do something here

        // You could make this call conditional
        this.innerPresenter.Present();

        // Or do something here
    }
}

public class NullPresenter : IPresenter
{
    public void Present()
    {
        // Do nothing here - this class is a placeholder
        // in case you want to expand the design later
    }
}

然后将它连接起来:

var nullPresenter = new NullPresenter();
var presenter3 = new Presenter3(nullPresenter);
var presenter2 = new Presenter2(presenter3);
var presenter1 = new Presenter1(presenter2);

var consumer = new SomeConsumer(presenter1);