Exemplo n.º 1
0
        public bool TryGet(Type requiredType, out IDependencyDefinition definition)
        {
            Ensure.NotNull(requiredType, "requiredType");

            DependencyDefinition result;

            if (definitionByRequiredType.TryGetValue(requiredType, out result))
            {
                definition = result.Clone(isResolvable(requiredType));
                return(true);
            }

            if (parentCollection != null)
            {
                if (parentCollection.TryGet(requiredType, out definition))
                {
                    if (!definition.IsResolvable && isResolvable(requiredType))
                    {
                        definition = new DependencyDefinition(definition.RequiredType, definition.Lifetime, definition.Target, true);
                    }

                    return(true);
                }
            }

            definition = null;
            return(false);
        }
 private void FillDependencyProperties(DependencyDefinition definition, object instance)
 {
     foreach (PropertyInfo propertyInfo in definition.DependencyProperties)
     {
         propertyInfo.SetValue(instance, Resolve(propertyInfo.PropertyType));
     }
 }
        internal bool TryGetInternal(Type requiredType, out DependencyDefinition definition)
        {
            string key = requiredType.FullName;

            if (definitionByKey.TryGetValue(key, out definition))
            {
                definition = definition.Clone(true);
                return(true);
            }

            foreach (List <DependencyDefinition> items in definitionByScopeName.Values)
            {
                foreach (DependencyDefinition item in items)
                {
                    if (item.RequiredType == requiredType)
                    {
                        definition = item.Clone(false);
                        return(true);
                    }
                }
            }

            if (parentCollection != null)
            {
                if (parentCollection.TryGetInternal(requiredType, out definition))
                {
                    return(true);
                }
            }

            definition = null;
            return(false);
        }
        private void AddDefinitionToScope(string scopeName, DependencyDefinition definition)
        {
            List <DependencyDefinition> list;

            if (!definitionByScopeName.TryGetValue(scopeName, out list))
            {
                definitionByScopeName[scopeName] = list = new List <DependencyDefinition>();
            }

            list.Add(definition);
        }
        /// <summary>
        /// Tries to find dependency properties on implementation type.
        /// </summary>
        /// <param name="definition">Dependency definition.</param>
        private void FindDependencyProperties(DependencyDefinition definition)
        {
            Type targetType = definition.Target as Type;

            if (targetType == null)
            {
                return;
            }

            foreach (PropertyInfo property in targetType.GetProperties())
            {
                if (property.GetCustomAttributes(typeof(DependencyAttribute)).Any())
                {
                    definition.DependencyProperties.Add(property);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds definition, if <paramref name="definition"/>  is for current scope, registers it the unity container.
        /// Otherwise stores <paramref name="definition"/> in current mapping collection to any child scopes.
        /// </summary>
        /// <param name="definition">New dependency definition.</param>
        /// <returns>Self (for fluency).</returns>
        public Mapper Add(DependencyDefinition definition)
        {
            // If lifetime is current scope, register to the unity container.
            if (!definition.Lifetime.IsNamed || definition.Lifetime.Name == scopeName)
            {
                LifetimeManager lifetimeManager = CreateLifetimeManager(definition.Lifetime, definition.Target is Type);
                Register(definition.RequiredType, lifetimeManager, definition.Target);
            }

            // If lifetime is not transient, store for child scopes.
            if (definition.Lifetime.IsNamed)
            {
                collection.Add(definition);
            }

            return(this);
        }
        private object Build(DependencyDefinition definition)
        {
            // For transient definition, create new instance (always).
            if (definition.Lifetime.IsTransient)
            {
                return(CreateNewInstanceFromDefinition(definition));
            }

            // For scoped definition, try get instance or create new one.
            object instance = instances.TryGetObject(definition.Key);

            if (instance == null)
            {
                instance = CreateNewInstanceFromDefinition(definition);
                instances.AddObject(definition.Key, instance);
            }

            return(instance);
        }
        private void AddDefinition(DependencyDefinition definition)
        {
            // If definition is usable for current scope, add it to the definitionByKey.
            if (definition.Lifetime.IsTransient || (definition.Lifetime.IsScoped && (definition.Lifetime.IsNamed && definition.Lifetime.Name == ScopeName) || !definition.Lifetime.IsNamed))
            {
                definitionByKey[definition.Key] = definition;
            }

            if (definition.Lifetime.IsNamed)
            {
                // If definition is targeted for other scope, add it to the definitionByScopeName.
                AddDefinitionToScope(definition.Lifetime.Name, definition);
            }
            else if (definition.Lifetime.IsScoped)
            {
                // If definition is targeted to any scope, add it to the definitionByScopeName with empty scope name.
                AddDefinitionToScope(String.Empty, definition);
            }
        }
Exemplo n.º 9
0
        public DependencyDefinition Clone(bool isResolvable)
        {
            DependencyDefinition result;

            if (HasConstructorInfo)
            {
                result = new DependencyDefinition(RequiredType, Lifetime, Target, ConstructorInfo);
            }
            else
            {
                result = new DependencyDefinition(RequiredType, Lifetime, Target);
            }

            if (DependencyProperties.Count > 0)
            {
                result.DependencyProperties.AddRange(DependencyProperties);
            }

            result.IsResolvable = isResolvable;
            return(result);
        }
        private object CreateNewInstanceFromDefinition(DependencyDefinition definition)
        {
            // When mapped to the implementation type or itself (type).
            if (definition.HasConstructorInfo)
            {
                object instance = CreateInstance(definition.ConstructorInfo);
                FillDependencyProperties(definition, instance);
                return(instance);
            }

            // When mapped to the instance of activator.
            IFactory <object> activator = instances.TryGetFactory(definition.Key);

            if (activator != null)
            {
                return(activator.Create());
            }

            // When mapped to the type of activator.
            //TODO: Implement.
            throw new NotImplementedException();
        }
        /// <summary>
        /// Adds NAME SCOPED dependency definition.
        /// </summary>
        /// <param name="definition">NAME SCOPED definition to add.</param>
        /// <returns>Self (for fluency).</returns>
        /// <exception cref="InvalidOperationException">If <paramref name="definition"/> is not scoped.</exception>
        internal MapperCollection Add(DependencyDefinition definition)
        {
            string scopeName;

            if (definition.Lifetime.IsNamed)
            {
                scopeName = definition.Lifetime.Name;
            }
            else
            {
                throw Ensure.Exception.InvalidOperation("MappingCollection supports only scoped or name-scoped registrations.");
            }

            List <DependencyDefinition> models;

            if (!storage.TryGetValue(scopeName, out models))
            {
                storage[scopeName] = models = new List <DependencyDefinition>();
            }

            models.Add(definition);
            return(this);
        }
Exemplo n.º 12
0
 public IDependencyDefinitionCollection Add(Type requiredType, DependencyLifetime lifetime, object target)
 {
     mapper.Add(definitionByRequiredType[requiredType] = new DependencyDefinition(requiredType, lifetime, target));
     return(this);
 }
        //TODO: Implement using registered features...
        public IDependencyDefinitionCollection Add(Type requiredType, DependencyLifetime lifetime, object target)
        {
            Ensure.NotNull(requiredType, "requiredType");
            Ensure.NotNull(target, "target");

            // Target is type to map to.
            Type targetType = target as Type;

            if (targetType != null)
            {
                if (targetType.IsInterface)
                {
                    throw new DependencyRegistrationFailedException(String.Format("Target can't be interface. Mapping '{0}' to '{1}'.", requiredType.FullName, targetType.FullName));
                }

                if (targetType.IsAbstract)
                {
                    throw new DependencyRegistrationFailedException(String.Format("Target can't be abstract class. Mapping '{0}' to '{1}'.", requiredType.FullName, targetType.FullName));
                }

                ConstructorInfo constructorInfo = FindBestConstructor(targetType);
                if (constructorInfo == null)
                {
                    throw new DependencyRegistrationFailedException(String.Format("Target must has public contructor. Mapping '{0}' to '{1}'.", requiredType.FullName, targetType.FullName));
                }

                DependencyDefinition definition = new DependencyDefinition(
                    requiredType,
                    lifetime,
                    targetType,
                    constructorInfo
                    );
                FindDependencyProperties(definition);
                AddDefinition(definition);

                return(this);
            }

            // Target is activator.
            IFactory <object> targetActivator = target as IFactory <object>;

            if (targetActivator != null)
            {
                DependencyDefinition definition = new DependencyDefinition(
                    requiredType,
                    lifetime,
                    targetActivator
                    );
                instances.AddFactory(definition.Key, targetActivator);
                AddDefinition(definition);
                return(this);
            }

            // Target is instance of required type.
            targetType = target.GetType();
            if (requiredType.IsAssignableFrom(targetType))
            {
                DependencyDefinition definition = new DependencyDefinition(
                    requiredType,
                    lifetime,
                    target
                    );
                instances.AddObject(definition.Key, target);
                AddDefinition(definition);
                return(this);
            }

            // Nothing else is supported.
            throw Ensure.Exception.InvalidOperation("Not supported target type '{0}'.", target.GetType().FullName);
        }