Skip to content

Commit

Permalink
Merge pull request #82 from NerosoftDev/develop
Browse files Browse the repository at this point in the history
Keyed service supported of LazyServiceProvider.
  • Loading branch information
Codespilot authored Sep 10, 2024
2 parents 9a79bb6 + 669d91f commit 0968458
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 141 deletions.
186 changes: 127 additions & 59 deletions Source/Euonia.Modularity/Dependency/ILazyServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,131 @@
/// </summary>
public interface ILazyServiceProvider
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T GetRequiredService<T>();

/// <summary>
///
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
object GetRequiredService(Type serviceType);

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T GetService<T>();

/// <summary>
///
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
object GetService(Type serviceType);

/// <summary>
///
/// </summary>
/// <param name="defaultValue"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T GetService<T>(T defaultValue);

/// <summary>
///
/// </summary>
/// <param name="serviceType"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
object GetService(Type serviceType, object defaultValue);

/// <summary>
///
/// </summary>
/// <param name="serviceType"></param>
/// <param name="factory"></param>
/// <returns></returns>
object GetService(Type serviceType, Func<IServiceProvider, object> factory);

/// <summary>
///
/// </summary>
/// <param name="factory"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T GetService<T>(Func<IServiceProvider, object> factory);
/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <returns></returns>
T GetRequiredService<T>();

/// <summary>
/// Get service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
object GetRequiredService(Type serviceType);

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <returns></returns>
T GetService<T>();

/// <summary>
/// Get service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
object GetService(Type serviceType);

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="defaultValue"></param>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <returns></returns>
T GetService<T>(T defaultValue);

/// <summary>
///
/// </summary>
/// <param name="serviceType"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
object GetService(Type serviceType, object defaultValue);

/// <summary>
/// Get service of <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType"></param>
/// <param name="factory"></param>
/// <returns></returns>
object GetService(Type serviceType, Func<IServiceProvider, object> factory);

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="factory"></param>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <returns></returns>
T GetService<T>(Func<IServiceProvider, object> factory);

/// <summary>
/// Get service of <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns></returns>
object GetKeyedService(Type serviceType, object serviceKey);

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns></returns>
T GetRequiredKeyedService<T>(object serviceKey);

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns></returns>
T GetKeyedService<T>(object serviceKey);

/// <summary>
/// Get service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType"></param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns></returns>
object GetRequiredKeyedService(Type serviceType, object serviceKey);

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <param name="defaultValue"></param>
/// <returns></returns>
T GetKeyedService<T>(object serviceKey, T defaultValue);

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <param name="factory"></param>
/// <returns></returns>
T GetKeyedService<T>(object serviceKey, Func<IServiceProvider, T> factory);

/// <summary>
/// Get service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType"></param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <param name="factory"></param>
/// <returns></returns>
object GetKeyedService(Type serviceType, object serviceKey, Func<IServiceProvider, object> factory);

/// <summary>
/// Get service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType"></param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <param name="defaultValue"></param>
/// <returns></returns>
object GetKeyedService(Type serviceType, object serviceKey, object defaultValue);
}
55 changes: 55 additions & 0 deletions Source/Euonia.Modularity/Dependency/LazyServiceProvider.Keyed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Extensions.DependencyInjection;
using Nerosoft.Euonia.Modularity;

namespace System;

public partial class LazyServiceProvider
{
/// <inheritdoc />
public virtual T GetKeyedService<T>(object serviceKey)
{
return (T)GetKeyedService(typeof(T), serviceKey);
}

/// <inheritdoc />
public virtual object GetKeyedService(Type serviceType, object serviceKey)
{
return CachedServices.GetOrAdd(new ServiceIdentifier(serviceKey, serviceType), _ => new Lazy<object>(() => ServiceProvider.GetKeyedService(serviceType, serviceKey))).Value;
}

/// <inheritdoc />
public virtual T GetRequiredKeyedService<T>(object serviceKey)
{
return (T)GetRequiredKeyedService(typeof(T), serviceKey);
}

/// <inheritdoc />
public virtual object GetRequiredKeyedService(Type serviceType, object serviceKey)
{
return CachedServices.GetOrAdd(new ServiceIdentifier(serviceKey, serviceType), _ => new Lazy<object>(() => ServiceProvider.GetRequiredKeyedService(serviceType, serviceKey))).Value!;
}

/// <inheritdoc />
public virtual T GetKeyedService<T>(object serviceKey, T defaultValue)
{
return (T)GetKeyedService(typeof(T), serviceKey, defaultValue);
}

/// <inheritdoc />
public virtual T GetKeyedService<T>(object serviceKey, Func<IServiceProvider, T> factory)
{
return (T)GetKeyedService(typeof(T), serviceKey, provider => factory(provider));
}

/// <inheritdoc />
public virtual object GetKeyedService(Type serviceType, object serviceKey, Func<IServiceProvider, object> factory)
{
return CachedServices.GetOrAdd(new ServiceIdentifier(serviceKey, serviceType), _ => new Lazy<object>(() => factory(ServiceProvider))).Value;
}

/// <inheritdoc />
public virtual object GetKeyedService(Type serviceType, object serviceKey, object defaultValue)
{
return CachedServices.GetOrAdd(new ServiceIdentifier(serviceKey, serviceType), _ => new Lazy<object>(() => defaultValue)).Value;
}
}
Loading

0 comments on commit 0968458

Please sign in to comment.