本文介绍了什么是与ninject的WinForms对话框中的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Ninject创建aplication。我有一个设置的MainForm 和对话的形式。我应该如何实现呢?应的MainForm 有一个内核实例,然后创建 SettingsForm 获取LT; SettingsForm>()的方法?还是应该的MainForm 构造与 SettingsForm 实例参数?

I try to create aplication with Ninject. I have one MainForm and dialog form for settings. How should i implement this? Should MainForm have a Kernel instance and then create SettingsForm with Get<SettingsForm>() method? Or should MainForm constructor have parameter with SettingsForm instance?

我试图找到ninject WinForm应用程序的一些例子,但我只发现这是在Visual C#2008 Express的无用某些ASP.NET应用程序。

I tried find some example of WinForm application with ninject, but i found only some ASP.NET applications which are useless in Visual C# 2008 express.

推荐答案

我会做的依赖是在表单到表单级别。你想拥有在介于两者之间。

I would make the dependency be at the form-to-form level. You want to have something in between that.

namespace Example
{
    public class SettingsRepository
    {
        public SettingsRepository()
        {

        }
    }
    public class SettingsForm
    {
        private SettingsRepository _settingsRepository;
        public SettingsForm( SettingsRepository settingsRepository )
        {
            _settingsRepository = settingsRepository;

        }
    }
    public class MainForm
    {
        private SettingsRepository _settingsRepository;
        private Func<SettingsForm> _createSettingsForm;
        public MainForm( Func<SettingsForm> createSettingsForm, SettingsRepository settingsRepository )
        {
            _createSettingsForm = createSettingsForm;
            _settingsRepository = settingsRepository;

        }
    }
}



然后你注入 Func键< SettingsForm> 到类去除容器直接使用/内核从您的代码(如果你正在做内联获取呼吁所有的地方,你在做服务定位,这是一个不同的事情完全DI)。

Then you inject a Func<SettingsForm> into your class to remove the direct usage of the container / Kernel from your code (if you're doing inline Get calls all over the place, you're doing Service Location, which is a different thing to DI entirely).

    public class ExampleNinjectModule : NinjectModule
    {
        public override void Load()
        {
            Bind<Func<SettingsForm>>().ToMethod( context => () => context.Kernel.Get<SettingsForm>()  );
        }
    }



另一种方法是添加内核来构造函数ARGS(Ninject自动解决),但迅速成为一般一团糟。

Another approach is to add a Kernel to your constructor args (Ninject automatically resolves it), but that quickly becomes a mess in general.

我试过一个快速搜索样本,但可悲的是剪掉迅速在的WinForms空间发现任何东西。 。我建议也许寻找WPF的例子,而不是

I tried a quick search for samples, but sadly didnt find anything quickly in the WinForms space. I'd suggest perhaps looking for WPF examples instead.

底线是,你不会出大错,如果你:

Bottom line is you wont go far wrong if you:


  1. 坚持使用构造器注入,避免内核或容器的属性直接使用您的真实代码尽可能

  2. 不要使用全局内核和/或服务定位

更新09月12日:这一天肯定会聘请Ninject.Extensions.Factory来管理工厂(即大部分代码将上面会自动genned幕后)

Update Sep 12: These days one would definitely employ Ninject.Extensions.Factory to manage the factory (i.e., most of the code would above would be auto-genned behind the scenes)

这篇关于什么是与ninject的WinForms对话框中的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 19:31