动手造轮子:实现一个简单的依赖注入(一) (2)

为了使得服务注册更加方便,可以写一些扩展方法来方便注册:

public static IServiceContainer AddSingleton<TService>([NotNull]this IServiceContainer serviceContainer, [NotNull]TService service) { serviceContainer.Add(new ServiceDefinition(service, typeof(TService))); return serviceContainer; } public static IServiceContainer AddSingleton([NotNull]this IServiceContainer serviceContainer, [NotNull]Type serviceType) { serviceContainer.Add(new ServiceDefinition(serviceType, ServiceLifetime.Singleton)); return serviceContainer; } public static IServiceContainer AddSingleton([NotNull]this IServiceContainer serviceContainer, [NotNull]Type serviceType, [NotNull]Type implementType) { serviceContainer.Add(new ServiceDefinition(serviceType, implementType, ServiceLifetime.Singleton)); return serviceContainer; } public static IServiceContainer AddSingleton<TService>([NotNull]this IServiceContainer serviceContainer, [NotNull]Func<IServiceProvider, object> func) { serviceContainer.Add(ServiceDefinition.Singleton<TService>(func)); return serviceContainer; } public static IServiceContainer AddSingleton<TService>([NotNull]this IServiceContainer serviceContainer) { serviceContainer.Add(ServiceDefinition.Singleton<TService>()); return serviceContainer; } public static IServiceContainer AddSingleton<TService, TServiceImplement>([NotNull]this IServiceContainer serviceContainer) where TServiceImplement : TService { serviceContainer.Add(ServiceDefinition.Singleton<TService, TServiceImplement>()); return serviceContainer; } public static IServiceContainer AddScoped([NotNull]this IServiceContainer serviceContainer, [NotNull]Type serviceType) { serviceContainer.Add(new ServiceDefinition(serviceType, ServiceLifetime.Scoped)); return serviceContainer; } public static IServiceContainer AddScoped([NotNull]this IServiceContainer serviceContainer, [NotNull]Type serviceType, [NotNull]Type implementType) { serviceContainer.Add(new ServiceDefinition(serviceType, implementType, ServiceLifetime.Scoped)); return serviceContainer; } public static IServiceContainer AddScoped<TService>([NotNull]this IServiceContainer serviceContainer, [NotNull]Func<IServiceProvider, object> func) { serviceContainer.Add(ServiceDefinition.Scoped<TService>(func)); return serviceContainer; } public static IServiceContainer AddScoped<TService>([NotNull]this IServiceContainer serviceContainer) { serviceContainer.Add(ServiceDefinition.Scoped<TService>()); return serviceContainer; } public static IServiceContainer AddScoped<TService, TServiceImplement>([NotNull]this IServiceContainer serviceContainer) where TServiceImplement : TService { serviceContainer.Add(ServiceDefinition.Scoped<TService, TServiceImplement>()); return serviceContainer; } public static IServiceContainer AddTransient([NotNull]this IServiceContainer serviceContainer, [NotNull]Type serviceType) { serviceContainer.Add(new ServiceDefinition(serviceType, ServiceLifetime.Transient)); return serviceContainer; } public static IServiceContainer AddTransient([NotNull]this IServiceContainer serviceContainer, [NotNull]Type serviceType, [NotNull]Type implementType) { serviceContainer.Add(new ServiceDefinition(serviceType, implementType, ServiceLifetime.Transient)); return serviceContainer; } public static IServiceContainer AddTransient<TService>([NotNull]this IServiceContainer serviceContainer, [NotNull]Func<IServiceProvider, object> func) { serviceContainer.Add(ServiceDefinition.Transient<TService>(func)); return serviceContainer; } public static IServiceContainer AddTransient<TService>([NotNull]this IServiceContainer serviceContainer) { serviceContainer.Add(ServiceDefinition.Transient<TService>()); return serviceContainer; } public static IServiceContainer AddTransient<TService, TServiceImplement>([NotNull]this IServiceContainer serviceContainer) where TServiceImplement : TService { serviceContainer.Add(ServiceDefinition.Transient<TService, TServiceImplement>()); return serviceContainer; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzypyz.html