仍然需要帮助理解为什么Ninject可能会比手动DI更好 [英] Still need help understanding why Ninject might be better than manual DI

查看:166
本文介绍了仍然需要帮助理解为什么Ninject可能会比手动DI更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个扩展的问题的为什么我需要一个IoC容器,而不是简单的DI代码?

This is an extension to the question Why do I need an IoC container as opposed to straightforward DI code?

我一直在学习Ninject和下面的例子上来,示例经过这样做的手工方式DI和做DI的Ninject方式:

I've been learning Ninject and came up with the following example, the example goes through the manual way of doing DI and the Ninject way of doing DI:

class Program
{
    static void Main(string[] args)
    {
        NinjectWay();
        ManualWay();
        Console.ReadKey();
    }

    private static void ManualWay()
    {
        Console.WriteLine("ManualWay***********************");
        IWeapon sword = new Sword();
        Samurai samurai = new Samurai(sword);

        Console.WriteLine(samurai.Attack("ManualWay..."));

        // change weapon
        IWeapon dagger = new Dagger();
        samurai.Weapon = dagger;

        Console.WriteLine(samurai.Attack("ManualWay..."));

        IWeapon weapon = new Shuriken();
        IWarrior ninja = new Ninja(weapon);
        Console.WriteLine("Manual way.. inject shuriken when a ninja. " + ninja.Weapon.Name);

        IWarrior ninja2 = new Ninja(weapon);
    }

    private static void NinjectWay()
    {
        Console.WriteLine("NinjectWay***********************");
        IKernel kernel = new StandardKernel();
        kernel.Bind<IWeapon>().To<Sword>();
        var samurai = kernel.Get<Samurai>();

        Console.WriteLine(samurai.Attack("NinjectWay..."));

        kernel.Rebind<IWeapon>().To<Dagger>();
        samurai = kernel.Get<Samurai>();

        Console.WriteLine(samurai.Attack("NinjectWay..."));

        kernel.Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Ninja>();
        var ninja = kernel.Get<Ninja>();
        ninja.OffHandWeapon = new ShortSword();

        Console.WriteLine("Conditional Injection..."+ninja.Weapon.Name);
        Console.WriteLine("Conditional Injection: OffhandWeapon = " + ninja.OffHandWeapon.Name);

        var ninja2 = kernel.Get<Ninja>();
        Console.WriteLine("Conditional Injection..." + ninja2.Weapon.Name);
        Console.WriteLine("Conditional Injection: OffhandWeapon = " + ninja2.OffHandWeapon.Name);

        Console.WriteLine("");
    }
}



我听到的利益发生时,该项目的规模增加了,但我没有看到它。帮助我更好的理解。在C#/ Ninject提供更多的例子和帮助我了解那里的好处真的变得很明显。

I hear the benefits happen when the scale of the project increases but I'm not seeing it. Help me understand this better. Provide more examples in C#/Ninject and help me understand where the benefits really become apparent.

推荐答案

与其他答案建议Ninject ISN T主要是使代码更容易测试。它是依赖注入,使你的代码的可测试性!依赖注入可以不IoC容器在创建facotries都可以使用。但肯定的是,beeing能够方便地更换一些部件的集成测试(不要在单元测试中使用Ninject)是一个很好的副作用。

Unlike the other answers suggest Ninject isn't mainly about making your code more testable. It's Dependency Injection that makes your code more testable! Dependency Injection can be used without an IoC container by creating everything in facotries. But sure, beeing able to replace some parts easily for Integration Tests (don't use Ninject in unit tests) is a nice side effect.

IoC容器例如Ninject主要是关于把你的类一起工作的软件。在小项目这可以很容易地使用一些工厂来完成。但是,随着应用的增长工厂变得越来越复杂。试想一下,有一些人被重用他人新近在每次使用创建的各种服务的应用程序。在一些服务也被几个组件。

IoC containers like Ninject are mainly about putting your classes together to a working software. In small projects this can easily be done using some factories. But as your application grows the factories get more and more complicated. Imagine an application that has various services some of them are reused others are newly created for every usage. Some of the services are also used by several components.

如果您使用的是你必须定义的一个IoC容器恰好一次如何获得服务实例,什么是它的生命周期。在一家工厂另一方面,你必须指定你如何获取实例的每类需要一个实例(例如新MyServiceFactory()。的CreateInstance())。此外,您必须手动控制的生命周期。

If you are using a IoC container you have to define exactly once how you get the service instance and what's its lifecycle. On the other hand in a factory you have to specify how you get the instance for every class that needs an instance (e.g. new MyServiceFactory().CreateInstance()). Furthermore, you have to control the lifecycle manually.

这意味着随着项目的发展IoC容器的配置与项目的规模一起成长的线性。但在另一方面工厂变得更喜欢,因为是在整个应用程序(例如用户验证)使用的服务exponentional

This means as the project grows the configuration of an IoC container grows linear together with the project size. But a factory on the other hand grows more exponentional like as there are services that are used throughout the application (e.g. user auth).

BTW:你举的例子是不是很好,因为重新连接是不是一个共同的行动。通常情况下,配置应用程序启动时只进行一次。

BTW: Your example isn't very good because Rebinding isn't a common action. Usually the configuration is done only once at application startup.

这篇关于仍然需要帮助理解为什么Ninject可能会比手动DI更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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