I am looking for a way that I can register a concrete type with a specified Lifestyle, essentially something like the following.
public void SomeFunction( Type concrete, Lifestyle lifestyle ) =>
container.Register( concrete, lifestyle );
When it comes to making a single one-to-one mapping in Simple Injector, there is practically just one method:
Container.Register(Type serviceType, Type implementationType, Lifestyle lifestyle);
All other methods are just convenient overloads or 'shortcuts' to this method. The following method for instance:
Container.Register<TService, TImplementation>(Lifestyle)
Eventually falls back to the non-generic overload by calling Register(typeof(TService), typeof(TImplementation), lifestyle)
.
The same holds for overloads that don't take in a Lifestyle
:
Container.Register<TService, TImplementation>()
They just forward the call by supplying the determined lifestyle for the given implementation type, which is -under default configuration- is the transient lifestyle: Register<TService, TImpementation>(Lifestyle.Transient)
.
And there are multiple overloads that allow a shortcut registration for concrete types, such as:
Container.Register<TConcrete>()
This method forwards the call to Register<TConcrete, TConcrete>()
. In other words, TConcrete
is both supplied for TService
and TImplementation
. So eventually, this call ends up as Register(typeof(TConcrete), typeof(TConcrete), Lifestyle.Transient)
.
So, long story short, the following methods allow you to register a concrete type with a lifestyle:
Register<TConcrete>(Lifestyle.Scoped)
Register<TConcrete, TConcrete>(Lifestyle.Scoped)
Register(typeof(Concrete), typeof(Concrete), Lifestyle.Scoped);