var container = new TinyIoCContainer();
container.Register<ICar, ChittyBangBang>();
container.Register<ICar, Herbie>("Beetle");
var actual = container
.ResolveBuilder()
.WithOverload<ICar>("car", "Beetle")
.Resolve<IGarage>();
Created
November 11, 2013 15:28
-
-
Save adbre/7414956 to your computer and use it in GitHub Desktop.
ResolveBuilder for TinyIoC
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
using System; | |
namespace TinyIoC | |
{ | |
public class ResolveBuilder | |
{ | |
private readonly NamedParameterOverloads _overloads = new NamedParameterOverloads(); | |
private readonly TinyIoCContainer _container; | |
private ResolveOptions _options; | |
private string _name; | |
public ResolveBuilder(TinyIoCContainer container) | |
{ | |
if (container == null) throw new ArgumentNullException("container"); | |
_container = container; | |
} | |
public ResolveBuilder WithOverload(string parameterName, object overload) | |
{ | |
_overloads.Add(parameterName, overload); | |
return this; | |
} | |
public ResolveBuilder WithOverload<T>(string parameterName) | |
where T : class | |
{ | |
_overloads.Add(parameterName, _container.Resolve<T>()); | |
return this; | |
} | |
public ResolveBuilder WithOverload<T>(string parameterName, string name) | |
where T : class | |
{ | |
_overloads.Add(parameterName, _container.Resolve<T>(name)); | |
return this; | |
} | |
public ResolveBuilder WithOptions(ResolveOptions options) | |
{ | |
_options = options; | |
return this; | |
} | |
public ResolveBuilder ClearOptions() | |
{ | |
_options = null; | |
return this; | |
} | |
public ResolveBuilder WithUnregisteredResolutionAction(UnregisteredResolutionActions action) | |
{ | |
var options = GetOptions(); | |
options.UnregisteredResolutionAction = action; | |
return this; | |
} | |
public ResolveBuilder WithNamedResolutionFailureAction(NamedResolutionFailureActions action) | |
{ | |
var options = GetOptions(); | |
options.NamedResolutionFailureAction = action; | |
return this; | |
} | |
public ResolveBuilder AsNamed(string name) | |
{ | |
if (name == null) throw new ArgumentNullException("name"); | |
_name = name; | |
return this; | |
} | |
public ResolveBuilder AsUnnamed() | |
{ | |
_name = null; | |
return this; | |
} | |
public T Resolve<T>() where T : class | |
{ | |
var options = _options; | |
var name = _name; | |
if (name != null && options != null) | |
return _container.Resolve<T>(name, _overloads, options); | |
if (name != null && options == null) | |
return _container.Resolve<T>(name, _overloads); | |
if (name == null && options != null) | |
return _container.Resolve<T>(_overloads, options); | |
return _container.Resolve<T>(_overloads); | |
} | |
private ResolveOptions GetOptions() | |
{ | |
return _options ?? (_options = new ResolveOptions()); | |
} | |
} | |
public static class TinyIoCContainerExtensions | |
{ | |
public static ResolveBuilder ResolveBuilder(this TinyIoCContainer container) | |
{ | |
if (container == null) throw new ArgumentNullException("container"); | |
return container.Resolve<ResolveBuilder>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment