示例#1
0
        /// <summary>
        /// Decorate all components implementing open generic service <paramref name="serviceType"/>.
        /// </summary>
        /// <param name="builder">Container builder.</param>
        /// <param name="decoratorType">The type of the decorator. Must be an open generic type, and accept a parameter
        /// of type <paramref name="serviceType"/>, which will be set to the instance being decorated.</param>
        /// <param name="serviceType">Service type being decorated. Must be an open generic type.</param>
        /// <param name="condition">A function that when provided with an <see cref="IDecoratorContext"/>
        /// instance determines if the decorator should be applied.</param>
        public static void RegisterGenericDecorator(
            this ContainerBuilder builder,
            Type decoratorType,
            Type serviceType,
            Func <IDecoratorContext, bool>?condition = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (decoratorType == null)
            {
                throw new ArgumentNullException(nameof(decoratorType));
            }

            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            var decoratorService = new DecoratorService(serviceType, condition);

            var genericRegistration = OpenGenericRegistrationExtensions
                                      .CreateGenericBuilder(decoratorType)
                                      .As(decoratorService);

            builder.RegisterServiceMiddlewareSource(new OpenGenericDecoratorMiddlewareSource(decoratorService, genericRegistration.RegistrationData, genericRegistration.ActivatorData));
        }
示例#2
0
        /// <summary>
        /// Register a resolve middleware for services providing a particular type.
        /// </summary>
        /// <param name="builder">The container builder.</param>
        /// <param name="serviceType">The service type.</param>
        /// <param name="middleware">The middleware to register.</param>
        /// <param name="insertionMode">The insertion mode of the middleware (start or end of phase).</param>
        public static void RegisterServiceMiddleware(this ContainerBuilder builder, Type serviceType, IResolveMiddleware middleware, MiddlewareInsertionMode insertionMode = MiddlewareInsertionMode.EndOfPhase)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (serviceType is null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            if (middleware is null)
            {
                throw new ArgumentNullException(nameof(middleware));
            }

            builder.RegisterServiceMiddlewareSource(new ServiceWithTypeMiddlewareSource(serviceType, middleware, insertionMode));
        }