.NET Core development actual combat (Lesson 5: Dependency Injection: good starting point architecture) - Study Notes (a)

05 | Dependency Injection: good starting point architecture

Why use dependency injection framework

  • With dependency injection framework, we can easily manage dependencies between classes, help us to follow the design principles when building applications, to ensure maintainability and scalability of the code
  • ASP.NET Core whole structure, the dependency injection framework provides core capabilities object creation and lifecycle management of the various components work together, but also by the ability to achieve dependency injection framework of

Package

  • Microsoft.Extensions.DependencyInjection.Abstractions
  • Microsoft.Extensions.DependencyInjection

Dependency injection is a core of two or more component package, a package is the abstract, is a specific implementation

Here used a classic design patterns, interfaces to achieve separation mode

Assembly only depend abstract interface, without the need to rely on specific implementation, when it is implanted can be embodied

The advantage of this is that you can decide on the specific implementation in use, it means that the future can do any extension, replace specific dependency injection framework

By default, it built using .NET Core provided dependency injection framework, third-party can also be used to replace the default dependency injection framework implemented

Core type

  • IServiceCollection: registration services
  • ServiceDescriptor: information of each service registered
  • IServiceProvider: specific container, produced by ServiceCollection Build
  • IServiceScope: child container life cycle of a container

Life cycle

  • Singleton the Singleton: throughout the life cycle of the root containers are single cases, either root or child container vessel, with the scope of the difference between: a global, one embodiment is a single range
  • Scope Scoped: Scope in the life cycle, that is, within the life cycle life cycle within the container or child container, the container if I relieved my objects will relieve
  • Transient (temporary) Transient: Every time when obtaining objects, you can get a brand new object from inside the container

Create a new ASP.NET Core Web Application DependencyInjectionDemo, select API

Adding a Services folder, create three representatives of the three service lifecycle services

namespace DependencyInjectionDemo.Services
{
    public interface IMyScopedService { }
    public class MyScopedService : IMyScopedService
    {
    }
}
namespace DependencyInjectionDemo.Services
{
    public interface IMySingletonService { }
    public class MySingletonService : IMySingletonService
    {
    }
}
namespace DependencyInjectionDemo.Services
{
    public interface IMyTransientService { }
    public class MyTransientService : IMyTransientService
    {
    }
}

In the Startup Register Service

public void ConfigureServices(IServiceCollection services)
{
    #region 注册服务不同生命周期的服务

    // 将单例的服务注册为单例的模式
    services.AddSingleton<IMySingletonService, MySingletonService>();

    // Scoped 的服务注册为 Scoped 的生命周期
    services.AddScoped<IMyScopedService, MyScopedService>();

    // 瞬时的服务注册为瞬时的生命周期
    services.AddTransient<IMyTransientService, MyTransientService>();

    #endregion

    services.AddControllers();
}

Get our service inside the Controller

// FromServices 标注的作用是从容器里面获取我们的对象
// 每个对象获取两遍,用于对比每个生命周期获取的对象是什么样子的
// HashCode 代表对象的唯一性
[HttpGet]
public int GetService(
    [FromServices]IMySingletonService singleton1,
    [FromServices]IMySingletonService singleton2,
    [FromServices]IMyTransientService transient1,
    [FromServices]IMyTransientService transient2,
    [FromServices]IMyScopedService scoped1,
    [FromServices]IMyScopedService scoped2)
{
    Console.WriteLine($"singleton1:{singleton1.GetHashCode()}");
    Console.WriteLine($"singleton2:{singleton2.GetHashCode()}");
    Console.WriteLine($"transient1:{transient1.GetHashCode()}");
    Console.WriteLine($"transient2:{transient2.GetHashCode()}");
    Console.WriteLine($"scoped1:{scoped1.GetHashCode()}");
    Console.WriteLine($"scoped2:{scoped2.GetHashCode()}");
    Console.WriteLine($"========请求结束========");
    return 1;
}

Start the program, refresh your browser again to access interface, the output is as follows:

Singleton not changed twice HashCode

Two instantaneous service twice HashCode completely different, which means instantaneous service each request will get a new object

Within each range the service request is the same object instance obtained between different requests of different

Creative Commons License

This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.

Welcome to reprint, use, repost, but be sure to keep the article signed by Zheng Ziming (containing links: http://www.cnblogs.com/MingsonZheng/), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification .

If you have any questions, please contact me ([email protected]).

Guess you like

Origin www.cnblogs.com/MingsonZheng/p/12324549.html