private ComponentRegistration <T> WithLifespan <T>( ComponentRegistration <T> componentRegistration, Lifespan lifespan) where T : class { if (lifespan == Lifespan.Singleton) { return(componentRegistration.LifestyleSingleton()); } else if (lifespan == Lifespan.Transient) { return(componentRegistration.LifeStyle.Transient); } else if (lifespan is Lifespan.LifespanTypeBound typeBound) { var method = componentRegistration.GetType().GetMethod("LifestyleBoundTo", new Type[] { }); var genericMethod = method.MakeGenericMethod(typeBound.BoundType); return((ComponentRegistration <T>)genericMethod.Invoke( componentRegistration, new object[0])); } else { throw new ArgumentException("invalid lifespan"); } }
public IContainer Register <T>(T value, Lifespan lifespan, string name) where T : class { _container.Register( WithLifespan( WithName(Component.For <T>().Instance(value), name), lifespan)); return(this); }
public IContainer Register <T>(Func <IContainer, T> factory, Lifespan lifespan, string name) where T : class { _container.Register( WithLifespan( WithName(Component.For <T>().UsingFactoryMethod(kernel => factory.Invoke(this)), name), lifespan)); return(this); }
// these methods are provided as a convenience ... we cannot abstract // away all facets of an IoC container, so we have taken a somewhat // opinionated view of the IoC container. we may abstract away other // parts at a later time. public IContainer Register <T, TImpl>(Lifespan lifespan, string name) where T : class where TImpl : T { _container.Register( WithLifespan( WithName(Component.For <T>().ImplementedBy <TImpl>(), name), lifespan)); return(this); }