Definition:
1. High level classes/modules should not depend on low-level classes/modules, both should depend on abstraction.
2. It relates to the way in which an object obtains references to its dependencies.
3. The main benefit of the Dependency Injection that classes are more loosely coupled, because they do not have hard-coded dependencies. This follows the Dependency Inversion Principle, which was mentioned above. Instead of referencing specific implementations, classes request abstractions (usually interfaces) which are provided to them when the class is constructed.
4. Dependency injection is a pattern to allow your application to inject objects on the fly to classes that need them, without forcing those classes to be responsible for those objects. It allows your code to be more loosely coupled, and Entity Framework Core plugs in to this same system of services.
The main benefit of the Dependency Injection that classes are more loosely coupled, because they do not have concrete dependencies. Instead of referencing specific implementations, classes request abstractions (usually interfaces) which are provided to them when the class is constructed.
Code Example:
Constructor Injection
This is the most commonly used dependency pattern in Object Oriented Programming. The constructor injection normally has only one parameterized constructor, so in this constructor dependency there is no default constructor and we need to pass the specified value at the time of object creation. We can use the injection component anywhere within the class. It addresses the most common scenario where a class requires one or more dependencies.
The following is an example:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace propertyinjuction
- {
- public interface text
- {
- void print();
- }
- class format : text
- {
- public void print()
- {
- Console.WriteLine(" here is text format");
- }
- }
- // constructor injection
- public class constructorinjection
- {
- private text _text;
- public constructorinjection(text t1)
- {
- this._text = t1;
- }
- public void print()
- {
- _text.print();
- }
- }
- class constructor
- {
- static void Main(string[] args)
- {
- constructorinjection cs = new constructorinjection(new format());
- cs.print();
- Console.ReadKey();
- }
- }
- }
By passing the services that implemented the text interface the builder assembled the dependencies.
Property Injection
We use constructor injection, but there are some cases where I need a parameter-less constructor so we need to use property injection.
Property Injection
We use constructor injection, but there are some cases where I need a parameter-less constructor so we need to use property injection.
The following is an example:
- public interface INofificationAction
- {
- void ActOnNotification(string message);
- }
- class atul
- {
- INofificationAction task = null;
- public void notify(INofificationAction at ,string messages)
- {
- this.task = at;
- task.ActOnNotification(messages);
- }
- }
- class EventLogWriter : INofificationAction
- {
- public void ActOnNotification(string message)
- {
- // Write to event log here
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //services srv = new services();
- //other oth = new other();
- //oth.run();
- //Console.WriteLine();
- EventLogWriter elw = new EventLogWriter();
- atul at = new atul();
- at.notify(elw, "to logg");
- Console.ReadKey();
- }
- }
You cannot control when the dependency is set at all, it can be changed at any point in the object's lifetime.
Method Injection
In method injection we need to pass the dependency in the method only. The entire class does not need the dependency, just the one method. I have a class with a method that has a dependency. I do not want to use constructor injection because then I would be creating the dependent object every time this class is instantiated and most of the methods do not need this dependent object.
The following is an example:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace propertyinjuction
- {
- public interface Iset
- {
- void print();
- }
- public class servic : Iset
- {
- public void print()
- {
- Console.WriteLine("print........");
- }
- }
- public class client
- {
- private Iset _set;
- public void run(Iset serv)
- {
- this._set = serv;
- Console.WriteLine("start");
- this._set.print();
- }
- }
- class method
- {
- public static void Main()
- {
- client cn = new client();
- cn.run(new servic());
- Console.ReadKey();
- }
- }
- }
Advantage of Dependency Injection(DI):
- Reduces class coupling
- Increases code reusing
- Improves code maintainability
- Improves application testing
Comments
Post a Comment