如何对通用接口使用依赖注入?

时间:2019-08-23 09:34:23

标签: c# winforms dependency-injection mvp

如何对通用接口使用依赖注入?我希望在DrawPresenter中创建IDrawView接口,并控制视图。

我不知道该使用什么,Ninject或其他。我正在使用WinForms。

哪个更好选择?

class Program
{
    static void Main(string[] args)
    {
        IDrawPresenter prisenter = new DrawPresenter(new DrawWindow());
        prisenter.Show();
        Console.ReadLine();
    }
}

public interface IView
{
    void Show();
}

public interface IDrawView : IView
{
    object GetGridDraw { get; }
}
public interface IPrisenter<TView> where TView : IView
{
    void Show();
}
public interface IDrawPresenter : IPrisenter<IDrawView>
{
    object SelectedDraws { get; }
}

public class DrawWindow : IDrawView
{
    public object GetGridDraw => 1;

    public void Show()
    {
        Console.WriteLine("Show Window");
    }
}

public abstract class BasePresenter<TView> : IPrisenter<TView>
where TView : IView
{
    protected BasePresenter(TView view)
    {
        View = view;
    }
    protected TView View { get; private set; }
    public void Show()
    {
        View.Show();
    }
}

public class DrawPresenter : BasePresenter<IDrawView>, IDrawPresenter
{
    public DrawPresenter(IDrawView view): base(view)
    {
    }
    public object SelectedDraws => View.GetGridDraw;
}

DI可以实现吗?

IDrawPresenter prisenter = new DrawPresenter();   
    public DrawPresenter()
    {
    }

我需要为Presenter管理表单的内容。

这就是我想要得到的。但这行不通...

public class NinjectProgram
{
    //Gets the inject kernal for the program.
    public static IKernel Kernel { get; protected set; }
}

public class DependencyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IDrawView>().To<DrawWindow>();
    }
}

    static void Main(string[] args)
    {
        StandardKernel Kernel = new StandardKernel();
        Kernel.Load(new DependencyModule());

        IDrawPresenter prisenter = new DrawPresenter();
        prisenter.Show();
        Console.ReadLine();
    }

public abstract class BasePresenter<TView> : IPrisenter<TView>
where TView : IView
{
    protected BasePresenter()
    {
        View = NinjectProgram.Kernel.Get<TView>();
    }
    protected TView View { get; private set; }
    public void Show()
    {
        View.Show();
    }
}

2 个答案:

答案 0 :(得分:1)

谢谢大家,这就是我想要做的。也许这会对以后的人有所帮助。

        static void Main(string[] args)
        {
            CompositionRoot.Wire(new DependencyModule());

            IDrawPresenter prisenter = new DrawPresenter();//kernel.Get<IDrawPresenter>();
            prisenter.Show();
            Console.ReadLine();
        }

    public class CompositionRoot
    {
        private static IKernel _ninjectKernel;

        public static void Wire(INinjectModule module)
        {
            _ninjectKernel = new StandardKernel(module);
        }

        public static T Resolve<T>()
        {
            return _ninjectKernel.Get<T>();
        }
    }

    public class DependencyModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IDrawView>().To<DrawWindow>();
        }
    }

    public abstract class BasePresenter<TView> : IPrisenter<TView>
    where TView : IView
    {
        protected BasePresenter()
        {
            View = CompositionRoot.Resolve<TView>();//NinjectProgram.Kernel.Get<TView>();
        }
        protected TView View { get; private set; }
}

答案 1 :(得分:0)

还将演示者包含在容器中并解决它。

public class DependencyModule : NinjectModule {
    public override void Load() {
        Bind<IDrawView>().To<DrawWindow>();
        Bind<IDrawPresenter>().To<DrawPresenter>();
    }
}

其所有依赖项(如果已注册)也将得到解决,并注入到演示者中

static void Main(string[] args) {
    var kernel = new StandardKernel();
    kernel.Load(new DependencyModule());

    IDrawPresenter presenter= kernel.Get<IDrawPresenter>();
    presenter.Show();
    Console.ReadLine();
}

以上内容基于

public abstract class BasePresenter<TView> : IPrisenter<TView> where TView : IView {
    protected BasePresenter(TView view) {
        View = view;
    }
    protected TView View { get; private set; }
    public void Show() {
        View.Show();
    }
}

public class DrawPresenter : BasePresenter<IDrawView>, IDrawPresenter {
    public DrawPresenter(IDrawView view): base(view) {
    }
    public object SelectedDraws => View.GetGridDraw;
}