Added shorthands methods for no-interface service types
All checks were successful
Nuget Pkg Build / build (push) Successful in 42s
All checks were successful
Nuget Pkg Build / build (push) Successful in 42s
This commit is contained in:
@@ -24,8 +24,9 @@ public class ServiceContainer {
|
|||||||
/// <typeparam name="TInterface">Interface the service is implementing</typeparam>
|
/// <typeparam name="TInterface">Interface the service is implementing</typeparam>
|
||||||
/// <typeparam name="TImplementation">Implementation type of the service</typeparam>
|
/// <typeparam name="TImplementation">Implementation type of the service</typeparam>
|
||||||
public ServiceContainer AddSingleton<TInterface, TImplementation>()
|
public ServiceContainer AddSingleton<TInterface, TImplementation>()
|
||||||
|
where TInterface : class
|
||||||
where TImplementation : class, TInterface {
|
where TImplementation : class, TInterface {
|
||||||
descriptors.Add(new ServiceDescriptor {
|
descriptors.Add(new () {
|
||||||
ServiceType = typeof(TInterface),
|
ServiceType = typeof(TInterface),
|
||||||
ImplementationType = typeof(TImplementation),
|
ImplementationType = typeof(TImplementation),
|
||||||
Lifetime = ServiceLifetime.Lifetime
|
Lifetime = ServiceLifetime.Lifetime
|
||||||
@@ -33,14 +34,29 @@ public class ServiceContainer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a singleton service where the service type is the same as the implementation type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TClass">Class type of the service</typeparam>
|
||||||
|
public ServiceContainer AddSingleton<TClass>()
|
||||||
|
where TClass : class {
|
||||||
|
descriptors.Add(new () {
|
||||||
|
ServiceType = typeof(TClass),
|
||||||
|
ImplementationType = typeof(TClass),
|
||||||
|
Lifetime = ServiceLifetime.Lifetime
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a transient service with its implementation.
|
/// Registers a transient service with its implementation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TInterface">Interface the service is implementing</typeparam>
|
/// <typeparam name="TInterface">Interface the service is implementing</typeparam>
|
||||||
/// <typeparam name="TImplementation">Implementation type of the service</typeparam>
|
/// <typeparam name="TImplementation">Implementation type of the service</typeparam>
|
||||||
public ServiceContainer AddTransient<TInterface, TImplementation>()
|
public ServiceContainer AddTransient<TInterface, TImplementation>()
|
||||||
|
where TInterface : class
|
||||||
where TImplementation : class, TInterface {
|
where TImplementation : class, TInterface {
|
||||||
descriptors.Add(new ServiceDescriptor {
|
descriptors.Add(new () {
|
||||||
ServiceType = typeof(TInterface),
|
ServiceType = typeof(TInterface),
|
||||||
ImplementationType = typeof(TImplementation),
|
ImplementationType = typeof(TImplementation),
|
||||||
Lifetime = ServiceLifetime.Transient
|
Lifetime = ServiceLifetime.Transient
|
||||||
@@ -48,6 +64,20 @@ public class ServiceContainer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a transient service where the service type is the same as the implementation type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TClass">Class type of the service</typeparam>
|
||||||
|
public ServiceContainer AddTransient<TClass>()
|
||||||
|
where TClass : class {
|
||||||
|
descriptors.Add(new () {
|
||||||
|
ServiceType = typeof(TClass),
|
||||||
|
ImplementationType = typeof(TClass),
|
||||||
|
Lifetime = ServiceLifetime.Transient
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
// you can't call generic methods with an unknown type at compile time
|
// you can't call generic methods with an unknown type at compile time
|
||||||
// so we use reflection to call the generic GetService<T> method with the provided type
|
// so we use reflection to call the generic GetService<T> method with the provided type
|
||||||
// Basically we build the method GetService<serviceType>() at runtime and then call it.
|
// Basically we build the method GetService<serviceType>() at runtime and then call it.
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Version>0.0.1.3-alpha</Version>
|
<Version>0.0.1.4-alpha</Version>
|
||||||
<Title>Syrette </Title>
|
<Title>Syrette </Title>
|
||||||
<Authors>Lorefice Samuele</Authors>
|
<Authors>Lorefice Samuele</Authors>
|
||||||
<Description>Syrette is a minimalistic dependency injection library for C#. It aims to provide a simple and efficient way to achieve dependency injections in your applications without the overhead of larger frameworks.</Description>
|
<Description>Syrette is a minimalistic dependency injection library for C#. It aims to provide a simple and efficient way to achieve dependency injections in your applications without the overhead of larger frameworks.</Description>
|
||||||
|
|||||||
Reference in New Issue
Block a user