-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keyed service supported of LazyServiceProvider.
- Loading branch information
1 parent
8c340fb
commit 669d91f
Showing
5 changed files
with
335 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Source/Euonia.Modularity/Dependency/LazyServiceProvider.Keyed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.