seesharper/LightInject

Is Register with PerContainerLifetime the same as RegisterInstance

githugt opened this issue · 2 comments

Till now we used to use something like this to get my Database session factory

((IServiceRegistry) serviceRegistry).RegisterInstance(typeof(ISessionFactory), StaticFunctionThatGivesSessionFactory());

But now I'd like to change this static function to one to which I can provide objects from the container to.
i.e

((IServiceRegistry) serviceRegistry).Register(factory => FunctionThatGivesSessionFactory( factory.GetInstance<MyCustomObject>()), new PerContainerLifetime());

using the LightInject function
IServiceRegistry.<TService>(Func<IServiceFactory, TService> factory, ILifetime lifetime);

I believe RegisterInstance provides a constant object.
While PerContainerLifetime scope means that the object is alive for the duration that the container is live (which in my case is the entire lifetime of my application). From my understanding, they are both the same. But I'm unsure.
Do both functions yield the same object? Or are there additional problems/nuances that I should be aware of?

They are pretty much the same. The only difference is the RegisterInstance lets you register an already instantiated object as a singleton with the container. If you would like the container to create the instance, you will register the service with the PerContainerLifetime. Or you can use the RegisterSingleton which is the same 👍

Awesome. Thanks for the clarification.
PS: Your library has been amazing. 🥇