Esempio n. 1
0
 public void DispatchMappingOverrideEvent(MappingId mappingId, InjectionMapping injectionMapping)
 {
     if (_mappingOverride != null)
     {
         _mappingOverride(mappingId, injectionMapping);
     }
 }
Esempio n. 2
0
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private void OnAddedSingleton(MappingId mappingId, object singleton)
        {
            if (_addedSingleton != null)
            {
                _addedSingleton(mappingId, singleton);
            }
        }
Esempio n. 3
0
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private InjectionMapping CreateMapping(MappingId mappingId)
        {
            if (_mappingsInProcess.ContainsKey(mappingId))
            {
                throw new InjectorException("Can't change a mapping from inside a listener to it's creation event");
            }

            _mappingsInProcess [mappingId] = true;

            if (_preMappingCreate != null)
            {
                _preMappingCreate(mappingId);
            }

            InjectionMapping mapping = new InjectionMapping(this, mappingId);

            _mappings [mappingId] = mapping;

            object sealKey = mapping.Seal();

            if (_postMappingCreate != null)
            {
                _postMappingCreate(mappingId, mapping);
            }

            _mappingsInProcess.Remove(mappingId);
            mapping.Unseal(sealKey);
            return(mapping);
        }
Esempio n. 4
0
        public DependencyProvider GetProvider(
            MappingId mappingId, bool fallbackToDefault = true)
        {
            DependencyProvider softProvider = null;
            Injector           injector     = this;

            while (injector != null)
            {
                DependencyProvider provider;
                if (injector.providerMappings.TryGetValue(mappingId, out provider))
                {
                    if (provider is SoftDependencyProvider)
                    {
                        softProvider = provider;
                        injector     = injector.parentInjector;
                        continue;
                    }
                    if (provider is LocalOnlyProvider && injector != this)
                    {
                        injector = injector.parentInjector;
                        continue;
                    }
                    return(provider);
                }
                injector = injector.parentInjector;
            }
            if (softProvider != null)
            {
                return(softProvider);
            }
            return(fallbackToDefault ? GetDefaultProvider(mappingId, true) : null);
        }
Esempio n. 5
0
 private void OnRemovedSingleton(MappingId mappingId)
 {
     if (_removedSingeton != null)
     {
         _removedSingeton(mappingId);
     }
 }
Esempio n. 6
0
 private void PostMappingRemove(MappingId mappingId)
 {
     if (_singletonInstances.ContainsKey(mappingId))
     {
         RemoveSingleton(mappingId);
     }
 }
		/*============================================================================*/
		/* Private Functions                                                          */
		/*============================================================================*/

		private void OnAddedSingleton(MappingId mappingId, object singleton)
		{
			if (_addedSingleton != null) 
			{
				_addedSingleton (mappingId, singleton);
			}
		}
Esempio n. 8
0
 public void DispatchPostMappingChangeEvent(MappingId mappingId, InjectionMapping injectionMapping)
 {
     if (_postMappingChange != null)
     {
         _postMappingChange(mappingId, injectionMapping);
     }
 }
Esempio n. 9
0
 private void AddSingleton(MappingId mappingId, object singleton)
 {
     _singletonInstances [mappingId] = singleton;
     if (_addedSingleton != null)
     {
         _addedSingleton(mappingId, singleton);
     }
 }
Esempio n. 10
0
 private void RemoveSingleton(MappingId mappingId)
 {
     _singletonInstances.Remove(mappingId);
     if (_removedSingleton != null)
     {
         _removedSingleton(mappingId);
     }
 }
 public TypeDescription AddPropertyInjection(MappingId mappingId, PropertyInfo propertyInfo, bool optional = false)
 {
     if (_postConstructAdded)
     {
         throw new InjectorException("Can't add injection point after post construct method");
     }
     AddInjectionPoint(new PropertyInjectionPoint(mappingId, propertyInfo, optional));
     return(this);
 }
Esempio n. 12
0
        /// <summary>
        /// Adds the or update.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TDestination">The type of the destination.</typeparam>
        /// <param name="mapping">The mapping.</param>
        public void AddOrUpdate <TSource, TDestination>(Action <TSource, TDestination> mapping)
        {
            Guard.Against.Null(() => mapping);

            var id = new MappingId {
                Source = typeof(TSource), Destination = typeof(TDestination), IsAction = true
            };

            this.mappings[id] = mapping;
        }
