中 | EN
Install-Package Masa.Utils.Extensions.DependencyInjection
services.AddAutoInject();
- ISingletonDependency: registers a service whose lifecycle is Singleton
- IScopedDependency: registers a service whose lifecycle is Scoped
- ITransientDependency: registers services whose lifecycle is Transient
- IAutoFireDependency: is automatically triggered (used in combination with ISingletonDependency, IScopedDependency, and ITransientDependency to trigger a service acquisition operation after the service is automatically registered, only inheriting IAutoFireDependency does not work)
Example:
public interface IRepository<TEntity> : IScopedDependency
where TEntity : class
{
}
Because IRepository inherits IScopedDependency, the life cycle of IRepository will be Scoped
Scan the interfaces and classes that inherit ISingletonDependency, IScopedDependency, and ITransientDependency in the assembly, and automatically register services for them
-
When inheriting an interface, its ServiceType is the current interface, and its ImplementationType is the implementation class of the current interface
-
If the current interface has multiple implementation classes, it will be added multiple times
public interface IUserService : IScopedDependency { } public class UserService : IUserService { }
Equivalent to service.AddScoped<IUserService, UserService>();
-
If you want the interface to have only one implementation class, add [Dependency(ReplaceServices = true)] above the implementation class
public interface IUserService : IScopedDependency { } public class UserService : IUserService { } [Dependency(ReplaceServices = true)] public class UserService2 : IUserService { }
Equivalent to service.AddScoped<IUserService, UserService2>();
-
-
When the inherited class is not an interface, its ServiceType is the current class, and its ImplementationType is also the current class
-
By default, the cascade scan registration service is supported, and subclasses of the current class will also be registered
public class BaseRepository : ISingletonDependency { } /// <summary> /// Abstract classes are not automatically registered /// </summary> public abstract class CustomizeBaseRepository : ISingletonDependency { } public class UserRepository : BaseRepository { }
Equivalent to:
service.AddSingleton<BaseRepository>();service.AddSingleton<UserRepository>();
-
- IgnoreInjection: Ignore injection, used to exclude not being injected automatically
- Dependency:
- TryRegister: Set true to be registered only when the service is not registered, similar to TryAdd of IServiceCollection... extension method
- ReplaceServices: Set true to replace previously registered services, similar to the Replace... extension method of IServiceCollection.
- Extend IServiceCollection
- GetInstance(): Get the instance of service T
- Any(): Whether there is a service TService, does not support generic services
- Any<TService, TImplementation>(): Whether there is a service whose interface is TService and whose implementation class is TImplementation
- Any(ServiceLifetime.Singleton): Whether there is a service TService with a life cycle of Singleton (generic services are not supported)
- Any<TService, TImplementation>(ServiceLifetime.Singleton): Is there an interface whose life cycle is Singleton as TService and is implemented as a TImplementation service (generic services are not supported)
- Replace(typeof(TImplementation), ServiceLifetime.Singleton): Remove the first service with the same service type in the service collection, and add typeof(TImplementation) to the collection, the life cycle is a singleton
- ReplaceAll(typeof(TImplementation), ServiceLifetime.Singleton): Remove all services with the same service type in the service collection, and add typeof(TImplementation) to the collection, the life cycle is a singleton