Dependency injection c# là gì

  -  

Dependency Injection (DI) is a software design pattern that allows us to lớn develop loosely coupled code. DI is a great way khổng lồ reduce tight coupling between software components. DI also enables us khổng lồ better manage future changes and other complexity in our software. The purpose of DI is lớn make code maintainable.The Dependency Injection pattern uses a builder object khổng lồ initialize objects and provide the required dependencies lớn the object means it allows you khổng lồ "inject" a dependency from outside the class.For example, Suppose your Client class needs to use two service classes, then the best you can vì chưng is lớn make your Client class aware of abstraction i.e. IService interface rather than implementation i.e. Service1 và Service2 classes. In this way, you can change the implementation of the IService interface at any time (và for how many times you want) without changing the client class code.

Bạn đang xem: Dependency injection c# là gì



We can modify this code by following the Dependency Injection implementation ways.

Xem thêm: Đọc Truyện Tấm Cám Anh Chị Suy Nghĩ Gì Về Cuộc Đấu Tranh Giữa Thiện Và Ác

We have sầu the following different ways lớn implement DI :

Constructor Injection

This is a widely used way khổng lồ implement DI.Dependency Injection is done by supplying the DEPENDENCY through the class’s constructor when creating the instance of that class.Injected component can be used anywhere within the class.Recommended to use when the injected dependency, you are using across the class methods.It addresses the most common scenario where a class requires one or more dependencies.public interface IService void Serve();public class Service1 : IService public void Serve() Console.WriteLine("Service1 Called"); public class Service2 : IService public void Serve() Console.WriteLine("Service2 Called"); public class Client private IService _service; public Client(IService service) this._service = service; public ServeMethod() this._service.Serve(); class Program static void Main(string<> args) //creating object Service1 s1 = new Service1(); //passing dependency Client c1 = new Client(s1); //TO DO: Service2 s2 = new Service2(); //passing dependency c1 = new Client(s2); //TO DO: The Injection happens in the constructor, bypassing the Service that implements the IService Interface. The dependencies are assembled by a "Builder" and Builder responsibilities are as follows:Knowing the types of each IServiceAccording khổng lồ the request, feed the abstract IService to lớn the Client

Property/Setter Injection

Recommended using when a class has optional dependencies, or where the implementations may need lớn be swapped.Different logger implementations could be used in this way.Does not require the creation of a new object or modifying the existing one. Without changing the object state, it could work.public interface IService void Serve();public class Service1 : IService public void Serve() Console.WriteLine("Service1 Called"); public class Service2 : IService public void Serve() Console.WriteLine("Service2 Called"); public class Client private IService _service; public IService Service set this._service = value; public ServeMethod() this._service.Serve(); class Program static void Main(string<> args) //creating object Service1 s1 = new Service1(); Client client = new Client(); client.Service = s1; //passing dependency //TO DO: Service2 s2 = new Service2(); client.Service = s2; //passing dependency //TO DO:

Method Injection

Inject the dependency inkhổng lồ a single method và generally for the use of that method.It could be useful, where the whole class does not need the dependency, only one method having that dependency.This is the way is rarely used.public interface IService void Serve();public class Service1 : IService public void Serve() Console.WriteLine("Service1 Called"); public class Service2 : IService public void Serve() Console.WriteLine("Service2 Called"); public class Client private IService _service; public void Start(IService service) service.Serve(); class Program static void Main(string<> args) //creating object Service1 s1 = new Service1(); Client client = new Client(); client.Start(s1); //passing dependency //TO DO: Service2 s2 = new Service2(); client.Start(s2); //passing dependency

Advantages of Dependency Injection

Reduces class couplingIncreases code reusabilityImproves code maintainabilityMake unit testing possible

DI Container

The recommended way to implement DI is, you should use DI containers. If you compose an application without a DI CONTAINER, it is lượt thích a POOR MAN’S DI. If you want lớn implement DI within your ASPhường.NET MVC application using DI container, please vì chưng refer Dependency Injection in ASP.NET MVC using Unity IoC Container.

Xem thêm: Nghĩa Của Từ Pupil Nghĩa Là Gì ? Nghĩa Của Từ Pupils Trong Tiếng Việt

Read More Articles Related khổng lồ Dependency InjectionWhat bởi vì you think?I hope, you will enjoy the various ways of implementing DI pattern. I would lượt thích to have feedbaông chồng from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.