Esempio n. 13
0
        public InjectionMapping GetMapping(Type type, object key = null)
        {
            MappingId        mappingId = new MappingId(type, key);
            InjectionMapping mapping;

            if (!_mappings.TryGetValue(mappingId, out mapping))
            {
                throw new InjectorMissingMappingException("Error while retrieving an injector mapping: "
                                                          + "No mapping defined for dependency " + mappingId);
            }
            return(mapping);
        }
Esempio n. 14
0
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private void PostMappingChange(MappingId mappingId, InjectionMapping mapping)
        {
            DependencyProvider dp = mapping.GetProvider();

            if (dp is SingletonProvider)
            {
                dp.PostApply += HandlePostApply;
                _dependencyMappingIds [dp] = mappingId;
            }
            else if (dp is ValueProvider)
            {
                AddSingleton(mappingId, _injector.GetInstance(mappingId.type, mappingId.key));
            }
        }
        public override string ToString()
        {
            string id;

            if (MappingId?.GetType().IsArray == true)
            {
                id = string.Join(",", ((IEnumerable)MappingId).Cast <object>());
            }
            else
            {
                id = MappingId?.ToString();
            }
            return($"{this.GetType()}/id[{id}]");
        }
Esempio n. 16
0
        protected virtual object[] GatherParameterValues(Type targetType, Injector injector)
        {
            if (_methodBase == null)
            {
                return(new object[0]);
            }
            List <object> parameters = new List <object> ();

            ParameterInfo[] parameterInfos = _methodBase.GetParameters();
            int             length         = parameterInfos.Length;

            for (int i = 0; i < length; i++)
            {
                Type      parameterType = parameterInfos [i].ParameterType;
                MappingId mappingId;
                if (_keys != null && _keys.Length > i && _keys[i] != null)
                {
                    mappingId = new MappingId(parameterType, _keys[i]);
                }
                else
                {
                    mappingId = new MappingId(parameterType);
                }

                DependencyProvider provider = injector.GetProvider(mappingId);
                if (provider == null)
                {
                    if (parameterInfos [i].IsOptional)
                    {
                        parameters.Add(parameterInfos [i].DefaultValue);
                        continue;
                    }
                    if (_optional)
                    {
                        parameters.Add(null);
                        continue;                         //TODO: Check optional parameters are in order (last) for this break to work, else use continue
                    }
                    throw(new InjectorMissingMappingException(
                              "Injector is missing a mapping to handle constructor injection into target type '"
                              + targetType.FullName + "'. \nTarget dependency: " + parameterType.FullName +
                              ", method: " + _methodBase.Name + ", parameter: " + (i + 1)
                              ));
                }
                parameters.Add(provider.Apply(targetType, injector, injectParameters));
            }
            return(parameters.ToArray());
        }
Esempio n. 17
0
        /// <summary>
        /// Tries the get.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TDestination">The type of the destination.</typeparam>
        /// <param name="mapping">The mapping.</param>
        /// <returns>Returns <c>true</c> if the mapping exists, otherwise <c>false</c>.</returns>
        public bool TryGet <TSource, TDestination>(out Action <TSource, TDestination> mapping)
        {
            var id = new MappingId {
                Source = typeof(TSource), Destination = typeof(TDestination), IsAction = true
            };

            var value = default(object);

            if (!this.mappings.TryGetValue(id, out value))
            {
                mapping = null;
                return(false);
            }

            mapping = value as Action <TSource, TDestination>;
            return(true);
        }
        private void AddPropertyInjectionPoints(TypeDescription description, Type type)
        {
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object[] injections = property.GetCustomAttributes(INJECT_ATTRIBUTE_TYPE, true);
                if (injections.Length == 0)
                {
                    continue;
                }

                Inject    attr      = injections [0] as Inject;
                object    key       = attr.name;
                MappingId mappingId = new MappingId(property.PropertyType, key);
                PropertyInjectionPoint injectionPoint = new PropertyInjectionPoint(mappingId, property, attr.optional);                  //injectParameters);
                description.AddInjectionPoint(injectionPoint);
            }
        }
