-
Hi Maksim, I am currently playing around with DryIoc 4.7.5 and gladly found that you have added IContainer.CreateChild(). I learned that by default a child shares all registrations and cached resolutions with the parent. Now I am wondering why this behavior is depending on where and when a singleton instance is created first. If it is created within the parent (prior to the child creation) then the instance resolved within the child is the same. But when the child was created before the singleton instance was created within the parent, then the instances resolved in both differ. Why is that so? And is there a way that allows me to avoid this behavior? When using a scope this works fine: All scopes share the same singleton instance, no matter where and when it was created first. I would like to have this behavior when using child containers as well. Cheers, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @chrisg32 , My main problem with the child containers is that slightly different expectations on what and how should they work by default. public static IContainer CreateChild(this IContainer container,
IfAlreadyRegistered? ifAlreadyRegistered = null, Rules newRules = null, bool withDisposables = false)
{
var rules = newRules != null && newRules != container.Rules ? newRules : container.Rules;
return container.With(
container.Parent,
ifAlreadyRegistered == null ? rules : rules.WithDefaultIfAlreadyRegistered(ifAlreadyRegistered.Value),
container.ScopeContext,
RegistrySharing.CloneAndDropCache,
container.SingletonScope.Clone(withDisposables),
container.CurrentScope ?.Clone(withDisposables));
} In your case the problematic like is the I will think about how to do that and meanwhile, I am open to ideas. |
Beta Was this translation helpful? Give feedback.
Hi @chrisg32 ,
My main problem with the child containers is that slightly different expectations on what and how should they work by default.
That's why it took me so long to provide the specific method
CreateChild
but anyway it does not solve everyone's problem.Here is the current
CreateChild
implementation: