bootstrapper as a static member

Topics: Web Client Software Factory
Apr 8, 2010 at 12:26 PM

In the App_EndRequest event I need the current UnitOfWork to commit the changes and for that I need the container.

Is it ok if I use a static member like this ?

 


private static MyBootstrapper bootstrapper; protected void Application_Start() { bootstrapper = new MyBootstrapper(); bootstrapper.Run(); RegisterRoutes(RouteTable.Routes); }

...

protected void Application_EndRequest(object sender, EventArgs e) { IUnitOfWork uow = bootstrapper.UnityContainer.Resolve<IUnitOfWork>(); uow.Commit(); }

Thanks

 

Apr 9, 2010 at 8:06 PM

Hi Lario,

Having a static memeber to store the bootstrapper will probably work in most scenarios.
Anyway I would use an instance member rather than a static memeber, and probably I would store the container and not the bootstrapper itself.

private MyBootstrapper bootstrapper;

protected void Application_Start()
{
MusicStoreApplicationBootstrapper bootstrapper = new MyBootstrapper();
bootstrapper.Run();

unityContainer = bootstrapper.UnityContainer;

RegisterRoutes(RouteTable.Routes);
}

...

protected void Application_EndRequest(object sender, EventArgs e)
{
IUnitOfWork uow = unityContainer.Resolve<IUnitOfWork>();

uow.Commit();
}

Depending your scenario, and if you are using MVC, it might be more suitable to use action filters rather than the Application_EndRequest. Action filters allow to run code after actions are executed, they can be set to particular actions or all actions (decorating a base controller).

Hope it helps!

PD: If you are using the Web Client Developer Guidance, the best tag to use is Web Guidance v-Next (not WCSF).

Matias Bonaventura
http://blogs.southworks.net/matiasb