Skip to content
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

Tight coupling between the Pointer and Provider #10

Open
rdadbhawala-cmspl opened this issue Jan 17, 2018 · 4 comments
Open

Tight coupling between the Pointer and Provider #10

rdadbhawala-cmspl opened this issue Jan 17, 2018 · 4 comments

Comments

@rdadbhawala-cmspl
Copy link

I'm not sure if I've read the examples right, but it appears that there may be a tight coupling between the Pointer variable and the Provider. When creating a definition in a graph, it always asks for a Pointer and a Provider. And I can't reuse this definition for a different instance of the same pointer type.
What is the pattern for creating multiple instances of the service, each with its own instance of dependency? Do I need to create a new Definition for every instantiation of Service/ Dependency in code?

@karlkfi
Copy link
Owner

karlkfi commented Feb 4, 2018

Yes. Each Definition is only constructed once, because each Pointer can only hold one object. If you want two copies of Thing then you can use NewThing as the Provider for both, but you need two Pointers to put the two objects in once they’ve been created.

@rdadbhawala-cmspl
Copy link
Author

Pardon my questions, but I'm trying to understand the constraints ...
Is there a way to get a Singleton instance of a dependency if I don't have the variable that was passed in the definition?

@karlkfi
Copy link
Owner

karlkfi commented Feb 7, 2018

Not easily, no.

The idea is that instead of hardcoding the name of the object as a string (which is equivalent to a “magic number” or enum) you reference the object with a pointer variable which you can then inject into objects that need it using the same construction mechanism. This guarantees type safety at compile time, makes refactoring trivial, and allows usage tracing, increasing maintenanability and reducing “magic”.

@karlkfi
Copy link
Owner

karlkfi commented Feb 7, 2018

FWIW, you CAN introspect the graph of objects, but if you don’t have the unique identifier (the pointer) you can only do thinks like get the list of definitions that match or satisfy a specific type.

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