mirror of
https://github.com/greenshot/greenshot
synced 2025-08-21 05:53:27 -07:00
Merge branch 'extend_service_provider' into feature/new_greenshot_file_format_v2
This commit is contained in:
commit
734abac175
2 changed files with 51 additions and 0 deletions
|
@ -12,8 +12,12 @@ namespace Greenshot.Base.Core
|
|||
{
|
||||
private readonly Dictionary<Type, IList<object>> _services = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current instance of the service locator.
|
||||
/// </summary>
|
||||
public static IServiceLocator Current { get; } = new SimpleServiceProvider();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IReadOnlyList<TService> GetAllInstances<TService>()
|
||||
{
|
||||
var typeOfService = typeof(TService);
|
||||
|
@ -25,11 +29,13 @@ namespace Greenshot.Base.Core
|
|||
return results.Cast<TService>().ToArray();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TService GetInstance<TService>()
|
||||
{
|
||||
return GetAllInstances<TService>().SingleOrDefault();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void AddService<TService>(IEnumerable<TService> services)
|
||||
{
|
||||
var serviceType = typeof(TService);
|
||||
|
@ -50,9 +56,40 @@ namespace Greenshot.Base.Core
|
|||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void AddService<TService>(params TService[] services)
|
||||
{
|
||||
AddService(services.AsEnumerable());
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RemoveService<TService>(IEnumerable<TService> services)
|
||||
{
|
||||
var serviceType = typeof(TService);
|
||||
if (!_services.TryGetValue(serviceType, out var currentServices))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var service in services)
|
||||
{
|
||||
if (service == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
currentServices.Remove(service);
|
||||
}
|
||||
|
||||
if (currentServices.Count == 0)
|
||||
{
|
||||
_services.Remove(serviceType);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RemoveService<TService>(params TService[] services)
|
||||
{
|
||||
RemoveService(services.AsEnumerable());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -54,5 +54,19 @@ namespace Greenshot.Base.Interfaces
|
|||
/// <typeparam name="TService">Type of the service</typeparam>
|
||||
/// <param name="services">IEnumerable{TService} with services to add</param>
|
||||
void AddService<TService>(IEnumerable<TService> services);
|
||||
|
||||
/// <summary>
|
||||
/// Remove one or more services from the registry
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">Type of the service</typeparam>
|
||||
/// <param name="services">One or more services which need to be removed</param>
|
||||
void RemoveService<TService>(params TService[] services);
|
||||
|
||||
/// <summary>
|
||||
/// Remove multiple services from the registry
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">Type of the service</typeparam>
|
||||
/// <param name="services">IEnumerable{TService} with services to remove</param>
|
||||
void RemoveService<TService>(IEnumerable<TService> services);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue