Nate Kohari released version 1.0 of the dependency injection container Ninject for. NET.

Source: Internet
Author: User

Ate kohari released version 1.0 of the dependency injection container ninject for. net. Dependency injection is now widely known, and ninject, a new tool, is designed to make better use of this technology.

Ninject is known for providing a fast and lightweight dependency injector for. NET applications. It helps developers split applications into a collection of loosely coupled, highly cohesive functional fragments and then stick them together in a flexible way. Using ninject in the software architecture makes your code easier to write, reuse, test, and modify.

The ninject website describes ninject as follows:

  1. Focus. At present, too many dependency injection projects sacrifice availability, but in exchange for less commonly used features. Every time we add a feature to Ninject, We will weigh its benefits and complexity. Our goal is to keep the entry-this is the most basic bottom line required by Ninject. Ninject has many advanced features. Compared with basic features, it is not necessary to understand them.
  2. Elegance. Framework expansion is the biggest problem for some projects. In view of this, all the core functions of Ninject are a separate assembly, and there is no dependency on the. NET basic class library. During compilation and release, the capacity of the separate assembly is approximately kb.
  3. Fast. Unlike other frameworks that rely on reflection calls, Ninject uses the lightweight code generation feature of CLR 2.0. In multiple scenarios, the performance has been significantly improved (8-50 times.
  4. Rigorous. Ninject effectively avoids mistakes made by developers during their first use. It does not rely on XML ing files and string identifiers to form components, but provides robust domain-specific languages. This means Ninject makes full use of the expertise of languages (such as type security) and IDES (such as smart awareness and code completion.
  5. Agility. Ninject is designed based on the component architecture and allows customization and system evolution. Many aspects of the system can be enhanced or modified to meet the needs of each project.
  6. Hide. Although the. NET feature is used, Ninject does not affect your code. You can easily isolate Ninject dependencies into a separate set of programs in the project.
  7. Powerful. Ninject covers many advanced features. For example, Ninject is the first dependency injection container that supports context binding. You can inject different implementations for the service according to the context of the Service request.

Infoq had the honor to interview Nate, author of the Framework, and introduced ninject's content and principles to readers.

Robert Bazinet(RB): Can you introduce yourself?

Nate Kohari(NK): I am a software architect at the age of 25 from akallow, Ohio. I work at commercial timesharing (CTI), a consulting company that develops software and business intelligence solutions for customers. At CTI, my role is to provide architecture guidance for the. NET project, but I am still involved in development-without a doubt, I am still an architect "fighting at the front. In my college age and early career, I have worked in C, C ++, PHP, and Perl development in Linux, which is one of the main reasons I firmly support open source.

RB: What is ninject?

NK: Ninject is a dependency injection framework. If you want to use it, you can also say "control reversal container ". We have heard about the factory model, and ninject is similar to a "Super factory" that knows how to create all types of objects based on the prompts you provide. Basically, if you think of an application as a "jigsaw puzzle game" and once you tell ninject which "puzzles" to use, ninject will put them together. In addition, if you want to modify the puzzle method in the future, you only need to modify your binding and ninject will do the rest for you.

RB: Why is dependency injection so important?

NK: As the saying goes, "the only constraint is change." In the software field, this is a perfect word ". Fred Brooks writes that there is no silver bullet in the software-since the software is "easy" to change (relatively speaking), people will always find reasons to make changes. The development and scope of the project are always difficult to define. In an instant, the small-scale application prototype you create will become a key task of the enterprise system. Control reversal-in fact, always implemented using dependency injection-will help you design software that meets the requirements for flexibility from the very beginning. When changes inevitably occur, the software can change as needed. This flexibility makes it easier for you to perform unit tests, because during testing, you can replace real components with Mock objects.

RB: Why should we develop a new dependency injection container?

NK: Traditionally, the dependency injection Framework relies on XML to define metadata to form components for your applications. XML is not only difficult to write, but is not easy to expand: As the application grows, XML files defining metadata will expand rapidly. Ninject adopts the Google Guice method, which is a relatively new dependency injection framework in the Java World. It provides interfaces to connect small pieces of software. Ninject's goal is to flexibly inject dependencies regardless of the project size. Even if the project grows, metadata management is still easy. I hope to reduce the difficulty of getting started with dependency injection. The ultimate goal is to create a lightweight, fast, and easy-to-use dependency injection framework.

RB: Why use Ninject instead of other products, such as StructureMap, Unity, Windsor, and others?

NK: Do not misunderstand what I mean. These are great projects. However, they are constrained by my concerns about the idea of "XML first. Because Ninject allows you to use code to build your components, you can use the language and IDE features-such as type security and code completion-which are written in C # and VB. NET, but cannot be used when writing XML. Some frameworks are built on "fluent interfaces" to compensate for XML configuration defects, but these are second-class citizens in APIs.

Ninject helps you provide the dependency injection feature when designing an application. When using Ninject, you can declare one or more "modules", each of which is an integral part of the application. Because these modules are classes, their construction is extremely simple and intelligent. For example, you can read the command line parameters, scan the Assembly to obtain the exact type, or even read the configuration outside the database, which is beyond the XML force. Ninject also has a very powerful advanced feature called "context binding", which can be used to declare multiple Bindings for a specific type. At the same time, Ninject will determine which binding should be used based on the context of the object being created. Ninject is very easy to expand. Ninject pays great attention to this to ensure that each separate functional block of the framework can be customized and can be integrated with different libraries.

RB: Can Ninject be applied to existing projects? If yes, then in general, whether it is code refactoring or writing new code, how should developers use Ninject to effectively promote development forward.

NK: Control reversal is just an idea. Various dependency injection (regardless of the Framework) depends on whether the software you design has the purpose of control reversal. Legacy Software is usually subject to bad strong dependencies (poor separation of concern) and "control issues", that is, objects are responsible for creating their own dependencies. The more reverse control anti-pattern the software has, the more difficult it is to use Ninject to optimize the code. It should be said that any dependency injection technology is hard to cope with this situation. Although difficult, it is still feasible. Ninject can make the dependency extremely simple. Once you use Ninject in the software, it will be easier to correct these anti-patterns. First, you need to find the appropriate separation between the component and the creation, and use Ninject to combine them. Then, you can introduce flexibility by changing the component to interact with the interface. It should start with a small part of the application, and then gradually expand to the rest with time permits. At the same time, Ninject also helps to use dependency injection in the new code, because once you use it at the beginning, it will help you use it throughout the Code. Remember, this is a good thing-it means your code is more flexible!

RB: Can Ninject be used by every developer? Or can Ninject and other DI containers be used only by senior technicians, such as architects or senior developers?

NK: It is successful only when all developers in the project use control inversion and participate in it. As I said, this is completely a mental issue. Ninject is designed to lower the entry threshold, that is, the framework should be simple enough for anyone to participate, without understanding its advanced features. In addition, the general control reversal should be taken into account as a whole. To be able to accept the project, the development leader or architect should enforce the rules on it. Sometimes such a provision is hard to implement ("do you mean we must use this framework? Why can't we directly use 'new' to create an object ?"), But all of this is to pay off the designed debt. Using the dependency injection framework in the early stages of a project is like paying cash instead of using a credit card. When changes inevitably occur, the software will be easier to adapt to new requirements. In my experience, once a large refactoring becomes as simple as implementing the new version and modifying the binding, the value of control reversal becomes more obvious. Building the idea of writing software with control reversal in your heart makes unit testing easier, because you can simulate it in testing instead of implementing it. If you do not need to write unit tests, you should do the same!

RB:: I have noticed that Ninject supports Silverlight 2.0 Beta 2. Can you explain this?

NK: Yes. One of the main objectives of the Ninject project is to make dependency injection suitable for projects of various sizes and types. Given this, Ninject 1.0 not only supports. NET frameworks 2.0, 3.0, and 3.5, but also supports. NET streamlined frameworks 2.0 and 3.5, and the latest trial version of Silverlight 2.0 beta 2. This multi-platform support is what I do and I am very proud. I think a misunderstanding of dependency injection is that it can only be effectively applied to large enterprise projects. However, Ninject is a lightweight and fast framework, as long as you run in. NET runtime, it can be used freely. I personally have successfully applied Ninject in some applications, all of which are built on handheld devices running on the. NET thin framework.

RB: Ninject is open-source. How should other developers participate and submit code?

NK: Yes, Ninject is released based on Apache License 2.0, that is, it can be used by various projects without re-distributing source code. I am always looking forward to user feedback to improve the framework. If you are happy to share the cool features you have created, I would like to put them in patches. The best way to submit them is to publish them in the user group. Select the "speak your mind" link on the Ninject website. At the same time, if you are interested in contributing additional documents, including guides, examples, or other content, you can participate in the document Wiki. Open source is closely related to discussions and communities. Although Ninject is "self-reliant" in software development, I still focus on providing solutions to meet the needs of the community.

RB: What is the best way for developers to get Ninject 1.0? Are there any shortcuts to getting started?

NK: Obtain binary files or source code of version 1.0 on the Ninject website. If you want to stay updated, you can check the latest build version provided on the Subversion. If you want to submit a patch, check the version server to ensure that you have the latest version.

RB: Can Ninject provide detailed documents and sample code/best practices?

NK: Of course! These contents are all placed on Dojo. The content provided here is the ultimate shortcut to Ninject entry, and the sample program demonstrates more advanced features. Dojo is still being improved. With the release of 1.0, I plan to take time out to write more documents. Open-source software often suffers from "waterloo" due to lack of good documentation, and I don't want to see Ninject repeat.

RB: Nate, thank you very much for your interview.

For more information and detailed documentation, you can download and obtain support from Ninject's official website. For details about the framework author Nate Kohari, refer to his blog.

View Original English text: Nate Kohari on Releasing Ninject 1.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.