DotNetStarter - Modules

DotNetStarter module types:

IStartupModules

Startup modules will execute a Startup method when either the StartupBuilder.Run() is executed or DotNetStarter.ApplicationContext.Default.Locator is accessed. This call should be placed early in the application, below are a few possible places to execute:

Note: The Shutdown method may not execute by default in all systems as noted in the known issues.

Filtering Modules

Modules can be removed during startup using the StartupModuleExpression in the StartupBuilder.ConfigureStartupModules callback. An example can be found in the customization fine-tuned example.

Startup Module Example

using DotNetStarter.Abstractions;
using System.IO;
using System.Text;
using System.Web.Hosting;

namespace Example
{
    public interface IMessageLogger
    {
        void LogMessage(string message);
    }

    [Register(typeof(IMessageLogger), LifeTime.Singleton)]
    public class MessageLogger : IMessageLogger
    {
        private StringBuilder LogBuilder = new StringBuilder();

        public void LogMessage(string message)
        {
            LogBuilder.AppendLine(message);
        }

        public override string ToString()
        {
            return LogBuilder.ToString();
        }
    }

    [StartupModule] // supports type() dependencies
    public class ExampleStartupModule : IStartupModule
    {
        IMessageLogger _Logger;

        /// <summary>
        /// Contstructors support dependency injection!
        /// </summary>
        /// <param name="logger"></param>
        public ExampleStartupModule(IMessageLogger logger)
        {
            _Logger = logger;
        }

        public void Shutdown()
        {
            _Logger.LogMessage("Shutting down!");

            File.WriteAllText(HostingEnvironment.MapPath("~/MessageLogger.txt"), _Logger.ToString());
        }

        public void Startup(IStartupEngine engine)
        {
            _Logger.LogMessage("Starting up!");
        }
    }
}