Exemple #1
0
        /// <summary>
        /// Creates a child container with all the registrations from the parent container intercepted by globalInterceptors
        /// and attribute interceptors
        /// </summary>
        /// <param name="unityContainer"></param>
        /// <param name="globalInterceptors"></param>
        /// <returns></returns>
        public static IUnityContainer InterceptContainer(IUnityContainer unityContainer, IInterceptor[] globalInterceptors, InterceptionOptions options = null)
        {
            options = options ?? new InterceptionOptions();

            var childContainer = unityContainer.CreateChildContainer();

            foreach (var registration in unityContainer.Registrations)
            {
                if (registration.MappedToType.GetInterfaces().Any(x => x == typeof(IInterceptor)))
                {
                    continue;   // no registrar los propios interceptores
                }
                if (!registration.RegisteredType.IsInterface)
                {
                    continue;
                }

                if (ReflectionHelper.MustNotIntercept(registration.MappedToType))
                {
                    continue;
                }

                var allInterceptors = GetInterceptors(unityContainer, globalInterceptors, registration, options);

                var proxied = ProxyManager.Generator.CreateInterfaceProxyWithTarget(registration.RegisteredType, unityContainer.Resolve(registration.RegisteredType, registration.Name), allInterceptors);

                childContainer.RegisterFactory(registration.RegisteredType, registration.Name, (c) => proxied);
            }
            return(childContainer);
        }
Exemple #2
0
        private static IInterceptor[] GetInterceptors(IUnityContainer unityContainer, IInterceptor[] globalInterceptors, IContainerRegistration registration, InterceptionOptions options)
        {
            var attributeInterceptors = ReflectionHelper.GetAttributeInterceptors(registration.MappedToType, unityContainer);

            var interceptors = new List <IInterceptor>();

            if (options.GlobalInterceptorsOrder == GlobalInterceptorsOrder.AfterAttributeInterceptors)
            {
                interceptors.AddRange(attributeInterceptors);
                interceptors.AddRange(globalInterceptors);
            }
            else
            {
                interceptors.AddRange(globalInterceptors);
                interceptors.AddRange(attributeInterceptors);
            }

            return(ReflectionHelper.RemoveExcludedGlobalInterceptors(registration.MappedToType, interceptors.ToArray()));
        }
Exemple #3
0
        public static IUnityContainer InterceptContainer(IUnityContainer unityContainer, Type[] globalInterceptors, InterceptionOptions options = null)
        {
            var interceptorInstances = globalInterceptors.Select(x => unityContainer.Resolve(x) as IInterceptor).ToArray();

            return(InterceptContainer(unityContainer, interceptorInstances));
        }