Tuesday, December 13, 2016

IoC with Unity config vs Fortis

In our G MVC app we had one web project in the solution in addition to class library ones. In it, in the App_Start we had a UnitConfig file that registered interfaces to use concretes like this:

public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<IAccountContext, AccountContext>();
            container.RegisterType<IAccountRepository, AccountRepository>();
            container.RegisterType<IPreferenceService, PreferenceService>();
            container.RegisterType<IFormProcessor, FormProcessor>();
}


In existing G solution they used Fortis and the following way to register container:

every project had this class

public class IoCRegistration : IIoCRegistrant
    {
        public void Initialize(IContainerAdapter container)
        {
            // Register Fortis
            container.RegisterSingleton<IAccountContext, AccountContext>();
            container.RegisterSingleton<IAccountRepository, AccountRepository>();
         
        }
    }

public interface IIoCRegistrant
    {
        void Initialize(IContainerAdapter container);
    }

And then in the web project they had:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(SimpleInjectorInitializer), "Initialize")]

And in a Core project they had

 private static void InitializeContainer(IContainerAdapter container)
        {
            //search all onegeisinger assemblies for IIOCRegistrant implementations
            List<IEnumerable<Type>> registrantsAssemblies = AppDomain.CurrentDomain.GetAssemblies()
                .Where(x => x.FullName.ToLower().Contains("geisinger"))
                .Select(x => x.GetTypes().Where(y => y.IsInterface == false && y.IsAssignableTo(typeof(IIoCRegistrant)))).ToList();

            if (!registrantsAssemblies.Any())
            {
                //this most likely means the project name above might be outdated or needs changing
                throw new Exception("Could not find any assemblies with IoC Registration");
            }

            //iterate through assemblies
            foreach (IEnumerable<Type> registrantAssembly in registrantsAssemblies)
            {
                //register any mvc controllers for this assembly
               

                //get the first class that implements IoCRegistrant
                Type registrantClass = registrantAssembly.FirstOrDefault();
                if (registrantClass != null)
                {
                    //create an instance of IoCRegistrant
                    IIoCRegistrant instance = Activator.CreateInstance(registrantClass) as IIoCRegistrant;
                    if (instance != null)
                    {
                        try
                        {
                            //initialize IoCRegistrant
                            instance.Initialize(container);
                        }
                        catch (Exception e)
                        {
                            //log any errors and move to the next one
                            Sitecore.Diagnostics.Log.Error("IOC Initialization Error from: " + registrantClass.FullName + ", Message: " +  e.Message, e);
                        }

                    }

                }

            }
        }

No comments:

Post a Comment