Skip to content

Allow for UnitOfWork Scoping #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kmanpro opened this issue Oct 8, 2019 · 3 comments
Closed

Allow for UnitOfWork Scoping #25

kmanpro opened this issue Oct 8, 2019 · 3 comments

Comments

@kmanpro
Copy link

kmanpro commented Oct 8, 2019

I really like the simplicity of get_it and it's speed.
Thank you so much for creating and maintaining this product!

I am wondering if we can get the ability to do UnitOfWork scoping where even factories will be the same instance if used in the same UnitOfWork.

See the code below for the example:

Registration

GetIt.I.registerFactory<SharedService>(() => new SharedService());
GetIt.I.registerFactory<Service3>(() => new Service3(GetIt.I.get<SharedService>()));
GetIt.I.registerFactory<Service2>(() => new Service2(GetIt.I.get<SharedService>()));
GetIt.I.registerFactory<MainService>(() => new MainService(GetIt.I.get<Service2>(), GetIt.I.get<Service3>()));

Classes

class MainService(Service2 service2, Service3 service3)
class Service2(SharedService sharedService)
class Service3(SharedService sharedService)

Test

var service1 = GetIt.I.get<MainService>();
        expect(service1.service2.service4, same(service1.service3.service4));

Can SharedService be the same instance in this unitOfWork without making it a singleton?

@escamoteur
Copy link
Collaborator

Sure, just use registerSingleton instead of registerFactory or why would this be a problem?

@kmanpro
Copy link
Author

kmanpro commented Oct 23, 2019

Thanks for your response, and time on this issue!

I was looking for a scoped lifetime on services, if possible. More than just Singleton/Factory.
I don't need SharedService to be a singleton globally, but I do need it to be a scoped singleton for a certain process.

Example (click button and start a new unit of work processing in the background. In the scope of that unit of work the SharedService is a singleton. Once that process is done the object should be disposed and the next time the process is ran it should new up a new SharedService.)

Hope that makes sense. I am coming from a C# web background and am used to scoping my Dependency Injection to the current request using StructureMap. So perhaps it is just me applying web concepts to my mobile app incorrectly ;).

I of course could handle this by making SharedService always a global singleton and reset the state of it every time one of these ends but like the clean ability to scope my services without passing them around to each other.

I'll close it for now since it probably doesn't make much sense in this context.

Thanks!

@kmanpro kmanpro closed this as completed Oct 23, 2019
@escamoteur
Copy link
Collaborator

Hmmm, perhaps it might interest you that the next version will have a reset for Lazy Singleton so this might be a solution.
I'm still pondering on when a scoped injection would be needed because I didn't have the requirement yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants