-
Notifications
You must be signed in to change notification settings - Fork 75
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
One concrete class for multiple singletons #196
Comments
@Narayane can you just register it with a tag? When it is needed just cast it to how you will use it? |
@iAmNaz Not sure to understand your words, can you give an example relative to my case. I think my aim is a little bit different, if i understand well named definitions examples. I don't want to have different implementations for a given protocol (dependant of an environment or whatever). I want to be sure thanks to Dip than only one instance of a concrete class will be used to resolve 3 different singletons |
Assuming this is your concrete class, correct? Registration: Usage: Use your datasource as normal and if you need it to be of some protocol then just cast it |
@Narayane try to use type forwarding for this - https://github.com/AliSoftware/Dip/wiki/type-forwarding |
@iAmNaz named definitions should be used to distinguish registrations of the same type which return different implementations depending on label, in this case its required to resolve different types to the same instance, so even if named definitions would work that would be a not a good solution IMO |
We have a solution to perform registartion chaining.
with another scope:
Implementation:
|
Hi,
I have created 3 protocols for evolutivity and testability reasons. For now, I have only one concrete class which implements these protocols.
I declare this in my DI configuration:
self.register(.singleton) { MyDataSource() as AuthProtocol }
self.register(.singleton) { MyDataSource() as RealTimeDatabaseProtocol }
self.register(.singleton) { MyDataSource() as FileStorageProtocol }
Will MyDataSource be instantiated only one time by Dip?
If not, how can I do this?
Thanks.
The text was updated successfully, but these errors were encountered: