Question

Given two class A and B where B depends on A (constructor injected); B is registered as SingleInstance in the base container, and it is built. A is registered in a MultiTenantContainer as SingleInstance, varying on the tenantId.

Question 1: will

Resolve<B>()

recognize that B must also vary on tenantId?

Question 2: same, but A is registered as lifetime-scoped within the multi-tenant container and B is dependent on

Func<Owned<A>>.

Update: closure!

This was a case of "what I really wanted was..." because of my beginner's lack of understanding of the semantics; specifically, I needed instances that were built/shared across all resolutions within a tenant lifetime scope, which did not exist. Got some guidance, submitted a patch on issue #318, it was pulled into the trunk on changeset 752, and we're good to go! Now the question above would read:

Given two class A and B where B depends on A (constructor injected); B is registered as InstancePerTenant in the base container, and it is built. A is registered in a MultiTenantContainer as InstancePerTenant, varying on the tenantId...

and the answer to both questions is now "yes".

Thanks Travis and Nick for your help and maintaining this great tool.

Was it helpful?

Solution

If I understand your example correctly the answer is "no" on both counts.

Autofac protects against any long-lived instance ever gaining a reference to a shorter-lived instance.

Though it might mean rethinking designs here and there, in my experience this makes for more predictable behaviour.

Hope this helps!

Nick

(BTW, if you need some input on any specific scenario you might consider creating another question with the concrete components described.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top