建筑使用MVC和Ninject作为IOC容器一个WinForms应用 [英] Building a WinForms Application using MVC and Ninject as IoC Container

查看:126
本文介绍了建筑使用MVC和Ninject作为IOC容器一个WinForms应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将不得不重新编写大量的WinForms应用程序,我想使用MVC允许增加测试功能等。我也想通过Ninject作为我IoC容器,因为它重量轻,速度快,会增加exstensibility我应用向前发展。



我做阅读了大量的,我已经成功地使这一新应用程序的arcitecture一个开始。但是,我不知道我使用Ninject时有正确的想法。该代码...



与Program.cs文件和相关类...

 <启动code>静态类节目
{
[STAThread]
静态无效的主要()
{
FileLogHandler fileLogHandler =新FileLogHandler(Utils.GetLogFilePath()) ;
Log.LogHandler = fileLogHandler;
Log.Trace(Program.Main():记录初始化);

CompositionRoot.Initialize(新ApplicationModule());

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(CompositionRoot.Resolve&所述; ApplicationShellView>());
}
}

公共类CompositionRoot
{
私有静态的iKernel _ninjectKernel;

公共静态无效初始化(INinjectModule模块)
{
_ninjectKernel =新StandardKernel(模块);
}

公共静态牛逼解决< T>()
{
返回_ninjectKernel.Get< T>();
}
}

公共类ApplicationModule:NinjectModule
{
公共覆盖无效的load()
{
绑定(typeof运算(IApplicationShellView))至(typeof运算(ApplicationShellView))。
}
}

这是我的 ApplicationShellView

 公共部分类ApplicationShellView:形式,IApplicationShellView 
{
公共ApplicationShellView( )
{
的InitializeComponent();
}

公共无效InitializeView()
{
dockPanel.Theme = vS2012LightTheme;
}
}



与接口

 公共接口IApplicationShellView 
{
无效InitializeView();
}



该视图的控制器是



 公共类ApplicationShellController 
{
私人IApplicationShellView图。

公共ApplicationShellController(IApplicationShellView视图)
{
view.InitializeView();
}
}

目前控制器是多余的,而且尽管此代码作品和我的视图显示,我有一些重要的问题...




  1. 我应该使用 ApplicationShellController 来初始化我的形式,这是目前的的使用MVC模式?

  2. 这感觉就像我写了一个服务定位器,并且从什么我已阅读,这是不好的。我应该有别的方法是使用Ninject海洋学委员会初始化我的应用程序?

  3. 任何其他意见,以我在做什么正确的[如果有什么!] /错了?



非常感谢您的时间。


解决方案

  1. 没有你不应该初始化你的控制器,这正是国际奥委会和Ninject是。当你初始化你的视图/表,Ninject应使视图获取它取决于控制器,这将自动获取控制器这取决于等等。结果
    当然,这是行不通的,就像你现在设置它。对于初学者来说,您认为需要知道它取决于控制器

     公共部分类ApplicationShellView:形式,IApplicationShellView 
    {
    私人IApplicationShellController _controller;

    公共ApplicationShellView()
    {
    的InitializeComponent();
    的init();

    // InitializeView()
    }

    私人无效的init(){
    _controller = NinjectProgram.Kernel.Get< IApplicationShellController>();
    //因为您的视图知道该控制器可以随时通过自己作为参数,甚至使用的setter注入
    //例如:_controller.SetView1(本);
    }

    公共无效InitializeView()
    {
    dockPanel.Theme = vS2012LightTheme;
    }
    }

    公共类ApplicationShellController:IApplicationShellController
    {

    // Implementes功能为MainForm的。

    公共ApplicationShellController()
    {
    //也有可能与DI
    以添加其他控制器}
    }


  2. 这的确看起来像一个服务定位器,只需初始化您认为应该做的足够了。

     公共类NinjectProgram 
    {
    //获取该程序的注入籽粒。
    公共静态的iKernel内核{搞定;保护套; }
    }

    公共类节目:NinjectProgram
    {
    [STAThread]
    私有静态无效的主要()
    {
    内核=新StandardKernel();
    Kernel.Load(新ApplicationModule());

    Application.Run(新ApplicationShellView());
    }
    }

    公共类ApplicationModule:NinjectModule
    {
    公共覆盖无效的load()
    {
    //这里在这里我们定义采用哪些实现地图到什么接口。
    绑定&所述; IApplicationShellController方式>()到< ApplicationShellController>();

    //我们也可以加载其他模块项目取决于。
    Kernel.Load(新NinjectModule());
    }
    }


  3. 不要试图使它太复杂的,良好的开端是重要的,但你可以随时申请变更的时间和地点在开发过程中需要的。




