如何理解.netcore添加作用域

问题描述

我正在尝试根据文档来了解实现或工作原理的逻辑。我正在创建.NET Core API

services.AddScoped ();
文档指出:

添加TService中指定类型的范围服务,并在其中添加 TImplementation中指定的实现类型 IServiceCollection。

public static IServiceCollection AddScoped<TService,TImplementation>(this IServiceCollection services)
    where TService : class
    where TImplementation : class,TService;

我想知道什么是TService。什么是实施。因为如果我第一次看到它,我不知道它的意思。

sqlCommanderRepo实现ICommanderRepo接口。

public class sqlCommanderRepo : ICommanderRepo
{
    private readonly CommanderContext _context;

    public sqlCommanderRepo(CommanderContext context)
    {
        _context = context;
    }

}

CommanderContext扩展了DbContext

public class CommanderContext : DbContext
{
    public CommanderContext(DbContextOptions<CommanderContext> opt) : base(opt)
    {

    }
    
}

解决方法

什么是TService。什么是实施?

TService:要添加的服务的类型。

根据您的情况,服务合同为ICommanderRepo

TImplementation:要使用的实现类型。

根据您的情况实现服务的具体类型为SqlCommanderRepo



AddScoped<TService,TImplementation>ServiceCollectionServiceExtensions的源代码中,将TService中指定类型的范围服务和TImplementation中指定的实现类型添加到指定的IServiceCollection。

        /// <summary>
        /// Adds a scoped service of the type specified in <typeparamref name="TService"/> with an
        /// implementation type specified in <typeparamref name="TImplementation"/> to the
        /// specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The type of the service to add.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <seealso cref="ServiceLifetime.Scoped"/>
        public static IServiceCollection AddScoped<TService,TImplementation>(this IServiceCollection services)
            where TService : class
            where TImplementation : class,TService
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            return services.AddScoped(typeof(TService),typeof(TImplementation));
        }

        /// <summary>
        /// Adds a scoped service of the type specified in <paramref name="serviceType"/> to the
        /// specified <see cref="IServiceCollection"/>.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="serviceType">The type of the service to register and the implementation to use.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <seealso cref="ServiceLifetime.Scoped"/>
        public static IServiceCollection AddScoped(
            this IServiceCollection services,Type serviceType)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            return services.AddScoped(serviceType,serviceType);
        }

注意:

Dependency injection in ASP.NET Core

  • 瞬态对象总是不同的;提供了一个新实例 每个控制器和每个服务。
  • 范围内的对象在请求内是相同的,但在不同对象之间是不同的 不同的请求。
  • 每个对象和每个请求的单个对象都是相同的。