gpt4 book ai didi

c# - 如何对通用接口(interface)使用依赖注入(inject)?

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:37 27 4
gpt4 key购买 nike

如何对泛型接口(interface)使用依赖注入(inject)?我想在 DrawPresenter 中创建 IDrawView 界面,它控制 View 。

我不知道该用什么,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();
}
}

最佳答案

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

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

关于c# - 如何对通用接口(interface)使用依赖注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57623493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com