Esempio n. 19
0
        private DependencyProvider GetDefaultProvider(
            MappingId mappingId, bool consultParents)
        {
            if (mappingId.key == null && BASE_TYPES.ContainsKey(mappingId.type))
            {
                return(null);
            }

            if (_fallbackProvider != null && _fallbackProvider.PrepareNextRequest(mappingId))
            {
                return(_fallbackProvider);
            }
            if (consultParents && !_blockParentFallbackProvider && _parentInjector != null)
            {
                return(_parentInjector.GetDefaultProvider(mappingId, consultParents));
            }
            return(null);
        }
        private void AddFieldInjectionPoints(TypeDescription description, Type type)
        {
            FieldInfo[] fields = type.GetFields();
            foreach (FieldInfo field in fields)
            {
                object[] injections = field.GetCustomAttributes(INJECT_ATTRIBUTE_TYPE, true);
                if (injections.Length == 0)
                {
                    continue;
                }

                Inject              attr           = injections [0] as Inject;
                object              key            = attr.name;
                MappingId           mappingId      = new MappingId(field.FieldType, key);
                FieldInjectionPoint injectionPoint = new FieldInjectionPoint(mappingId,
                                                                             field, attr.optional);// injectParameters.optional == 'true', injectParameters);
                description.AddInjectionPoint(injectionPoint);
            }
        }
Esempio n. 21
0
        public void Unmap(Type type, object key = null)
        {
            MappingId        mappingId = new MappingId(type, key);
            InjectionMapping mapping;

            _mappings.TryGetValue(mappingId, out mapping);
            if (mapping != null && mapping.isSealed)
            {
                throw new InjectorException("Can't unmap a sealed mapping");
            }
            if (mapping == null)
            {
                throw new InjectorException("Error while removing an injector mapping: " +
                                            "No mapping defined for dependency " + mappingId);
            }
            mapping.GetProvider().Destroy();
            _mappings.Remove(mappingId);
            providerMappings.Remove(mappingId);
            if (_postMappingRemove != null)
            {
                _postMappingRemove(mappingId);
            }
        }
Esempio n. 22
0
        public object GetInstance(Type type, object key = null, Type targetType = null)
        {
            MappingId          mappingId = new MappingId(type, key);
            DependencyProvider provider  = GetProvider(mappingId);

            if (provider == null)
            {
                provider = GetDefaultProvider(mappingId, true);
            }

            if (provider != null)
            {
//				ConstructorInjectionPoint ctor = _typeDescriptor.GetDescription(type).ctor; //TODO: Make this CTOR
                ConstructorInjectionPoint ctor = null;
                return(provider.Apply(targetType, this, ctor != null ? ctor.injectParameters : null));
            }

            string fallbackMessage = _fallbackProvider != null
                                ? "the fallbackProvider, '" + _fallbackProvider + "', was unable to fulfill this request."
                                : "the injector has no fallbackProvider.";

            throw new InjectorMissingMappingException("No mapping found for request " + mappingId
                                                      + " and " + fallbackMessage);
        }
 private void OnPreMappingCreate(MappingId mappingId)
 {
     _logger.Debug("Mapping event PRE_MAPPING_CREATE. Mapped type: {1}. Mapped name: {2}",
                   mappingId.type, mappingId.key);
 }
Esempio n. 24
0
        /// <summary>
        /// Indicates whether the injector can supply a response for the specified dependency either
        /// by using a mapping of its own or by querying one of its ancestor injectors.
        /// </summary>
        /// <param name="key">The name of the dependency under query</param>
        /// <typeparam name="T">The type of the dependency under query</typeparam>
        /// <returns><c>true</c>, if the dependency can be satisfied,, <c>false</c> if not.</returns>

        public bool Satisfies(Type type, object key = null)
        {
            MappingId mappingId = new MappingId(type, key);

            return(GetProvider(mappingId, true) != null);
        }
 private void OnPostMappingChange(MappingId mappingId, InjectionMapping instanceType)
 {
     _logger.Debug("Mapping event POST_MAPPING_CHANGE. Mapped type: {1}. Mapped name: {2}",
                   mappingId.type, mappingId.key);
 }
 private void OnMappingOverride(MappingId mappingId, InjectionMapping instanceType)
 {
     _logger.Debug("Mapping event MAPPING_OVERRIDE. Mapped type: {1}. Mapped name: {2}",
                   mappingId.type, mappingId.key);
 }
		private void OnPreMappingCreate (MappingId mappingId)
		{
			_logger.Debug("Mapping event PRE_MAPPING_CREATE. Mapped type: {1}. Mapped name: {2}",
				mappingId.type, mappingId.key);
		}
