This article introduces an implementation example of C# DI dependency injection, as follows:
using ; using System; namespace ioc1 { class Program { static void Main(string[] args) { ServiceCollection services = new ServiceCollection(); //Every time you get a new object instance <ITestService, TestServiceImpl>(); //Every time the request to obtain the same object is requested in a life cycle <TestServiceImpl2>(); //The same object is obtained within the same range. The scope of the same object refers to the object in the same braces. <TestServiceImpl3>(); using( ServiceProvider sp = ()) { //If the GetService cannot find the service, it will return null //GetRequiredService: Required, if it cannot be found, an exception will be reported //GetServices finds the collection of services; ITestService t = <ITestService>(); = "Baisha King"; (); ITestService t1 = <ITestService>(); (()); ((t,t1)); = "Himalaya"; (); ("----------------------"); TestServiceImpl2 t2 = <TestServiceImpl2>(); = "Baisha King"; (); TestServiceImpl2 t21 = <TestServiceImpl2>(); ((t2, t21)); = "Himalaya"; (); (); ("----------------------"); using (IServiceScope scope1 = ()) { //Get scope related objects in scope, instead of sp TestServiceImpl3 t3 = <TestServiceImpl3>(); = "Baisha King"; (); TestServiceImpl3 t33 = <TestServiceImpl3>(); = "Himalaya"; (); ((t3, t33)); } } (); } } interface ITestService { string Name { get; set; } void SayHi(); } public class TestServiceImpl : ITestService { public string Name { get; set; } public void SayHi() { ($"Hi,i'm{Name}"); } } public class TestServiceImpl2 : ITestService { public string Name { get; set ; } public void SayHi() { ($"Hello,I am{Name}"); } } public class TestServiceImpl3 : ITestService { public string Name { get; set; } public void SayHi() { ($"you good,I am{Name}"); } } }
Nuget Add; Quote
services = new ServiceCollection();
This line of code creates a newServiceCollection
Example. This is the container in .NET Core for managing services (that is, dependencies).
<TestServiceImpl>();
This line of codeServiceCollection
Added a name calledTestServiceImpl
temporary services.AddTransient
Methods tell .NET Core to create a new instance each time it requests the service. This means that every time you get the service from the service provider, you will get a new oneTestServiceImpl
Example.
3. using( ServiceProvider sp = ())
This line of code creates aServiceProvider
Instance, this instance is the actual object that implements dependency injection in .NET Core. useBuildServiceProvider
After the method, you can use thisServiceProvider
Instance to get the previously added services.
4. TestServiceImpl t = <TestServiceImpl>();
This line of code is created usingServiceProvider
Examplesp
to get a type ofTestServiceImpl
service. Since I used itAddTransient<TestServiceImpl>()
, so here you will get a new oneTestServiceImpl
Example.
5. = "Baisha King";
This line of code is obtainedTestServiceImpl
ExampleName
Assign attribute values and set it to "White Sand King".
6. ();
This line of code callsTestServiceImpl
ExampleSayHi
method. This method may output some information, but since you did not giveTestServiceImpl
I cannot determine the specific behavior of the class.
In general, this code creates a service, registers into the dependency injection container, and obtains the service from the container for use. This is an example of a common dependency injection pattern in .NET Core applications.
<TestServiceImpl2>();
This line of codeServiceCollection
Added a name calledTestServiceImpl2
singleton service. This means that throughout the life cycle of the application, there is only oneTestServiceImpl2
The instance is created and the same instance is returned every time the service is requested.
This is the end of this article about the implementation example of C# DI dependency injection. For more related C# DI dependency injection content, please search for my previous articles or continue browsing the related articles below. I hope you support me in the future!