/// <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);
            }
        }
Пример #2
0
        /// <summary>
        /// Evaluates if a specified type was registered in the container.
        /// </summary>
        /// <param name="container">The container to check if the type was registered in.</param>
        /// <param name="type">The type to check if it was registered.</param>
        /// <returns><see langword="true" /> if the <paramref name="type"/> was registered with the container.</returns>
        /// <remarks>
        /// In order to use this extension, you must first call <see cref="UnityExtensions.AddNewExtension{TExtension}"/>
        /// and specify <see cref="UnityContainerExtension"/> as the extension type.
        /// </remarks>
        public static bool IsTypeRegistered(IUnityContainer container, Type type)
        {
            UnityBootstrapperExtension extension = container.Configure <UnityBootstrapperExtension>();

            if (extension == null)
            {
                //Extension was not added to the container.
                return(false);
            }
            IBuildKeyMappingPolicy policy = extension.Context.Policies.Get <IBuildKeyMappingPolicy>(new NamedTypeBuildKey(type));

            return(policy != null);
        }
            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);
                    }
                }
            }
Пример #4
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);
             */
        }
Пример #6
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);
        }