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
Add built-in support for auto-traversing Assembly trees #468
base: master
Are you sure you want to change the base?
Conversation
|
||
struct NestedAssembly: Assembly { | ||
|
||
typealias ArrayType = NSMutableArray |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The testing concept injects two (reference type) arrays to allow tracking the order of the assemble and load operations. Seems easiest to do with NSMutableArray
. Just first thing that came to mind
let d = NestedAssembly(name: "d", children: []) | ||
let c = NestedAssembly(name: "c", children: [d]) | ||
let b = NestedAssembly(name: "b", children: []) | ||
let a = NestedAssembly(name: "a", children: [b, c]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple tree:
A
| \
B C
\
D
expect(assemble).to(equal(["b", "d", "c", "a"])) | ||
expect(loaded).to(equal(["b", "d", "c", "a"])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strictly depth-first, since B->D->C->A, but may be good enough
0c04323
to
95f085e
Compare
I was also thinking the exact same thing. We do something very similar at my company. Would love to see this become a part of Swinject. |
Do they? They're all registering to the same Container, so I'm not sure what you get from the hierarchy of Assemblies. You would be able to get the same behaviour if you flattened your Assemblies. I'll typically do something like this to register large groups of assemblies:
|
@mpdifran This is certainly how we were using it in a very large project, with quite a big tree of dependencies. It was understood at the beginning that this wasn't harmful, since re-registering services (because a node appears more than once in the tree) would just overwrite the registration with the same thing. Proved to be not so straightforward, though! So yes, since making this I've become more inclined to agree that there's no way around flattening the tree. I actually wondered about building some component that was an |
Hi everyone,
Having used Swinject for many years, particularly the Assembler features, I was suddenly struck by the fact that the current implementation is "missing a trick".
In our rather large project, I/we have tried to make use of the
loaded(...)
hooks, but were surprised that they didn't behave as expected.The approach we have is basically a big tree of
Assembly
. Assemblies tend to have children internally, and up until now we supported this by doing something like:This works fine, but since the
loaded(...)
hook is a feature ofAssembler
, it won't be called; only on the "flat" array of assemblies passed when theAssembler
is created.It seems that the "built-in" way to do this is to use
Assembler
parent/child relationships. However, this requires a reference to the parentAssembler
, and felt rather unintuitive, so we took this "lighter-weight" approach.However, it dawned on me: why doesn't
Assembly
/Assembler
support expressing a hierarchy out-of-the-box?By using the method provided, it is possible to express a full tree using a single
Assembler
andContainer
if desired (this is what we do). Then all theloaded(...)
callbacks are also called as expected.Since the default is a no-op, this doesn't change anything if the feature is unused.
Currently this works in a sort-of depth-first order, although not quite. It could also be done in a strict depth-first, but I figured this is probably not so important; on the other hand, it makes sense that node assemblies have
loaded(...)
called first, just that the order of the nodes probably doesn't matter much. This could of course be changed, but would require using a stack, as with normal depth-first.Also, trees could contain cycles, so that could be added, but also starts feeling a little heavy-weight.
Anyway, this is an idea/concept. Please let me know what you think.
Thanks