0

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.

Marc L.
  • 3,296
  • 1
  • 32
  • 42

1 Answers1

0

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.)

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • Thanks. I started to peruse the source to get at this, and it quickly becomes apparent that it is all about lifetimes. I am curious about the "protection" you mention. Don't "Owned" instances bypass that? – Marc L. Mar 29 '11 at 04:10
  • Reading your second scenario again, I'm not too clear on what you mean by that one - whether the multi-tenancy aspect is still involved in the second scenario... But yes, you're right about `Owned` changing the rules, but it may be in a way other than as you expect. Some more details on the big picture here might help. Cheers! – Nicholas Blumhardt Mar 29 '11 at 10:47
  • I'm new to Autofac and my question was to gain understanding of the tool itself rather than its application, so the abstract format was a better fit. Anyway, it looks like this is a non-starter, since MTC operates by registering tenant-specific components in a child lifetime-scope using `BeginLifetimeScope(Action)`. Any attempt to resolve `B` (registered on root) when `A` is registered only in the MTC (child lifetime scope) is going to result in a `DependencyResolutionException`. – Marc L. Mar 30 '11 at 17:11