为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
猛跑小猪 2023-09-16 17:33:41
如何对通用接口使用依赖注入?我想在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();    }
查看完整描述

2 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

谢谢大家,这就是我想做的。也许这会对将来的某人有所帮助。


        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; }

}


查看完整回答
反对 回复 2023-09-16
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

还将 Presenter 包含在容器中并解析它。


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;

}


查看完整回答
反对 回复 2023-09-16
  • 2 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信