Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force the order of Installer Execution

I have been building a new .NET solu­tion with Cas­tle per­form­ing my DI.

Its now at the stage where i would like to con­trol the order in which my installers run. I have built indi­vid­ual classes which implement IWind­sorIn­staller to han­dle my core types — eg IRepos­i­tory, IMap­per and ISer­vice to name a few.

I see that its suggested i implement my own Installer­Fac­tory (guessing i just override Select) in this class.

Then use this new factory in my call to:

FromAssembly.InDirectory(new AssemblyFilter("bin loca­tion")); 

My ques­tion — when over­rid­ing the save method — what is the best way to force the order of my installers.

like image 725
Adam Stewart Avatar asked Jun 15 '11 13:06

Adam Stewart


1 Answers

I know its already solved but I couldn't find any example on how to actually implement the InstallerFactory so here's a solution if anyone is googling for it.

How to use:

[InstallerPriority(0)]
    public class ImportantInstallerToRunFirst : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
        {
            // do registrations
        }
    }

Just add the InstallerPriority attribute with a priority to your "install-order-sensitive" classes. Installers will be sorted by ascending. Installers without priority will default to 100.

How to implement:

public class WindsorBootstrap : InstallerFactory
    {

        public override IEnumerable<Type> Select(IEnumerable<Type> installerTypes)
        {
            var retval =  installerTypes.OrderBy(x => this.GetPriority(x));
            return retval;
        }

        private int GetPriority(Type type)
        {
            var attribute = type.GetCustomAttributes(typeof(InstallerPriorityAttribute), false).FirstOrDefault() as InstallerPriorityAttribute;
            return attribute != null ? attribute.Priority : InstallerPriorityAttribute.DefaultPriority;
        }

    }


[AttributeUsage(AttributeTargets.Class)]
    public sealed class InstallerPriorityAttribute : Attribute
    {
        public const int DefaultPriority = 100;

        public int Priority { get; private set; }
        public InstallerPriorityAttribute(int priority)
        {
            this.Priority = priority;
        }
    }

When starting application, global.asax etc:

container.Install(FromAssembly.This(new WindsorBootstrap()));
like image 160
Jonas Stensved Avatar answered Nov 04 '22 02:11

Jonas Stensved