Parent container + child containers + different lifetimes

956 views
Skip to first unread message

ahjohannessen

unread,
May 5, 2010, 7:05:19 PM5/5/10
to Autofac
Hi :)

I have this compositional problem in that I have a mvc web application
that acts as a host for mini-applications that in concert compose a
modular application.
The host has several system-wide services that handles stuff like
persistence, logging, users, permissions and so on. These need to have
different lifetimes (singleton, pr httprequest, session) and use named
services in order to support multi-tenancy w.r.t. different
implementations of specific components.

Apparently this is relatively easy to get up and going with Autofac
when looking into the system.web integration support Autofac has mvc
and asp.net (not so sure about session scoping) - However, when
considering that mini-applications should not mess about with
registrations of components in the application container, nor
registrations of each other leads to thinking about one application
container and multiple mini-app containers.
The deal is that these mini-app containers should have scoping of
components that correspond to httprequest, session and singleton, yet
be able to resolve "system-wide" services via their parent container.
This means that components in the mini-app can take constructor
dependency on components from the host ifself and the mini-app
subsystem itself.

I don't think configuring a child container on each request seems like
is a viable solution, so my thinking is that when bootstrapping I do
something similar like:

First the bootstrap application container, then for each mini-app
create a child container and pass it to the mini-app to configure and
store it in the application container, identified by a guid that I can
deduce on each http request. Then when a http request comes along I
get a hold on the application container and create a request container
from it, then in the request container's scope where the execution
pipeline for the mini-app begins I resolve the child container by guid
and again create a request lifetime scope from there, and when the
execution pipeline ends, i dispose that request lifetime scope.

I am not confident that this the correct approach. The only container
that seems be able to do this is Castle Windsor with its ability to
register child containers directly on parent containers - Structuremap
does not handle this as it just has a simple nested container
functionality that does not work with this.

I have looked through almost every post here related to the matter,
surfed the net, looked at Orchard code. Nothing I found takes an
approach that I wish for.

How are people doing this host container -> child containers with
regards to resolving upwards + having same lifetime scope features on
both levels?

Cheers,
Hopefully an Autofac user soon :)

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.

ahjohannessen

unread,
May 6, 2010, 12:46:42 PM5/6/10
to Autofac
Hi again :)

I think I figured it out by myself :D will post code that illustrates
how I did it.

ahjohannessen

unread,
May 10, 2010, 5:51:35 AM5/10/10
to Autofac
Hi again :)

Here is how I solved my problem. The code below is just illustrative.

var miniApps = new List<IApplication> { new MiniApp1 { Id = "1" }, new
MiniApp2 { Id = "2" } };

var systemBuilder = new ContainerBuilder();

// Do system wide config
systemBuilder.RegisterType<Baz>().As<IBaz>();
systemBuilder.RegisterType<Bar>().As<IBar>().SingleInstance();

var appContainer = systemBuilder.Build();

// Config of individual apps
var compositionBuilder = new ContainerBuilder();
foreach (var application in miniApps)
{
var app = application;
compositionBuilder
.RegisterInstance(appContainer.BeginLifetimeScope(app.Id,
app.Configure))
.Named<ILifetimeScope>(application.Id);
}

compositionBuilder.Update(appContainer);
...
_containerProvider = new ContainerProvider(appContainer);
...

After having deduced the identifier of the current active mini-
application, I can do things like the following:

var uniqueId = ...
var appScope = appContainer.Resolve<ILifetimeScope>(uniqueId);
var baz = appScope.Resolve<IBaz>();
...

and init other types of scopes within this scope:

var requestScope = appScope.BeginLifetimeScope();
...


I have little experience with Autofac - therefore it would be nice to
get some feedback from one of you Autofac guys whether this is a sound
way of doing things :) I have noticed one potential flaw that is if
the mini-apps do a .Build() on the provided builder, however clever
usage of dynamicproxy can probably guard against that, if not then it
is acceptable as it is a bootstrapping thing that fails fast.


Cheers

Nicholas Blumhardt

unread,
May 11, 2010, 6:55:53 AM5/11/10
to aut...@googlegroups.com
Hi,

It is tricky to digest all of this in one go; you seem to be doing okay so don't let my suggestions distract you :)

There is some discussion of how we might do multi-tenancy as a general-purpose feature here: http://groups.google.com/group/autofac/msg/c547ee056bd7e3db - perhaps some of that will be of use to you.

Cheers,
Nick

ahjohannessen

unread,
May 11, 2010, 1:48:00 PM5/11/10
to Autofac
Thanks for your reply, Nicholas :) will look into it.

On May 11, 11:55 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi,
>
> It is tricky to digest all of this in one go; you seem to be doing okay so
> don't let my suggestions distract you :)
>
> There is some discussion of how we might do multi-tenancy as a
> general-purpose feature here:http://groups.google.com/group/autofac/msg/c547ee056bd7e3db- perhaps some
> of that will be of use to you.
>
> Cheers,
> Nick
>
> > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com >
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/autofac?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "Autofac" group.
> To post to this group, send email to aut...@googlegroups.com.
> To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/autofac?hl=en.
Reply all
Reply to author
Forward
0 new messages