我相信下面GitHub上的项目可能是一个很好的起点:您可能如何WinForms应用程序中使用Ninject示例



如果您有任何问题,只是发表评论,我会尽力尽快


回答

I am having to re-write a large WinForms application and I want to use MVC to allow increased testing capability etc. I want to also adopt Ninject as my IoC container as it is lightweight, fast and will increase the exstensibility of my application going forward.

I have done a great deal of reading and I have managed to make a start on the arcitecture of this new application. However, I am not sure i have the right idea when using Ninject. The code...

Starting with Program.cs and related classes...

static class Program
{
    [STAThread]
    static void Main()
    {
        FileLogHandler fileLogHandler = new FileLogHandler(Utils.GetLogFilePath());
        Log.LogHandler = fileLogHandler;
        Log.Trace("Program.Main(): Logging initialized");

        CompositionRoot.Initialize(new ApplicationModule());

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(CompositionRoot.Resolve<ApplicationShellView>());
    }
}

public class CompositionRoot
{
    private static IKernel _ninjectKernel;

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

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

public class ApplicationModule : NinjectModule
{
    public override void Load()
    {
        Bind(typeof(IApplicationShellView)).To(typeof(ApplicationShellView));
    }
}

An my ApplicationShellView is

public partial class ApplicationShellView : Form, IApplicationShellView
{
    public ApplicationShellView()
    {
        InitializeComponent();
    }

    public void InitializeView()
    {
        dockPanel.Theme = vS2012LightTheme;
    }
}

with interface

public interface IApplicationShellView
{
    void InitializeView();
}

The controller for this view is

public class ApplicationShellController
{
    private IApplicationShellView view;

    public ApplicationShellController(IApplicationShellView view)
    {
        view.InitializeView();
    }
}

Currently the controller is redundant, and although this code works and my view displays, I have some important questions...

  1. Should I be using the ApplicationShellController to initialize my form, currently this is not using MVC "pattern"?
  2. It feels like I have written a Service Locator, and from what I have read, this is bad. How else should I be using Ninject for IoC to initialize my application?
  3. Any other advice as to what I am doing right[if anything!]/wrong?

Thanks very much for your time.

解决方案

  1. No you should not be initializing your controller, this exactly what IoC and Ninject are for. When you initialize your view/form, Ninject should make the view fetch the controller it depends on, which will auto fetch controllers it depends on and so on.
    Of course this won't work like you've set it up right now. For starters, your view needs to know the controller it depends on.

    public partial class ApplicationShellView : Form, IApplicationShellView
    {
        private IApplicationShellController _controller;
    
        public ApplicationShellView()
        {
            InitializeComponent();
            init();
    
            //InitializeView()
        }
    
        private void init() {
            _controller = NinjectProgram.Kernel.Get<IApplicationShellController>();
            //Because your view knows the controller you can always pass himself as parameter or even use setter to inject
            //For example:  _controller.SetView1(this);
        }
    
        public void InitializeView()
        {
            dockPanel.Theme = vS2012LightTheme;
        }
    }
    
    public class ApplicationShellController : IApplicationShellController
    {
    
        //Implementes functionality for the MainForm.
    
        public ApplicationShellController()
        {
            //Also possible to add other controllers with DI
        }
    }
    

  2. This does indeed look like a Service Locator, simply initializing your view should do be sufficient.

    public class NinjectProgram
    {
        //Gets the inject kernal for the program.
        public static IKernel Kernel { get; protected set; }
    }
    
    public class Program : NinjectProgram
    {
        [STAThread]
        private static void Main()
        {
            Kernel = new StandardKernel();
            Kernel.Load(new ApplicationModule());
    
            Application.Run(new ApplicationShellView());
        }
    }
    
    public class ApplicationModule : NinjectModule
    {
        public override void Load()
        {
            //Here is where we define what implementations map to what interfaces.
            Bind<IApplicationShellController>().To<ApplicationShellController>();
    
            //We can also load other modules this project depends on.
            Kernel.Load(new NinjectModule());
        }
    }
    

  3. Don't try and make it too complicated, a good start is important but you can always apply changes when and where needed during development.

I believe the following GitHub project might be a good starting point: Example of how you might use Ninject within a WinForms application.

If you have any more questions, just leave a comment and I'll try to answer them as soon as possible

这篇关于建筑使用MVC和Ninject作为IOC容器一个WinForms应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