Possible Duplicate:
What is Inversion of Control?
I am not familiar with Inversion of Control (IoC). What is IoC and how can applications benefit from using IOC. How is it implemented in .NET with C#?
NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.
IoC means that one code calls another; DI goes beyond that and implements IoC by using composition. A DI or IoC container needs to instantiate objects (dependencies) and provide them to the application. To do so, it must deal with constructor injection, setter injection, and interface injection.
ASP.NET Core contains a built-in dependency injection mechanism. In the Startup. cs file, there is a method called ConfigureServices which registers all application services in the IServiceCollection parameter. The collection is managed by the Microsoft.
Inversion of control (IOC) talks about who is going to initiate the call to dependent object where as the Dependency Injection (DI) talks about how one object can acquire dependency. Sometimes it becomes very tough to understand the concepts.
As a developer, you may be familiar with the terms IoC (Inversion of Control), DIP (Dependency Inversion Principle), DI (Dependency Injection) Design pattern, and IoC containers. But are you confident what each term means with some real-time examples?
Dependency Injection is a design pattern whereas IoC container is a framework. Let’s have a look at each term before going into details. The main objective of Inversion of control (IoC) is to remove dependencies between the objects of an application which makes the application more decoupled and maintainable.
The .NET Core IoC container is located in Microsoft.Extensions.DependencyInjection namespace. Let’s look at what are the steps involved in order to use this in our application.
ASP.NET Core framework includes built-in IoC container for automatic dependency injection. The built-in IoC container is a simple yet effective container. Let's understand how the built-in IoC container works internally.
If you have a set of classes that depend on eachother, it's difficult to replace some class for another (better/cheaper/faster) class that does the same job. In order to make your code more flexible, you can use dependency injection.
An easy to use DI framework for C# is Ninject. They have a very understandable tutorial about this subject.
Links:
http://ninject.org/learn
http://blog.andresays.org/2010/10/dependency-injection-with-ninject-and-asp-net-mvc/
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With