Bootstrapper 1.0 Guide

Working with your IOC Container
Bootstrapper works closely with your IOC container. The first step to use Bootstrapper is to reference the Boostraper.dll and a Bootstrapper Container extension for your IOC container.
Currently Bootstrapper works with Windsor Castle, Unity and StructureMap (more coming soon...).
Lets say you decide to use Windsor Castle, then you will have to reference Bootstrapper.dll and Bootstrapper.WindsorExtension.dll.

Initializing your IOC Container
To initialize your container you must declare classes that implement I<container>Registration (IWindsorRegistration for Windsor Castle, IUnityRegistration for Unity, etc.), and implement the Register method.
The method will receive an instance of a container to configure. For example:

 public class MyServicesRegistration: IUnityRegistration
 {
        public void Register(IUnityContainer container)
        {
            container.RegisterType<IMyService1, Service1>();            
            container.RegisterType<IMyService2, Service2>();            
            //etc.
        }
    } 

Invoking Bootstrapper
Once you have defined all your registration classes, Bootstrapper will invoke the Register method of all of them once it is invoked.
You can simply call Boostrapper from your startup method like this:

public void YourStartupMethod()
{
   Bootstrapper
       .With.Container(
          new StructureMapContainerExtension()).Start();
}

Specifying Assemblies for Registrations
By default Bootstrapper will look for registrations in the invoking assembly and the entry assembly. You can add assemblies to look for registrations using the LookForRegistrations method.
For example:

public void YourStartupMethod()
{
   Bootstrapper.With.Container(
                new WindsorContainerExtension()
                            .LookForRegistrations
                                .InAssemblyNamed("MyAssembly1")
                            .LookForRegistrations
                                .InAssemblyNamed("MyAssembly2")
                            .LookForRegistrations
                                .InAssembly(Assembly.GetAssembly(typeof(MyType1)))
                            .LookForRegistrations
                                .InAssembly(Assembly.GetAssembly(typeof(MyType2)))
                )
                .Start();
}

Initializing your Automapper maps
To initialize your maps you must declare classes that implements IMapCreator and implement MapCreator method. For example:
public class Type1ToType2MapCreator: IMapCreator
{
     public void CreateMap()
     {
         Mapper.CreateMap<Type1, Type2>();
     }
}

Bootstrapper will execute the CreateMap methods of all the classes implementing IMapCreator when invoked.

Specifying Assemblies for Maps
Similarly to registration, Bootstrapper will look for maps in the invoking assembly and the entry assembly. You can add assemblies to look for maps using the LookForMaps method.
For example:

public void YourStartupMethod()
{
   Bootstrapper.With.Container(
                new UnityContainerExtension()
                            .LookForMaps
                                .InAssemblyNamed("MyAssembly1")
                            .LookForMaps
                                .InAssemblyNamed("MyAssembly2")
                            .LookForMaps
                                .InAssembly(Assembly.GetAssembly(typeof(MyType1)))
                            .LookForMaps
                                .InAssembly(Assembly.GetAssembly(typeof(MyType2)))
                )
                .Start();
}

Initializing the rest of your application
Any other task that needs to be executed at startup can be declared in a class that implements IStartupTask. The class in question will have to implement the Run and Reset method. For example:
    public class TestStartupTask: IStartupTask
    {
        public void Run()
        {
            //do something important
        }
        public void Reset()
        {
            //undo something important
        }
    }

Note that the container and maps will be fully initialized by the time the startup task runs. Bootstrappper will execute the Run method of all startup task classes when invoked. To invoke the Reset method of the startup tasks simply call the Reset method of Bootstrapper. For example:
public void YourStartupMethod()
{
   Bootstrapper
       .With.Container(
          new StructureMapContainerExtension()).Start();
}

public void YourFinalizeMethod()
{
   Bootstrapper.Reset();
}


Specifying Assemblies for Startup Tasks
Similarly to registration and maps, Bootstrapper will look for startup tasks in the invoking assembly and the entry assembly. You can add assemblies to look for startup tasks using the LookForStartupTasks method. For example:

public void YourStartupMethod()
{
   Bootstrapper.With.Container(
                new UnityContainerExtension()
                            .LookForStartupTasks
                                .InAssemblyNamed("MyAssembly1")
                            .LookForStartupTasks
                                .InAssemblyNamed("MyAssembly2")
                            .LookForStartupTasks
                                .InAssembly(Assembly.GetAssembly(typeof(MyType1)))
                            .LookForStartupTasks
                                .InAssembly(Assembly.GetAssembly(typeof(MyType2)))
                )
                .Start();
}

Accessing the initialized Container
After Bootstrapper has been invoked and the container has been initialized it can be accessed with the GetContainer method of Boostrapper. For example:
public void YourStartupMethod()
{
   Bootstrapper
       .With.Container(
          new WindsorContainerExtension()).Start();
   var container = (IWindsorContainer)Bootstrapper.GetContainer();
   var myInstance = container.Resolve<IMyType>();
}

Bootstrapper and the Common Service Locator
Alternatively, the container can be used trough the Common Service Locator since Bootstrapper by default configures it when invoked. For Example:
   Bootstrapper
       .With.Container(
          new StructureMapContainerExtension()).Start();
   var myInstance = ServiceLocator.Current.GetInstance<IMyType>();

Last edited Jun 21, 2011 at 4:20 AM by luisbocaletti, version 2

Comments

No comments yet.