Dependency Injection (Ninject)
Definition:- Is a set of software design principles and patterns that enable us to develop loose coupled code.
Loose Coupled means that class, modules and assembly have good isolation from each other. So if we need to make a change to one part of our application we only need to update the relevant module. We do not have to make modification through the application.
Why do we need Loose Coupled Code ?
- Extensibility – we can swap code to include additional function. This code can reply to new requirements, which become very easy to do.
- Testability – allow you to unit test loose couple code
- Late Binding – dynamic load modules when load application, this allows us to shipped different components to different clients and their application which load the right functionility when they load it
- Parallel Development – allows for parallel development, if one component does not depend on another, we can have different teams to work in parallel on each component.
- Maintainability – if we have different parts of code isolated from each other we can know exact where to troubleshoot if something goes wrong, or fix bugs or make updates.
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.
Write your code so it’s flexible…
.Weapon = weapon; } }
…and let Ninject glue it together for you.
.Bind<IWeapon>().To<Sword>(); } }
- 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.
- 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.
- 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.
- 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).
- 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.
- Stealthy. Ninject will not invade your code. You can easily isolate the dependency on Ninject to a single assembly in your project.
- 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.