Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyed service supported of LazyServiceProvider. #82

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading