Do you know about Bootstrapper?
If you don't know about it before, I will explain it now. If you used to turn on your computer, you shall see that you only are able to work when your operating system (OS) ready to booting. And underline your computer must do many works for preparing your OS. You can't see those, it only run underline. That is a bootstrapper task. You shall not be care about all task that run under OS, you only need working. Do you agree with me? Yeah! If you agree with me, you understood the jobs that Bootstrapper take it to you.
Now is the time that we come back to software world, How are we able to boot all our tasks in software (include Windows Form & Web)? And why must we run bootstrapper in our application? The second question, one articles can answer for you at
here. And the first question I will answer for you after my post end. OK now we shall start to preparing all thing for implementing the Bootstrapper at below:
+ First thing, we must define the interface for the contract in each task:
public interface IBootstrapperTask
{
void Execute();
}
+ Next, I will implement the abstract class for compositing all functions that I think I can re-use:
public abstract class CommonBootStrapper
{
public static IServiceLocator Locator;
protected IConfigurationManager _configurationManager;
protected CommonBootStrapper(IConfigurationManager configurationManager, string conStringName, params string[] bootTaskAssemblies)
{
_configurationManager = configurationManager;
Locator = CreateServiceLocator(conStringName, bootTaskAssemblies);
Run();
}
///
/// Create Service Locator object, including create and configure Nhibernate object
///
/// Service Locator instance
protected abstract IServiceLocator CreateServiceLocator(string conStringName, params string[] bootTaskAssemblies);
///
/// Run all task in background
///
protected abstract void Run();
}
+ And I continue to implement sub-class for CommonBootStrapper that I just implemented:
public class BootStrapper : CommonBootStrapper
{
public BootStrapper() : this(null, "", "") { }
public BootStrapper(IConfigurationManager configurationManager, string conStringName, params string[] bootTaskAssemblies)
: base(configurationManager, conStringName, bootTaskAssemblies)
{ }
///
/// Create ServiceLocator object
///
///
protected override IServiceLocator CreateServiceLocator(string conStringName, params string[] bootTaskAssemblies)
{
HibernateRegister(conStringName, bootTaskAssemblies);
return new StructureMap.ServiceLocatorAdapter.StructureMapServiceLocator(
ObjectFactory.Container);
}
///
/// Using Structure Map for scanning all instances from Assemply
/// , after that we call for execiting it
///
protected override void Run()
{
var listTask = IoC.GetAllInstances();
if (listTask != null)
{
foreach (var task in listTask)
{
task.Execute();
}
}
}
///
/// Register NHibernate configuration for container
///
private void HibernateRegister(string conStringName, params string[] bootTaskAssemblies)
{
ObjectFactory.Initialize(x => {
x.AddRegistry(new NHibernateRegistry(_configurationManager, conStringName, bootTaskAssemblies));
});
}
}
+ Don't confuse about some messy thing in my code. Because I copied it in
NMA project, so it have many things (StructureMap, Automapper, Fluent NHibernate,...) in it. But if you follow all post at my blog, I will explain all thing for you. Now continue, we shall implement one class for registering Automapper task when my application started:
public class AutoMapperConfigurator : IBootstrapperTask
{
public void Execute()
{
Initialize();
}
public static void Initialize()
{
Mapper.Initialize(x => x.AddProfile());
}
}
Please go to
http://thangcq.blogspot.com/2010/04/configurating-automapper.html for clear about AutoMapper.
Certainly, you can inplement many tasks for booting, such as setup action filter, add model binder, add custom view engine,...
+ Finally, we shall call it in Global.asax for web or App.xaml in WPF. But in this post I only implement in web:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
new NMA.Infrastructure.NHibernate.Bootstrapper.BootStrapper(
new ConfigurationManagerWrapper(),
"NMAConnectionString",
"NMA.Infrastructure.NHibernate", "NMA.Application", "NMA.Infrastructure.AutoMapper");
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
}
At here, I used StructureMap for scanning 3 dlls: NMA.Infrastructure.NHibernate, NMA.Application, NMA.Infrastructure.AutoMapper for finding all tasks in my application.
I implemented scanning dll at CoreRegistry.cs, and this is the important function inside this class:
private void ScanTaskAssemblies(IAssemblyScanner scanner, params string[] bootTaskAssemblies)
{
for (int i = 0; i < bootTaskAssemblies.Length; i++)
{
scanner.Assembly(bootTaskAssemblies[i].ToString());
}
scanner.AddAllTypesOf();
scanner.WithDefaultConventions();
}
As you see this function will find all class that implemented interface IBootstrapperTask. If you implement it in any other dll, you also show it for Bootstrapper.
* And I answered for you the first question if you read all my post.
In past time, I also suggested this ideas for
Deran Schilling and he also implemented very excellent post at
here.
OK, I just finished the post about Bootstrapper, if you don't want to implement from scratch, you can use Unity Bootstrapper that mention at
here. But it use
Prism on WPF.
Finally you can find all my source code on
NMA project. Good bye and see you next time!