Skip to content

Instantly share code, notes, and snippets.

@adamped
Last active April 13, 2016 06:01
Show Gist options
  • Save adamped/82a60523d4390d60cd37617e698327e8 to your computer and use it in GitHub Desktop.
Save adamped/82a60523d4390d60cd37617e698327e8 to your computer and use it in GitHub Desktop.
public class Injection: IInjection
{
private static ContainerBuilder _builder = null;
private static IContainer Container { get; set; } = null;
private static IList<Type> _registered = new List<Type>();
public void Init()
{
_builder = new ContainerBuilder();
_builder.RegisterInstance<IInjection>(this).SingleInstance();
}
public void Complete()
{
Container = _builder.Build();
}
private void Register<T>(IRegistrationBuilder<T, ConcreteReflectionActivatorData, SingleRegistrationStyle> register, InstanceType type)
{
switch (type)
{
case InstanceType.EachResolve:
register.InstancePerDependency();
break;
case InstanceType.SingleInstance:
register.SingleInstance();
break;
default:
register.InstancePerDependency();
break;
}
}
public void Register<T>(InstanceType type) where T : class
{
Register(_builder.RegisterType<T>(), type);
_registered.Add(typeof(T));
}
public void RegisterInterface<I, T>(InstanceType type) where T : class, I
where I : class
{
Register(_builder.RegisterType<T>().As<I>(), type);
_registered.Add(typeof(I));
}
public T Get<T>() where T : class
{
return Container.Resolve<T>();
}
public object Get(Type type)
{
return Container.Resolve(type);
}
public bool IsRegistered<T>()
{
return _registered.Contains(typeof(T));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment