added new methods to get information about already registered types
This commit is contained in:
parent
8954cd82fc
commit
93f1a7ab63
@ -11,22 +11,30 @@ namespace SmallInjector
|
|||||||
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
|
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
|
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default)
|
||||||
|
where TService : TInterface
|
||||||
{
|
{
|
||||||
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton, null));
|
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void RegisterType<TService>(bool isSingleton, TService instance = default) => RegisterType<TService, TService>(isSingleton, instance);
|
public void RegisterType<TService>(bool isSingleton, TService instance = default) =>
|
||||||
|
RegisterType<TService, TService>(isSingleton, instance);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
|
public TService Resolve<TService>() => (TService) Resolve(typeof(TService));
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool IsRegistered<TService>() => IsRegistered(typeof(TService));
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool IsRegistered(Type serviceType) => _container.ContainsKey(serviceType);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public object Resolve(Type serviceInterface)
|
public object Resolve(Type serviceInterface)
|
||||||
{
|
{
|
||||||
if (!_container.TryGetValue(serviceInterface, out var registeredType))
|
if (!_container.TryGetValue(serviceInterface, out var registeredType))
|
||||||
throw new Exception("Can't resolve dependency " + serviceInterface);
|
throw new Exception(@"Dependency {serviceInterface} is not registered");
|
||||||
|
|
||||||
if (registeredType.IsSingleton && registeredType.Instance != null)
|
if (registeredType.IsSingleton && registeredType.Instance != null)
|
||||||
return registeredType.Instance;
|
return registeredType.Instance;
|
||||||
@ -37,22 +45,23 @@ namespace SmallInjector
|
|||||||
{
|
{
|
||||||
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
||||||
}
|
}
|
||||||
|
|
||||||
var instance = constructor.Invoke(constructorParameters);
|
var instance = constructor.Invoke(constructorParameters);
|
||||||
return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class RegisteredType
|
private class RegisteredType
|
||||||
{
|
{
|
||||||
|
public bool IsSingleton { get; }
|
||||||
|
public Type ServiceType { get; }
|
||||||
|
public object Instance { get; set; }
|
||||||
|
|
||||||
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
||||||
{
|
{
|
||||||
ServiceType = serviceType;
|
ServiceType = serviceType;
|
||||||
IsSingleton = isSingleton;
|
IsSingleton = isSingleton;
|
||||||
Instance = instance;
|
Instance = instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly bool IsSingleton;
|
|
||||||
public readonly Type ServiceType;
|
|
||||||
public object Instance { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -30,5 +30,15 @@ namespace SmallInjector
|
|||||||
/// Resolve service of specified type.
|
/// Resolve service of specified type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
TService Resolve<TService>();
|
TService Resolve<TService>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true, if the service is registered. False otherwise.
|
||||||
|
/// </summary>
|
||||||
|
bool IsRegistered<TService>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true, if the service is registered. False otherwise.
|
||||||
|
/// </summary>
|
||||||
|
bool IsRegistered(Type serviceType);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user