I have this static class in an ASP.NET MVC project:
public static class Setup
{
private static bool _intialized = false;
public static void Initialize(IWindsorContainer Container)
{
//Check if it has already run
if (_intialized)
{ return; }
var connectionString = config.ConnectionStrings["ccsMongo"].ConnectionString;
var dbName = config.AppSettings["ccsMongoDb"];
//Initialization code here.
Container.Register(
Component.For<IOrgRepos>().ImplementedBy<OrgRepos>()
.DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName}),
Component.For<IIndividualRepos>().ImplementedBy<IndividualRepos>()
.DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName}),
Component.For<IScoreboardRepos>().ImplementedBy<ScoreboardRepos>()
.DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName})
);
//Mark as run
_intialized = true;
}
}
I then include a call to it in my Application_Start. The intent is to be sure that it will never run more than once.
- Is there a better way to create a function that can't be run more than once in the the application lifetime?
- Am I being paranoid? I could just put a call to the initialization code into my
Application_Startand forget about the "run only once" guard code.
I would be delighted to get any feedback.
IWindsorContainer comefrom, and why can it be called multiple times, and yet should be "initialized" only once? \$\endgroup\$