-
-
Notifications
You must be signed in to change notification settings - Fork 837
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
Enable build callbacks to run when registered in child lifetime scopes #985
Comments
I kind of feel like this is "functions as designed" - the callbacks are for container build time (hence the name) but a nested lifetime scope isn't building a container. I can see how it's potentially confusing since the builder is how things are registered in nested scopes, but it's building on top of an existing container. That said, it's a possible future enhancement we may be able to add, so I'll leave the issue open and if a PR comes in for it, we can check it out. |
We have a scenario where we are trying to avoid registering module repeatedly due to time. In previous container frameworks (e.g. Guice), this was the use case for the "life-time scope"-like pattern. Is there a pattern that I'm missing from the documentation that covers this? I can move this to StackOverflow should that be more appropriate. |
Yes, I think SO will get you help faster since you can include some context there around why you're trying to about registering the module and why things like "instance per lifetime scope" won't work. |
…emove the option when building a lifetime scope
Since I created a PR for this solution to see what it would take and what it would look like, if this is useful #1049 . The only question remaining is what to do when a module is registered. It could be enough to throw an exception if I've added a commit to my PR where the hierarchy is also present in the modules. A As you can see in the PR it could be too much. I'm not entiry sure what to think of this solution. |
Let's talk about ways to solve this since it'd be nice to have agreement on that before we head into code land. Things I can think of: Throw an exception for
|
I'm not sure if I can think of any additional options. I don't really have a problem with the proposed approach of changing The problem with allowing a build callback for lifetime scopes is mostly a semantic one I think, but also that mean's there's yet another thing to do when we initialise a lifetime scope. The purpose of build callbacks (from my perspective) was always to register the container into some other entity for integration purposes, whereas you aren't likely to need that for lifetime scopes, which are usually thread-local and available directly when you create it. |
Build callbacks definitely get used in... unforeseen ways. I don't think adding a foreach to run the build callbacks on a scope is that big of a deal, but yeah, it's one more thing. I do sort of cringe at seeing the difference between a "container builder" and a "lifetime scope builder" on this level, just to stop one thing from happening. Do we know how any of this changes when we factor in the immutable container work? Does the builder construct change enough there that it maybe makes more sense to take this container/scope builder separation? |
Taking a look at the changes for immutable container, it looks like there will be some impact at least. But nothing that appears to change the container/scope relationship sufficiently. Something that has occurred to me though...since startable components are started for objects registered in a nested lifetime scope (only when configuring the new scope), in the same way as a container, could we consider 'build callbacks' to be an internal startable component that we register the first time you call When this new The only question is what the type passed to the build callback would be... |
Making it startable is an interesting thread which may be valuable to pull on a little. Something to consider is you can build a container and skip startable components which would foil this attempt. You can't skip build callbacks. I think switching a build callback to be on an |
Perhaps not precisely an 'IStartable', but if we take a variant of |
If we add a ''RegisterContainerBuildCallback'' method to the ''ContainerBuilder'', we could take an explicitly typed IContainer callback that is only called for container builds, whereas the regular build callback is invoked regardless? |
No, I think Easier to explain based on the name but still... no. I like the startable-ish pattern you're talking about, though. |
Fair point. I'll try and find time to put a PR together with the startable approach. |
When registering a Build Callback (i.e.
RegisterBuildCallback
) within a lifetime scope secondary registration do not get called.I apologize if this is incorrect usage.
packages.config
:The text was updated successfully, but these errors were encountered: