-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eased requirements for custom type resolvers
- Loading branch information
Showing
5 changed files
with
143 additions
and
13 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
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
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
67 changes: 67 additions & 0 deletions
67
...ToolKit.Spectre.AutoCompletion.Tests/Utilities/DependencyInjection/CustomTypeRegistrar.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,67 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace JKToolKit.Spectre.AutoCompletion.Tests.Utilities.DependencyInjection; | ||
|
||
public class CustomTypeRegistrar : ITypeRegistrar | ||
{ | ||
private readonly IServiceCollection _builder; | ||
|
||
public CustomTypeRegistrar(IServiceCollection builder) | ||
{ | ||
_builder = builder; | ||
} | ||
|
||
public ITypeResolver Build() | ||
{ | ||
return new CustomTypeResolver(_builder.BuildServiceProvider()); | ||
} | ||
|
||
public void Register(Type service, Type implementation) | ||
{ | ||
_builder.AddSingleton(service, implementation); | ||
} | ||
|
||
public void RegisterInstance(Type service, object implementation) | ||
{ | ||
_builder.AddSingleton(service, implementation); | ||
} | ||
|
||
public void RegisterLazy(Type service, Func<object> func) | ||
{ | ||
if (func is null) | ||
{ | ||
throw new ArgumentNullException(nameof(func)); | ||
} | ||
|
||
_builder.AddSingleton(service, _ => func()); | ||
} | ||
} | ||
|
||
public sealed class CustomTypeResolver : ITypeResolver, IDisposable | ||
{ | ||
private readonly IServiceProvider _provider; | ||
|
||
public CustomTypeResolver(IServiceProvider provider) | ||
{ | ||
_provider = provider ?? throw new ArgumentNullException(nameof(provider)); | ||
} | ||
|
||
public object? Resolve(Type? type) | ||
{ | ||
return type == null ? null : _provider.GetService(type); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_provider is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} |
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