This project is read-only.

auto-wiring and singleton

May 24, 2015 at 9:19 PM
Edited May 24, 2015 at 9:20 PM
Hi, I am new to DI and Simple injector. While reading through the tutorial I got some confusion with auto-wiring.
Note: Because UserService is a concrete type, calling container.GetInstance<UserService>() without registering it explicitly will work. This feature can significantly simplify the Container‘s configuration for more complex scenarios. Always keep in mind that best practice is to program to an interface not a concrete type. Prevent using and depending on concrete types as much possible.
So my question is: this approach will create a new instance each time when container.GetInstance<ConcreteType>() gets called or the instance is singleton?

Thanks
May 24, 2015 at 11:21 PM
Edited May 24, 2015 at 11:22 PM
When the container resolves an unregistered concrete type, it will use the container's Options.LifestyleSelectionBehavior to determine the lifestyle of such component. This means that, unless you override its default behaviour, such concrete type is registered as transient.

This paragraph in the documentation however, might need to be rewritten. Although it still correctly describes the correct technical behaviour, it is confusing and isn't in line with how we think Dependency Injection should be practiced and a DI container should be used. The reason for this is that although it can be really convenient to let the container auto-register a concrete type, you should never do this with a 'root type'. A root type is a type that is on the top of the object graph. Something is a root type if you ask the container for it directly, using container.GetInstance<T>() for instance. In an MVC application for instance, your Controller classes are root types, since they are the objects where the whole request starts with; they are requested directly from the container. All other objects are direct or indirect dependencies of controller types.

Thing is, you should always register all root types, such as your MVC controllers, even though Simple Injector could create them for you lazily. Reason is that you should want to verify your configuration. This verification however, would give falls positives in case you don't register your root types. That would make the verification process succeed, while your application will still fail at runtime.

So for that reason, that section in the documentation is misleading. Yes, Simple Injector will auto-register/resolve unregistered concrete types for you (as transient), but every concrete type that you resolve explicitly should also be registered explicitly up front.

btw. Did you know we moved to Github?
May 25, 2015 at 8:55 PM
Thanks a lot for the prompt reply! It clears all my confusion and is so helpful to understanding the best practice of using Simple Injection!

Sorry I don't know that you've moved to GitHub. My next thread will be posted there :).

Regards
May 26, 2015 at 8:58 AM
Good luck! We'll see you again on Github.