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>();
|
||||
|
||||
/// <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));
|
||||
}
|
||||
|
||||
/// <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/>
|
||||
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/>
|
||||
public object Resolve(Type serviceInterface)
|
||||
{
|
||||
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)
|
||||
return registeredType.Instance;
|
||||
@ -37,22 +45,23 @@ namespace SmallInjector
|
||||
{
|
||||
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
||||
}
|
||||
|
||||
var instance = constructor.Invoke(constructorParameters);
|
||||
return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
||||
}
|
||||
|
||||
private class RegisteredType
|
||||
{
|
||||
public bool IsSingleton { get; }
|
||||
public Type ServiceType { get; }
|
||||
public object Instance { get; set; }
|
||||
|
||||
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
||||
{
|
||||
ServiceType = serviceType;
|
||||
IsSingleton = isSingleton;
|
||||
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.
|
||||
/// </summary>
|
||||
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