-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
*.DotSettings.user | ||
.idea/ | ||
.experiments/ | ||
nuget.config | ||
nuget.config | ||
/.vs/tone |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using Sandreas.SpectreConsoleHelpers.DependencyInjection; | ||
|
||
using Spectre.Console.Cli; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace tone.SpectreConsoleHelpers; | ||
|
||
public class ToneCustomTypeRegistrar : ITypeRegistrar | ||
{ | ||
private readonly IServiceCollection _builder; | ||
|
||
public ToneCustomTypeRegistrar(IServiceCollection builder) | ||
{ | ||
_builder = builder; | ||
} | ||
|
||
public ITypeResolver Build() | ||
{ | ||
var resolver = new ToneCustomTypeResolver(); | ||
_builder.AddSingleton<ITypeResolver>(resolver); | ||
var sp = _builder.BuildServiceProvider(); | ||
resolver.Provider = sp; | ||
return resolver; | ||
} | ||
|
||
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) | ||
{ | ||
Func<object> func2 = func; | ||
if (func2 == null) | ||
{ | ||
throw new ArgumentNullException("func"); | ||
} | ||
|
||
_builder.AddSingleton(service, (IServiceProvider _) => func2()); | ||
} | ||
} | ||
|
||
public sealed class ToneCustomTypeResolver : ITypeResolver, IDisposable | ||
{ | ||
private IServiceProvider _provider; | ||
|
||
public IServiceProvider Provider | ||
{ | ||
get | ||
{ | ||
return _provider; | ||
} | ||
set | ||
{ | ||
if (value == null) | ||
{ | ||
throw new ArgumentNullException("value"); | ||
} | ||
|
||
if (_provider != null) | ||
{ | ||
throw new InvalidOperationException("Provider has already been set."); | ||
} | ||
|
||
_provider = value; | ||
} | ||
} | ||
|
||
public ToneCustomTypeResolver() | ||
{ | ||
} | ||
|
||
public object? Resolve(Type? type) | ||
{ | ||
if (!(type == null)) | ||
{ | ||
if (type == typeof(ITypeResolver)) | ||
{ | ||
return this; | ||
} | ||
|
||
return _provider.GetService(type); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
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