首页 > 代码库 > Note3:大话Ninject

Note3:大话Ninject

1.简介

Ninject(官网:http://www.ninject.org/)是一个快如闪电,超轻量级的基于的.Net平台的依赖注入框架。它能够帮助你把应用程序分离成一个个松耦合,高内聚的模块,然后用一种灵活的方式组装起来。通过使用Ninject配套你的软件架构,那么代码将会变得更加容易编写,重用性强,易于测试和修改。

Ninject is a lightning-fast, ultra-lightweight dependency injector for .NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software‘s architecture, your code will become easier to write, reuse, test, and modify.

2.特性

(1)“聚焦”:很多现有的依赖注入项目牺牲了特性的可用性,这通常是不必要的。Ninject每添加一个功能,其相较于增加复杂性的好处在于它提高了日常使用。我们的目标是保持知识的门槛(即使用Ninject所需的基本知识水平)尽可能地低。Ninject有很多高级特性,但是理解他们不需要使用基本功能。

Focused. Too many existing dependency injection projects sacrifice usability for features that aren‘t often necessary. Each time a feature is added to Ninject, its benefit is weighed against the complexity it adds to everyday use. Our goal is to keep the barrier to entry - the baseline level of knowledge required to use Ninject - as low as possible. Ninject has many advanced features, but understanding them is not required to use the basic features.

(2)”光滑“:框架膨胀是一些项目的主要关注点,这样,Ninject的所有核心功能是在单一程序集中对.NET基类库没有外部依赖。当编译为release版本时单个程序集的脚本大约85KB。

Sleek. Framework bloat is a major concern for some projects, and as such, all of Ninject‘s core functionality is in a single assembly with no dependencies outside the .NET base class library. This single assembly‘s footprint is approximately 85KB when compiled for release.

(3)“快速”。Ninject在CLR中利用轻量级代码生成的优点,而不是依靠反射调用。这在许多解决方案中能导致性能上8-50x的戏剧性提升。

Fast. Instead of relying on reflection for invocation, Ninject takes advantage of lightweight code generation in the CLR. This can result in a dramatic (8-50x) improvement in performance in many situations.

(4)“精确“:Ninject有助于开发人员在第一时间将事情做正确。Ninject并非依赖XML映射文件和字符串标识符来连接组件,而是提供了一个健壮的领域特定语言。这意味着,Ninject利用了语言(如类型安全)和IDE(如智能感知和代码自动完成)的功能优势。

Precise. Ninject helps developers get things right the first time around. Rather than relying on XML mapping files and string identifiers to wire up components, Ninject provides a robust domain-specific language. This means that Ninject takes advantage of the capabilities of the language (like type-safety) and the IDE (like IntelliSense and code completion).

(5)“敏捷“:Ninject是围绕基于组件的架构而设计,定制和演化。系统的许多方面可以增强或修改以适应每个项目的要求。

Agile. Ninject is designed around a component-based architecture, with customization and evolution in mind. Many facets of the system can be augmented or modified to fit the requirements of each project.

(6)“隐形”:Ninject不会侵犯你的代码。在你的项目中,可以很容易地分离Ninject对单一程序集的依赖。

Stealthy. Ninject will not invade your code. You can easily isolate the dependency on Ninject to a single assembly in your project.

(7)“强大”:Ninject包括许多高级功能。例如,Ninject是第一个支持上下文绑定的依赖注入容器,其中一个服务的不同具体实现根据请求的上下文而被注入。

Powerful. Ninject includes many advanced features. For example, Ninject is the first dependency injector to support contextual binding, in which a different concrete implementation of a service may be injected depending on the context in which it is requested.

3.应用案例

 (1)

Note3:大话Ninject