Пример #1
0
        public void Configure <T>(Func <T> componentFactory, DependencyLifecycle dependencyLifecycle)
        {
            var componentType = typeof(T);

            if (HasComponent(componentType))
            {
                return;
            }

            var interfaces = GetAllServiceTypesFor(componentType);

            foreach (Type t in interfaces)
            {
                if (DefaultInstances.Contains(t))
                {
                    container.RegisterType(t, Guid.NewGuid().ToString(), GetLifetimeManager(dependencyLifecycle),
                                           new InjectionFactory(unityContainer => componentFactory()));
                }
                else
                {
                    container.RegisterType(t, GetLifetimeManager(dependencyLifecycle), new InjectionFactory(unityContainer => componentFactory()));
                    DefaultInstances.Add(t);
                }
            }
        }
        public object Build(Type typeToBuild)
        {
            if (!DefaultInstances.Contains(typeToBuild))
            {
                throw new ArgumentException(typeToBuild + " is not registered in the container");
            }

            return(container.Resolve(typeToBuild));
        }
        public IEnumerable <object> BuildAll(Type typeToBuild)
        {
            if (DefaultInstances.Contains(typeToBuild))
            {
                yield return(container.Resolve(typeToBuild));

                foreach (var component in container.ResolveAll(typeToBuild))
                {
                    yield return(component);
                }
            }
        }
Пример #4
0
        public void Configure <T>(Func <T> componentFactory, DependencyLifecycle dependencyLifecycle)
        {
            var componentType = typeof(T);

            if (HasComponent(componentType))
            {
                return;
            }

            container.RegisterType <T>(GetLifetimeManager(dependencyLifecycle),
                                       new InjectionFactory(unityContainer => componentFactory()));
            DefaultInstances.Add(componentType);
        }
Пример #5
0
        public void Configure(Type concreteComponent, DependencyLifecycle dependencyLifecycle)
        {
            if (HasComponent(concreteComponent))
            {
                return;
            }

            var interfaces = GetAllServiceTypesFor(concreteComponent);

            foreach (Type t in interfaces)
            {
                if (DefaultInstances.Contains(t))
                {
                    container.RegisterType(t, concreteComponent, Guid.NewGuid().ToString(), GetLifetimeManager(dependencyLifecycle));
                }
                else
                {
                    container.RegisterType(t, concreteComponent, GetLifetimeManager(dependencyLifecycle));
                    DefaultInstances.Add(t);
                }
            }
        }
 public void RegisterSingleton(Type lookupType, object instance)
 {
     DefaultInstances.Add(lookupType);
     container.RegisterInstance(lookupType, instance);
 }