/// <summary>
        /// Adds a service instance to the container.
        /// </summary>
        /// <typeparam name="T">The service type.</typeparam>
        /// <param name="container">The container instance itself.</param>
        /// <param name="serviceName">The service name.</param>
        /// <param name="serviceInstance">The service instance.</param>
        public static void AddService <T>(this IMicroContainer container, string serviceName, T serviceInstance)
        {
            var serviceType = typeof(T);

            // Find the next available container slot
            AddService(container, serviceType, serviceName, serviceInstance);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="HiroServiceRegistrar"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 /// <param name="builder">The builder.</param>
 /// <param name="containerBuilder">The container builder.</param>
 public HiroServiceRegistrar(HiroServiceLocator parent, DependencyMap builder, out Func <IMicroContainer> containerBuilder)
 {
     _parent              = parent;
     _builder             = builder;
     containerBuilder     = (() => _container = _builder.CreateContainer());
     LifetimeForRegisters = ServiceRegistrarLifetime.Transient;
 }
Esempio n. 3
0
        public override void PrepareBasic()
        {
            var map = new DependencyMap();

            RegisterBasic(map);
            this.container = map.CreateContainer();
        }
Esempio n. 4
0
        /// <summary>
        /// Compiles and instantiates a container instance using the current dependencies in the dependency map.
        /// </summary>
        /// <returns>A <see cref="IMicroContainer"/> instance.</returns>
        public IMicroContainer CreateContainer()
        {
            if (ContainerCompiler == null)
            {
                throw new NullReferenceException("The ContainerCompiler property cannot be null");
            }

            var      compiler       = ContainerCompiler;
            var      assembly       = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", this);
            Assembly loadedAssembly = CompileContainerAssembly(assembly);

            var containerTypes = new List <Type>();

            foreach (var type in loadedAssembly.GetTypes())
            {
                if (!typeof(IMicroContainer).IsAssignableFrom(type))
                {
                    continue;
                }

                containerTypes.Add(type);
            }

            var             containerType = containerTypes[0];
            IMicroContainer result        = (IMicroContainer)Activator.CreateInstance(containerType);

            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates the instance with the given <paramref name="container"/> and functor <paramref name="key"/>.
        /// </summary>
        /// <param name="key">The unique functor id.</param>
        /// <param name="container">The host container.</param>
        /// <returns>A new object instance.</returns>
        public static object CreateInstance(string key, IMicroContainer container)
        {
            var functor = GetFunctor(key);
            if (functor == null)
                return null;

            return functor(container);
        }
Esempio n. 6
0
        //public HiroServiceLocator(IMicroContainer container)
        //{
        //    if (container == null)
        //        throw new ArgumentNullException("container");
        //    Container = container;
        //}

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_container != null)
            {
                _container        = null;
                _registrar        = null;
                _containerBuilder = null;
            }
        }
        public override void Prepare()
        {
            var map = new DependencyMap();
            RegisterBasic(map);
            
            RegisterPropertyInjection(map);

            this.container = map.CreateContainer();
        }
Esempio n. 8
0
        /// <summary>
        /// Compiles and instantiates a container instance using the current dependencies in the dependency map.
        /// </summary>
        /// <returns>A <see cref="IMicroContainer"/> instance.</returns>
        public IMicroContainer CreateContainerAndCache(string path, DateTime updateIfOlderThan)
        {
            if (ContainerCompiler == null)
            {
                throw new NullReferenceException("The ContainerCompiler property cannot be null");
            }
            Assembly loadedAssembly;

            if (Directory.Exists(path))
            {
                path = Path.Combine(path, "Hiro.CompiledContainers.dll");
            }
            var cachedAssemblyExists = File.Exists(path);
            var needsUpdate          = !cachedAssemblyExists || (new FileInfo(path).LastWriteTime < updateIfOlderThan);

            if (needsUpdate)
            {
                var compiler = ContainerCompiler;
                var assembly = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", this);
                assembly.Write(path);
                loadedAssembly = CompileContainerAssembly(assembly);
            }
            else
            {
                //loadedAssembly = Assembly.LoadFile(path);
                loadedAssembly = Assembly.Load(File.ReadAllBytes(path));
                // deserialize the functor registry
                foreach (var list in _entries.Values)
                {
                    foreach (var implementation in list)
                    {
                        if (implementation is FunctorCall)
                        {
                            ((FunctorCall)implementation).RegisterFunctor();
                        }
                    }
                }
            }

            var containerTypes = new List <Type>();

            foreach (var type in loadedAssembly.GetTypes())
            {
                if (!typeof(IMicroContainer).IsAssignableFrom(type))
                {
                    continue;
                }

                containerTypes.Add(type);
            }

            var             containerType = containerTypes[0];
            IMicroContainer result        = (IMicroContainer)Activator.CreateInstance(containerType);

            return(result);
        }
        public override void Prepare()
        {
            var map = new DependencyMap();

            RegisterDummies(map);
            RegisterStandard(map);
            RegisterComplex(map);

            this.container = map.CreateContainer();
        }
        public void Prepare()
        {
            var map = new DependencyMap();

            map.AddSingletonService <ISingleton, Singleton>();
            map.AddService <ITransient, Transient>();
            map.AddService <ICombined, Combined>();

            this.container = map.CreateContainer();
        }
Esempio n. 11
0
        public override void Prepare()
        {
            var map = new DependencyMap();

            map.AddSingletonService<ISingleton, Singleton>();
            map.AddService<ITransient, Transient>();
            map.AddService<ICombined, Combined>();

            this.container = map.CreateContainer();
        }
        public override void Prepare()
        {
            var map = new DependencyMap();

            RegisterDummies(map);
            RegisterStandard(map);
            RegisterComplex(map);

            this.container = map.CreateContainer();
        }
Esempio n. 13
0
        /// <summary>
        /// Creates the instance with the given <paramref name="container"/> and functor <paramref name="key"/>.
        /// </summary>
        /// <param name="key">The unique functor id.</param>
        /// <param name="container">The host container.</param>
        /// <returns>A new object instance.</returns>
        public static object CreateInstance(string key, IMicroContainer container)
        {
            var functor = GetFunctor(key);

            if (functor == null)
            {
                return(null);
            }

            return(functor(container));
        }
        /// <summary>
        /// Adds a service instance to the container.
        /// </summary>
        /// <param name="container">The container instance itself.</param>
        /// <param name="serviceType">The service type.</param>
        /// <param name="serviceName">The service name.</param>
        /// <param name="serviceInstance">The service instance.</param>
        public static void AddService(this IMicroContainer container, Type serviceType, string serviceName, object serviceInstance)
        {
            var targetContainer = container;

            while (targetContainer.NextContainer != null)
            {
                targetContainer = targetContainer.NextContainer;
            }

            targetContainer.NextContainer = new InstanceContainer(serviceType, serviceName, serviceInstance);
        }
Esempio n. 15
0
        public static void EnableMvc(this IMicroContainer container)
        {
            // Replace the default controller factory with Hiro
            var factory = new HiroControllerFactory(container);

            ControllerBuilder.Current.SetControllerFactory(factory);

            // Use Hiro as the dependency resolver
            var resolver = new HiroDependencyResolver(container);

            DependencyResolver.SetResolver(resolver);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
        }
        private void BuildBaseContainer()
        {
            _serviceMap = _dependencyContainer.GetServiceMap();

            var map = new DependencyMap();

            foreach (var dependency in _serviceMap.Keys)
            {
                map.AddService(dependency, _serviceMap[dependency]);
            }

            _baseContainer = map.CreateContainer();
        }
Esempio n. 17
0
        public Hiro()
        {
            var map = new DependencyMap();
            map.AddSingletonService<Game, Game>();
            map.AddService<Player, Player>();
            map.AddService<Gun, Gun>();
            map.AddService<Bullet, Bullet>();

            Func<IMicroContainer, Bullet> createBullet = c => c.GetInstance<Bullet>();
            Func<IMicroContainer, Func<Bullet>> createBulletFunctor = c => () => createBullet(c);
            map.AddService(createBulletFunctor);

            container = map.CreateContainer();
        }
Esempio n. 18
0
        public Hiro()
        {
            var map = new DependencyMap();

            map.AddSingletonService <Game, Game>();
            map.AddService <Player, Player>();
            map.AddService <Gun, Gun>();
            map.AddService <Bullet, Bullet>();

            Func <IMicroContainer, Bullet>         createBullet        = c => c.GetInstance <Bullet>();
            Func <IMicroContainer, Func <Bullet> > createBulletFunctor = c => () => createBullet(c);

            map.AddService(createBulletFunctor);

            container = map.CreateContainer();
        }
Esempio n. 19
0
        static HiroUseCase()
        {
            var map = new DependencyMap();
            map.AddService(typeof(IWebService), typeof(WebService));
            map.AddService(typeof(IAuthenticator), typeof(Authenticator));
            map.AddService(typeof(IStockQuote), typeof(StockQuote));
            map.AddService(typeof(IDatabase), typeof(Database));
            map.AddService(typeof(IErrorHandler), typeof(ErrorHandler));
            map.AddService(typeof(ILogger), typeof(Logger));

            IContainerCompiler compiler = new ContainerCompiler();
            var assembly = compiler.Compile(map);

            var loadedAssembly = assembly.ToAssembly();
            var containerType = loadedAssembly.GetTypes()[0];
            container = (IMicroContainer)Activator.CreateInstance(containerType);
        }
Esempio n. 20
0
        /// <summary>
        /// Compiles and instantiates a container instance using the current dependencies in the dependency map.
        /// </summary>
        /// <returns>A <see cref="IMicroContainer"/> instance.</returns>
        public IMicroContainer CreateContainer()
        {
            if (ContainerCompiler == null)
            {
                throw new NullReferenceException("The ContainerCompiler property cannot be null");
            }

            var      compiler       = ContainerCompiler;
            var      assembly       = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", this);
            Assembly loadedAssembly = CompileContainerAssembly(assembly);

            var containerTypes = new List <System.Type>();

            foreach (var type in loadedAssembly.GetTypes())
            {
                if (!typeof(IMicroContainer).IsAssignableFrom(type))
                {
                    continue;
                }

                containerTypes.Add(type);
            }

            var containerType = containerTypes[0];
            var result        = (IMicroContainer)Activator.CreateInstance(containerType);

            // Append the generic containers
            var containers = new Queue <IMicroContainer>(_genericContainers);

            IMicroContainer iterator = result;

            while (containers.Count > 0)
            {
                var currentContainer = containers.Dequeue();
                if (iterator.NextContainer == null)
                {
                    iterator.NextContainer = currentContainer;
                    iterator = iterator.NextContainer;
                }
            }

            return(result);
        }
        public HiroUseCase()
        {
            var map = new DependencyMap();

            map.AddService(typeof(IWebService), typeof(WebService));
            map.AddService(typeof(IAuthenticator), typeof(Authenticator));
            map.AddService(typeof(IStockQuote), typeof(StockQuote));
            map.AddService(typeof(IDatabase), typeof(Database));
            map.AddService(typeof(IErrorHandler), typeof(ErrorHandler));
            map.AddService(typeof(ILogger), typeof(Logger));

            IContainerCompiler compiler = new ContainerCompiler();
            var assembly = compiler.Compile(map);

            var loadedAssembly = assembly.ToAssembly();
            var containerType  = loadedAssembly.GetTypes()[0];

            container = (IMicroContainer)Activator.CreateInstance(containerType);
        }
Esempio n. 22
0
 public HiroTypeFactory(IMicroContainer container)
 {
     this.container = container;
 }
Esempio n. 23
0
        /// <summary>
        /// Obtains a cache instance from the given container.
        /// </summary>
        /// <param name="container">The container that contains the <see cref="ICache"/> instance.</param>
        /// <returns>A cache that contains the services to be cached.</returns>
        public static ICache GetCache(IMicroContainer container)
        {
            var cache = container.GetInstance<ICache>();

            return cache;
        }
Esempio n. 24
0
 public void Initialize(IMicroContainer container)
 {
     HasBeenCalled = true;
 }
 public SampleContainerAwareType(IMicroContainer container)
 {
     _container = container;
 }
Esempio n. 26
0
        /// <summary>
        /// Obtains a cache instance from the given container.
        /// </summary>
        /// <param name="container">The container that contains the <see cref="ICache"/> instance.</param>
        /// <returns>A cache that contains the services to be cached.</returns>
        public static ICache GetCache(IMicroContainer container)
        {
            var cache = container.GetInstance <ICache>();

            return(cache);
        }
        private void BuildBaseContainer()
        {
            _serviceMap = _dependencyContainer.GetServiceMap();

            var map = new DependencyMap();
            foreach (var dependency in _serviceMap.Keys)
            {
                map.AddService(dependency, _serviceMap[dependency]);
            }

            _baseContainer = map.CreateContainer();
        }
 /// <summary>
 /// Adds a service instance to the container.
 /// </summary>
 /// <param name="container">The container instance itself.</param>
 /// <param name="serviceType">The service type.</param>
 /// <param name="serviceInstance">The service instance.</param>
 public static void AddService(this IMicroContainer container, Type serviceType, object serviceInstance)
 {
     AddService(container, serviceType, null, serviceInstance);
 }
Esempio n. 29
0
 public HiroControllerFactory(IMicroContainer container)
 {
     _container = container;
 }
 /// <summary>
 /// Adds a service instance to the container.
 /// </summary>
 /// <typeparam name="T">The service type.</typeparam>
 /// <param name="container">The container instance itself.</param>
 /// <param name="serviceInstance">The service instance.</param>
 public static void AddService <T>(this IMicroContainer container, T serviceInstance)
 {
     container.AddService <T>(null, serviceInstance);
 }
 /// <summary>
 /// Obtains an object instance that matches the given service type
 /// and <paramref name="key">service name</paramref>.
 /// </summary>
 /// <typeparam name="T">The service type.</typeparam>
 /// /// <param name="container">The target container.</param>
 /// <param name="key">The name of the service itself.</param>
 /// <returns>An object instance that matches the given service description.</returns>
 public static T GetInstance <T>(this IMicroContainer container, string key)
 {
     return((T)container.GetInstance(typeof(T), key));
 }
 public override void Dispose()
 {
     // Allow the container and everything it references to be garbage collected.
     this.container = null;
 }
Esempio n. 33
0
 public void Initialize(IMicroContainer container)
 {
     Container = container;
     NumberOfTimesInitialized++;
 }
 public override void Dispose()
 {
     // Allow the container and everything it references to be disposed.
     this.container = null;
 }
 public HiroTypeFactory(IMicroContainer container)
 {
     this.container = container;
 }
Esempio n. 36
0
 public void Initialize(IMicroContainer container)
 {
     Container = container;
     NumberOfTimesInitialized++;
 }
Esempio n. 37
0
 public SampleContainerAwareType(IMicroContainer container)
 {
     _container = container;
 }
Esempio n. 38
0
 public HiroDependencyResolver(IMicroContainer container)
 {
     _container = container;
 }
 /// <summary>
 /// Obtains an object instance that matches the given service type.
 /// </summary>
 /// <typeparam name="T">The service type.</typeparam>
 /// <param name="container">The target container.</param>
 /// <returns>An object instance that matches the given service description.</returns>
 public static T GetInstance <T>(this IMicroContainer container)
 {
     return(container.GetInstance <T>(null));
 }
Esempio n. 40
0
 public HiroControllerFactory(IMicroContainer container)
 {
     _container = container;
 }
Esempio n. 41
0
 public void Initialize(IMicroContainer container)
 {
     HasBeenCalled = true;
 }