locked
Unity Extension Reference. RRS feed

  • Question

  • User-963208184 posted

    Hi All,

    I'm trying to use the UnityBootstrapper class https://msdn.microsoft.com/en-us/library/microsoft.practices.composite.unityextensions.unitybootstrapper.aspx

    But I'm having trouble making a reference to it.

    I'm using VS2013. Using NuGet I have installed-package unity which gives me the Microsoft.Practices.Unity Namespace, but what ever I try I cannot obtain a reference to this library. Anybody have any ideas what  need to install to get this? otherwise if this is now obsolete what should I use now?

    My project is a class Library and I want to attach my registered types to the calling apps (e.g. an MVC Project that might reference my dll).

    Basically this is how I'm trying to use it (My classes are in separate files)

    public class Bootstrapper : UnityBootstrapper
        {
            protected override void ConfigureContainer()
            {
                base.ConfigureContainer();
    
                Container.AddNewExtension<PaymentEngineUnityExtension>();
            }
        }
    
    public class PaymentEngineUnityExtension : UnityContainerExtension
        {
            //unity binding goes here etc
            protected override void Initialize()
            {
                this.Context.Strategies.AddNew<HierarchicalLifetimeStrategy>(Microsoft.Practices.Unity.ObjectBuilder.UnityBuildStage.Lifetime);
    
                Container.RegisterType<IMypay, Mypay>(new ContainerControlledLifetimeManager());
    ......
    .....

    Wednesday, May 6, 2015 8:00 AM

All replies

  • User401857628 posted

    Hello,

    Maybe I misunderstand your question, but since I have not found

    UnityBootstrapper

    class after referencing Unity, to register classes in my libraries I've created the following interface:

        public interface IUnityApplicationModule
        {
            void Configure(IUnityContainer unityContainer);
            void AfterConfigure(IUnityContainer unityContainer);
        }

    than in each lib where I need to make some unity registrations i create class which derived from this interface, for example:

        public class DataAccessTaxonomyUnityApplicationModule : IUnityApplicationModule
        {
            public void Configure(IUnityContainer unityContainer)
            {
                unityContainer.RegisterType<ITaxonomyService, TaxonomyService>(new PerRequestLifetimeManager());
            }
    
            public void AfterConfigure(IUnityContainer unityContainer)
            {
                var configurationStore = unityContainer.Resolve<ConfigurationStore>();
    
                configurationStore.AddProfile<TaxonomyProfile>();
            }
        }
    

    And finally when you creating UnityContainer, you should load all of these Modules:

        public abstract class UnityConfig
        {
            protected UnityConfig()
            {
                _containerLazy = new Lazy<IUnityContainer>(CreateContainer);
            }
    
            protected virtual IUnityContainer CreateContainer()
            {
                var container = new UnityContainer();
    
                RegisterModules(container);
                ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
    
                return container;
            }
    
            #region Unity Container
    
            private readonly Lazy<IUnityContainer> _containerLazy;
    
    
            protected abstract IUnityApplicationModule[] GetModules();
    
            protected virtual void RegisterModules(IUnityContainer unityContainer)
            {
                var modules = GetModules();
    
                //configure
                foreach (var module in modules)
                    module.Configure(unityContainer);
    
                // after configure
                foreach (var module in modules)
                    module.AfterConfigure(unityContainer);
            }
    
            public IUnityContainer GetConfiguredContainer()
            {
                return _containerLazy.Value;
            }
    
            #endregion
        }

    in derived class you may use dynamic

        public class WebUnityConfig : ProjectBase.Dependency.UnityConfig
        {
            protected override IUnityApplicationModule[] GetModules()
            {
                try
                {
                    var interfaceType = typeof(IUnityApplicationModule);
                    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    var result = assemblies
                        .SelectMany(s => s.GetTypes())
                        .Where(x => interfaceType.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
                        .Select(x => Activator.CreateInstance(x) as IUnityApplicationModule)
                        .ToArray();
                    return result;
                }
                catch (Exception exception)
                {
                    throw;
                }
            }
        }

    or static 

        public class WebUnityConfig : ProjectBase.Dependency.UnityConfig
        {
            protected override IUnityApplicationModule[] GetModules()
            {
                return new[]
                {
                    new WebUnityApplicationModule()
                };
            }
        }

    implementation of gathering Application modules 

    Monday, October 26, 2015 11:13 AM