This project is read-only.

Bootstrapping

ApplicationBootstrapper class

Bootstrapping engine runs plugins which should be execute only once at the application startup. The following line need to be added in your App.xaml:

 

<?xml version="1.0" encoding="utf-8"?>
<Application 

xmlns:vm="clr-namespace:SecureBox.UI.ViewModel"
             xmlns:core="clr-namespace:PhoneCore.Framework.Bootstrap;assembly=PhoneCore.Framework" mc:Ignorable="d">
  <!--Application Resources-->
  <Application.Resources>
        <core:ApplicationBootstrapper x:Key="Bootstrapper" d:IsDataSource="False"  />

  </Application.Resources>

</Application>

 

ApplicationBootstrapper class performs the next actions:

  • create container
  • create bootstrapper service
  • run bootstrapper service

BootsrapperService class

BootsrapperService runs the list of defined plugins:

 

namespace PhoneCore.Framework.Bootstrap
{
    /// <summary>
    /// Provides default functionality to execute startup plugins
    /// </summary>
    public class BootstrapperService: IBootstrapperService
    {
        //TODO ensure that it runs only once
        private readonly ITrace _trace = TraceFactory.GetTrace();
        private readonly ITraceCategory _category = TraceCategory.GetInstance("Bootstrapping.Service");

        private readonly IBootstrapperPlugin[] _plugins;

        public BootstrapperService(ConfigSection config, IContainer container)
        {
            _trace.Info(_category, "get bootstrappers from configuration");
            var bootstrappers = config.GetSections("bootstrappers/bootstrapper");

            List<IBootstrapperPlugin> tasks = new List<IBootstrapperPlugin>();
            foreach (var bootsrappersSection in bootstrappers)
            {
                _trace.Info(_category, String.Format("create '{0}' of type '{1}'", bootsrappersSection.GetString("@name"), bootsrappersSection.GetString("@type")));
                var plugin = bootsrappersSection.GetInstance<IBootstrapperPlugin>("@type", bootsrappersSection, container);
                tasks.Add(plugin);
            }
            _plugins = tasks.ToArray();
            _trace.Info(_category, String.Format("register plugins: {0}", _plugins.Count()));
        }

        public BootstrapperService(IBootstrapperPlugin[] plugins)
        {
            _trace.Info(_category, String.Format("register plugins: {0}", plugins.Count()));
            _plugins = plugins;
        }

        #region IBootstrapperService members

        /// <summary>
        /// Run all registred bootstrappers
        /// </summary>
        /// <returns></returns>
        public bool Run()
        {
            _trace.Info(_category, "run bootstrapper");
            return _plugins.Aggregate(true, (current, task) => current & task.Run());
        }

        /// <summary>
        /// Updates all registred bootstrappers
        /// </summary>
        /// <returns></returns>
        public bool Update()
        {
            return _plugins.Aggregate(true, (current, task) => current & task.Update());
        }

        /// <summary>
        /// Returns the task registered in bootstrapper by name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IBootstrapperPlugin GetPlugin(string name)
        {
            return _plugins.Single(task => task.Name == name);
        }

        /// <summary>
        /// Returns the task registered in bootstrapper by type
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public IBootstrapperPlugin GetPlugin(Type t)
        {
            return _plugins.Single(task => task.GetType() == t);
        }

        /// <summary>
        ///  Returns the task registered in bootstrapper by type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IBootstrapperPlugin GetPlugin<T>()
        {
            return GetPlugin(typeof(T));
        }

        #endregion
    }
} 

 

The following methods are defined below:

  • Run() – execute all plugins by calling its Run() method
  • Update() –calls Update() method of each plugin
  • GetPlugin() – gets the registered plugin instance by its type or name

The last group of methods provides the access to the separate instance of registered plugin by type or name:

 

container.Resolve<IBootstrapperService>().GetPlugin<CoreBootstrapperPlugin>(); 

 

Let’s look at the major plugin: CoreBootstrapperPlugin

Core plugin

CoreBootstrapperPlugin plugin registers core services in container. The services should be defined in configuration and implement framework’s interfaces:

 <bootstrapper name="Core" type="PhoneCore.Framework.Bootstrap.CoreBootstrapperPlugin, PhoneCore.Framework">
          <services>
            <navigation type="PhoneCore.Framework.Navigation.NavigationService, PhoneCore.Framework" />
            <fileSystem type="PhoneCore.Framework.Storage.IsolatedStorageFileService, PhoneCore.Framework" />
            <settings type="PhoneCore.Framework.Storage.IsolatedStorageSettingService, PhoneCore.Framework" />
          </services>
        </bootstrapper> 

Afterwards, user code can consume the registered services. They are registered in the container by the core plugin:

  • PageMapping
  • NavigationService
  • FileSystemService
  • SettingsService
  • IsolatedStorageSettingsService

If there's a need to switch to the usage of your custom implementation, you should create your own implementation of bootstrapper plugin and define it instead of Core.

Custom plugins

You can create your custom plugins which will be executed after Core plugin. All you need is to create class inherited from IBootstrapperPlugin:

 

namespace PhoneCore.Framework.Bootstrap
{
    /// <summary>
    /// Represents a startup plugin
    /// </summary>
    public interface IBootstrapperPlugin
    {
        /// <summary>
        /// The name of plugin
        /// </summary>
        string Name { get; }

        /// <summary>
        /// Run plugin
        /// </summary>
        /// <returns></returns>
        bool Run();

        /// <summary>
        /// Update plugin
        /// </summary>
        /// <returns></returns>
        bool Update();
    }
}

and add it in configuration section:

<system>
    <!-- bootstrapping -->
    <bootstrapping>
      <bootstrappers>
        <bootstrapper name="Core" type="PhoneCore.Framework.Bootstrap.CoreBootstrapperPlugin, PhoneCore.Framework" />
        <bootstrapper name="Init" type="SecureBox.UI.Infrastructure.Plugins.InitPlugin, SecureBox.UI" />

 

Note: The order is important. The core plugin must be first as the plugins are executed as they are defined.

Last edited Feb 11, 2012 at 10:21 PM by Ilya_Builuk, version 2

Comments

No comments yet.