/// <summary>
        /// Called during the chain of responsibility for a build operation.  Looks for the <see cref="IBuildKeyMappingPolicy"/>
        /// and if found maps the build key for the current operation.
        /// </summary>
        /// <param name="context">The context for the operation.</param>
        public override void PreBuildUp(IBuilderContext context)
        {
            IBuildKeyMappingPolicy policy = context.Policies.Get <IBuildKeyMappingPolicy>(context.BuildKey);

            if (policy != null)
            {
                context.BuildKey = policy.Map(context.BuildKey);
            }
        }
            public void RemoveIsDefault(Type serviceType, Type implementationType, string name)
            {
                NamedTypeBuildKey key = new NamedTypeBuildKey(serviceType);

                IBuildKeyMappingPolicy mappingPolicy = Context.Policies.Get <IBuildKeyMappingPolicy>(key);

                if (mappingPolicy != null)
                {
                    NamedTypeBuildKey mappedKey = mappingPolicy.Map(key, null);
                    if (string.Equals(mappedKey.Name, name))
                    {
                        Context.Policies.Clear <IBuildKeyMappingPolicy>(key);
                    }
                }
            }
Пример #3
0
        /// <summary>
        /// Called during the chain of responsibility for a build operation.  Looks for the <see cref="IBuildKeyMappingPolicy"/>
        /// and if found maps the build key for the current operation.
        /// </summary>
        /// <param name="context">The context for the operation.</param>
        public override object PreBuildUp(IBuilderContext context)
        {
            IBuildKeyMappingPolicy policy = context.PersistentPolicies.Get <IBuildKeyMappingPolicy>(context.OriginalBuildKey.Type,
                                                                                                    context.OriginalBuildKey.Name, out _)
                                            ?? (context.OriginalBuildKey.Type.GetTypeInfo().IsGenericType
                                          ? context.Policies.Get <IBuildKeyMappingPolicy>(context.OriginalBuildKey.Type.GetGenericTypeDefinition(),
                                                                                          context.OriginalBuildKey.Name, out _)
                                          : null);

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

            context.BuildKey = policy.Map(context.BuildKey, context);
            return(null);
        }
        public override object BuildUp(IBuilderContext context,
                                       object buildKey,
                                       object existing)
        {
            IBuildKeyMappingPolicy policy = context.Policies.Get <IBuildKeyMappingPolicy>(buildKey);

            if (policy != null)
            {
                object newBuildKey = policy.Map(buildKey);

                Type originalType;
                Type newType;

                if (TryGetTypeFromBuildKey(newBuildKey, out newType) &&
                    TryGetTypeFromBuildKey(buildKey, out originalType) &&
                    originalType.IsGenericType)
                {
                    buildKey = newType.MakeGenericType(originalType.GetGenericArguments());
                }
                else
                {
                    buildKey = newType;
                }
            }

            return(base.BuildUp(context, buildKey, existing));

            /*
             * ITypeMappingPolicy policy = context.Policies.Get<ITypeMappingPolicy>(typeToBuild, idToBuild);
             *
             * if (policy != null)
             * {
             * DependencyResolutionLocatorKey resolution = policy.Map(new DependencyResolutionLocatorKey(typeToBuild, idToBuild));
             *
             * if (resolution.Type.IsGenericType)
             *     typeToBuild = resolution.Type.MakeGenericType(typeToBuild.GetGenericArguments());
             * else
             *     typeToBuild = resolution.Type;
             *
             * idToBuild = resolution.ID;
             * }
             *
             * return base.BuildUp(context, typeToBuild, existing, idToBuild);
             */
        }
Пример #5
0
        /// <summary>
        /// Called during the chain of responsibility for a build operation.  Looks for the <see cref="IBuildKeyMappingPolicy"/>
        /// and if found maps the build key for the current operation.
        /// </summary>
        /// <param name="context">The context for the operation.</param>
        public override void PreBuildUp(IBuilderContext context)
        {
            IBuildKeyMappingPolicy policy = (IBuildKeyMappingPolicy)context.PersistentPolicies
                                            .Get(typeof(IBuildKeyMappingPolicy), context.OriginalBuildKey, out _);

            if (null == policy)
            {
                return;
            }

            var existing = (policy as IDependencyResolverPolicy)?.Resolve(context);

            if (existing != null)
            {
                context.Existing      = existing;
                context.BuildComplete = true;
                return;
            }

            context.BuildKey = policy.Map(context.BuildKey, context);
        }