Esempio n. 28
0
        public bool HasDirectMapping(Type type, object key = null)
        {
            MappingId mappingId = new MappingId(type, key);

            return(_mappings.ContainsKey(mappingId));
        }
		private void AddSingleton(MappingId mappingId, object singleton)
		{
			_singletonInstances [mappingId] = singleton;
			if (_addedSingleton != null) 
			{
				_addedSingleton(mappingId, singleton);
			}
		}
		/*============================================================================*/
		/* Private Functions                                                          */
		/*============================================================================*/
		
		private void PostMappingChange (MappingId mappingId, InjectionMapping mapping)
		{
			DependencyProvider dp = mapping.GetProvider ();
			if (dp is SingletonProvider) 
			{
				dp.PostApply += HandlePostApply;
				_dependencyMappingIds [dp] = mappingId;
			} 
			else if (dp is ValueProvider) 
			{
				AddSingleton(mappingId, _injector.GetInstance(mappingId.type, mappingId.key));
			}
		}
		private void RemoveSingleton(MappingId mappingId)
		{
			_singletonInstances.Remove (mappingId);
			if (_removedSingleton != null) 
			{
				_removedSingleton(mappingId);
			}
		}
		private void OnPostMappingChange(MappingId mappingId, InjectionMapping instanceType)
		{
			_logger.Debug("Mapping event POST_MAPPING_CHANGE. Mapped type: {1}. Mapped name: {2}",
				mappingId.type, mappingId.key);
		}
Esempio n. 33
0
        public InjectionMapping Map(Type type, object key = null)
        {
            MappingId mappingId = new MappingId(type, key);

            return(_mappings.ContainsKey(mappingId) ? _mappings [mappingId] : CreateMapping(mappingId));
        }
Esempio n. 34
0
 public FieldInjectionPoint(MappingId mappingId, FieldInfo fieldInfo, bool optional)
 {
     _mappingId = mappingId;
     _fieldInfo = fieldInfo;
     _optional  = optional;
 }
		private void OnRemovedSingleton(MappingId mappingId)
		{
			if (_removedSingeton != null) 
			{
				_removedSingeton (mappingId);
			}
		}
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        void MappingOverrideHandler(MappingId mappingId, InjectionMapping instanceType)
        {
            throw new InjectorException("Injector mapping override for type " +
                                        mappingId.type + " with name " + mappingId.key);
        }
		/*============================================================================*/
		/* Private Functions                                                          */
		/*============================================================================*/

		void MappingOverrideHandler (MappingId mappingId, InjectionMapping instanceType)
		{
			throw new InjectorException("Injector mapping override for type " +
				mappingId.type + " with name " + mappingId.key);
		}
		private void PostMappingRemove (MappingId mappingId)
		{
			if (_singletonInstances.ContainsKey (mappingId)) 
			{
				RemoveSingleton(mappingId);
			}
		}
Esempio n. 39
0
        public bool HasMapping(Type type, object key = null)
        {
            MappingId mappingId = new MappingId(type, key);

            return(GetProvider(mappingId) != null);
        }
		private void OnMappingOverride(MappingId mappingId, InjectionMapping instanceType)
		{
			_logger.Debug("Mapping event MAPPING_OVERRIDE. Mapped type: {1}. Mapped name: {2}",
				mappingId.type, mappingId.key);
		}
		private void OnPostMappingRemove (MappingId mappingId)
		{
			_logger.Debug("Mapping event POST_MAPPING_REMOVE. Mapped type: {1}. Mapped name: {2}",
				mappingId.type, mappingId.key);
		}