Reconsider initialization behavior of RegisterSingleton<TService>(TService) #353

Open
dotnetjunkie opened this Issue Dec 18, 2016 · 2 comments

Projects

None yet

2 participants

@dotnetjunkie
Collaborator

Considering the following code:

container.RegisterSingleton<IService>(new MyService());
container.RegisterInitializer<MyService>(c => c.ConnectionString == "foobar");

Simple Injector v3 will not run the initializer for MyService when resolving IService, even though it has all the type information available to do this.

Reason for this is to keep the behavior identical to the following:

container.Register<IService>(() => new MyService());
container.RegisterInitializer<MyService>(c => c.ConnectionString == "foobar");

With the registration of the delegate, Simple Injector does not have the information of the implementation type available and will never be able to run the initializer.

But take a look at the following registration:

container.Register<IService, MyService>();
container.RegisterInitializer<MyService>(c => c.ConnectionString == "foobar");

Here Simple Injector actually has the type information available and this will cause the initializer to run on MyService.

Question now becomes: how should RegisterSingleton<TService>(TService) behave: Like Register<TService>(Func<TService>) or like Register<TService, TImplementation>()?

Should we change the v3 behavior in v4?

@dotnetjunkie dotnetjunkie added this to the Backlog milestone Dec 18, 2016
@dotnetjunkie dotnetjunkie modified the milestone: v4, Backlog Dec 29, 2016
@dotnetjunkie
Collaborator

The current behavior in v3 is very inconsistent. While some code paths use the service type for the creation of such singleton, other features use the implementation type.

@TheBigRic

I'm having a hard time understanding the problem and the question...

The current behavior in v3 is very inconsistent.

Only thing I can say at this moment is that inconsistency should be prevented. On the other hand, I never came across questions regarding this, so apparently everything works well enough from a users perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment