ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

grace and multitenant with tenant based container (Dotnettency) #251

Closed mdissel closed 4 years ago

mdissel commented 4 years ago

What is the correct lifetime when I use the child containers for each tenant?

see https://github.com/dazinator/Dotnettency for a tenant based container

Singleton -> same instance in all child containers SingletonScope ?? -> asp.net core session request scope, resolved from child container SingletonPerObjectGraph -->different instance per child container

Thanks

Marco

ipjohnson commented 4 years ago

SingletonScope -> different instance per child container

SingletonPerObjectGraph -> Only singleton within the object graph, it will never be reused outside that call to scope.Locate where.

mdissel commented 4 years ago

Thanks, so SingletonPerObjectGraph should be used for one instance per http request?

ipjohnson commented 4 years ago

No you want SingletonScope as http request gets one scope for the lifetime of the request. Are you looking for a lifestyle that does one per child container and is reused for any containers the are created from the child container?

If so you want SingletonPerNamedScope and then when you create your child container with the same scope name.

mdissel commented 4 years ago

Yes, for each tenant some services can be overruled/removed/added, see this sample with AutoFac.

https://github.com/dazinator/Dotnettency/blob/develop/src/Sample.AspNetCore30.RazorPages/Startup.cs

ipjohnson commented 4 years ago

This is an interesting idea but ultimately I think it's probably really slow because child containers themselves are inherently much much slower than scopes where you can't change registration.

Grace is probably not going to perform very well, then again neither will Autofac or StructureMap.

mdissel commented 4 years ago

But it seems to be a one-time only performance penalty for each child container, see https://github.com/dazinator/Dotnettency/blob/develop/src/Dotnettency.AspNetCore/Container/TenantContainerMiddleware.cs

ipjohnson commented 4 years ago

I'm honestly surprised this works correctly and there are no weird side effects. I saw the AutofacAsync method and thought they were configuring the container mid request.

I guess if this works correctly then Grace should work the same as Autofac as that's what I modeled it after.

Are you putting together an adapter similar to what they have for Autofac/StructureMap?

mdissel commented 4 years ago

Thanks for your time, but I'm switching to another framework.