Replies: 2 comments 1 reply
-
Doesn't work that way. You're not "registering" anything in a Factory definition. It's just a factory struct returned from a calculated variable on request during runtime. Nor is there any way to see them (Mirror doesn't show calculated variables). Bottom line is that you can't tell if anything's missing until you ask for it. That's kind of why |
Beta Was this translation helpful? Give feedback.
-
Yes, I know that computed variables are added to the container that reference Factory to create an object instance. I was going through the code and there is a second mechanism that is used to additionally register a new closure to create the object (the Factory.register(factory:) function). This function in effect changes the FactoryMap "registrations" on the container. This map is primarily used when resolving dependencies before using the actual factory. Therefore, it is possible to create a special new .init function for Factory that, in addition to creating a special FactoryRegistration, internally calls the register(factory:) function to get this special registration type into the Container.registrations map. The values in this map could then be checked for the presence of the specially created registration. |
Beta Was this translation helpful? Give feedback.
-
Hi Michael,
let's have an example:
I have a main application and 2 modules that are not directly dependent on each other. Each module has its own container in which the factories it needs are registered.
If I want to register a factory in ModuleA to a class defined in ModuleB, I have to create a calculated variable with a factory in ModuleA that returns e.g. fatalError():
and solve registration in the main application (see module documentation).
But this causes dependencies not to be evaluated during compilation, but at runtime.
My idea is to make the definition of the protocol type in ContainerA as non-optional and to ensure a check to register all closures immediately after registering the dependencies in the main application. This way, immediately after the successful launch of the application, I would know that I have all the necessary dependencies available and I would not have to deal with cases with optional types in factories. Although it is not a full-fledged solution of dependencies during compilation (this is also not possible thanks to the separation of modules), but it will allow me to easily check whether all dependencies are defined at the start of the application, so all modules can then rely on the fact that the dependencies are available.
I wanted to adapt Factory to this case using extensions, but I was not successful (it would require changes in Factory itself).
If a special .init were added to the Factory, which would not take a closure, but instead used an internal closure "unregistered" e.g. with fatalError() and subsequently, using the register(factory:) function, this closure would also be set in the FactoryMap manager.registrations , it could then be checked, thanks to the TypedFactory type, whether there is any "unregistered" closure registered in manager.registrations.
Beta Was this translation helpful? Give feedback.
All reactions