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

如何对通用接口使用依赖注入?我想在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();

    }




猛跑小猪
浏览 96回答 2
2回答

蝴蝶刀刀

谢谢大家,这就是我想做的。也许这会对将来的某人有所帮助。&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CompositionRoot.Wire(new DependencyModule());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IDrawPresenter prisenter = new DrawPresenter();//kernel.Get<IDrawPresenter>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prisenter.Show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; public class CompositionRoot&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private static IKernel _ninjectKernel;&nbsp; &nbsp; &nbsp; &nbsp; public static void Wire(INinjectModule module)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ninjectKernel = new StandardKernel(module);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static T Resolve<T>()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _ninjectKernel.Get<T>();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class DependencyModule : NinjectModule&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public override void Load()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bind<IDrawView>().To<DrawWindow>();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public abstract class BasePresenter<TView> : IPrisenter<TView>&nbsp; &nbsp; where TView : IView&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; protected BasePresenter()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View = CompositionRoot.Resolve<TView>();//NinjectProgram.Kernel.Get<TView>();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected TView View { get; private set; }}

凤凰求蛊

还将 Presenter 包含在容器中并解析它。public class DependencyModule : NinjectModule {&nbsp; &nbsp; public override void Load() {&nbsp; &nbsp; &nbsp; &nbsp; Bind<IDrawView>().To<DrawWindow>();&nbsp; &nbsp; &nbsp; &nbsp; Bind<IDrawPresenter>().To<DrawPresenter>();&nbsp; &nbsp; }}它的所有依赖项(如果已注册)也将被解析并注入到演示者中static void Main(string[] args) {&nbsp; &nbsp; var kernel = new StandardKernel();&nbsp; &nbsp; kernel.Load(new DependencyModule());&nbsp; &nbsp; IDrawPresenter presenter= kernel.Get<IDrawPresenter>();&nbsp; &nbsp; presenter.Show();&nbsp; &nbsp; Console.ReadLine();}以上是基于public abstract class BasePresenter<TView> : IPrisenter<TView> where TView : IView {&nbsp; &nbsp; protected BasePresenter(TView view) {&nbsp; &nbsp; &nbsp; &nbsp; View = view;&nbsp; &nbsp; }&nbsp; &nbsp; protected TView View { get; private set; }&nbsp; &nbsp; public void Show() {&nbsp; &nbsp; &nbsp; &nbsp; View.Show();&nbsp; &nbsp; }}public class DrawPresenter : BasePresenter<IDrawView>, IDrawPresenter {&nbsp; &nbsp; public DrawPresenter(IDrawView view): base(view) {&nbsp; &nbsp; }&nbsp; &nbsp; public object SelectedDraws => View.GetGridDraw;}
打开App,查看更多内容
随时随地看视频慕课网APP