Beispiel #1
0
        /// <summary>
        /// Decorate all components implementing service <paramref name="serviceType"/>
        /// with decorator service <paramref name="decoratorType"/>.
        /// </summary>
        /// <param name="builder">Container builder.</param>
        /// <param name="decoratorType">Service type of the decorator. Must 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.</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 RegisterDecorator(
            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 rb = RegistrationBuilder.ForType(decoratorType).As(decoratorService);

            var decoratorRegistration = rb.CreateRegistration();

            var middleware = new DecoratorMiddleware(decoratorService, decoratorRegistration);

            builder.RegisterServiceMiddleware(serviceType, middleware, MiddlewareInsertionMode.StartOfPhase);

            // Add the decorator to the registry so the pipeline gets built.
            builder.RegisterCallback(crb => crb.Register(decoratorRegistration));
        }
Beispiel #2
0
        /// <summary>
        /// Decorate all components implementing service <typeparamref name="TService"/>
        /// using the provided <paramref name="decorator"/> function.
        /// </summary>
        /// <typeparam name="TService">Service type being decorated.</typeparam>
        /// <param name="builder">Container builder.</param>
        /// <param name="decorator">Function decorating a component instance that provides
        /// <typeparamref name="TService"/>, given the context, parameters and service to decorate.</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 RegisterDecorator <TService>(
            this ContainerBuilder builder,
            Func <IComponentContext, IEnumerable <Parameter>, TService, TService> decorator,
            Func <IDecoratorContext, bool>?condition = null)
            where TService : class
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            var service = new DecoratorService(typeof(TService), condition);

            var rb = RegistrationBuilder.ForDelegate((c, p) =>
            {
                TService?instance = (TService?)p
                                    .OfType <TypedParameter>()
                                    .FirstOrDefault(tp => tp.Type == typeof(TService))
                                    ?.Value;

                if (instance == null)
                {
                    throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.DecoratorRequiresInstanceParameter, typeof(TService).Name));
                }

                return(decorator(c, p, instance));
            }).As(service);

            var decoratorRegistration = rb.CreateRegistration();

            var middleware = new DecoratorMiddleware(service, decoratorRegistration);

            builder.RegisterServiceMiddleware <TService>(middleware, MiddlewareInsertionMode.StartOfPhase);

            // Add the decorator to the registry so the pipeline gets built.
            builder.RegisterCallback(crb => crb.Register(decoratorRegistration));
        }
        /// <summary>
        /// Decorate all components implementing service <typeparamref name="TService"/>
        /// with decorator service <typeparamref name="TDecorator"/>.
        /// </summary>
        /// <typeparam name="TDecorator">Service type of the decorator. Must accept a parameter
        /// of type <typeparamref name="TService"/>, which will be set to the instance being decorated.</typeparam>
        /// <typeparam name="TService">Service type being decorated.</typeparam>
        /// <param name="builder">Container builder.</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 RegisterDecorator <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TDecorator, TService>(this ContainerBuilder builder, Func <IDecoratorContext, bool>?condition = null)
            where TDecorator : notnull, TService
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var decoratorService = new DecoratorService(typeof(TService), condition);

            var rb = RegistrationBuilder.ForType <TDecorator>().As(decoratorService);

            var decoratorRegistration = rb.CreateRegistration();

            var middleware = new DecoratorMiddleware(decoratorService, decoratorRegistration);

            builder.RegisterServiceMiddleware <TService>(middleware, MiddlewareInsertionMode.StartOfPhase);

            // Add the decorator to the registry so the pipeline gets built.
            builder.RegisterCallback(crb => crb.Register(decoratorRegistration));
        }
Beispiel #4
0
 /// <summary>
 /// Register a resolve middleware for services providing a particular type.
 /// </summary>
 /// <typeparam name="TService">The service type.</typeparam>
 /// <param name="builder">The container builder.</param>
 /// <param name="descriptor">A description for the middleware; this will show up in any resolve tracing.</param>
 /// <param name="phase">The phase of the pipeline the middleware should run at.</param>
 /// <param name="callback">
 /// A callback invoked to run your middleware.
 /// This callback takes a <see cref="ResolveRequestContext"/>, containing the context for the resolve request, plus
 /// a callback to invoke to continue the pipeline.
 /// </param>
 public static void RegisterServiceMiddleware <TService>(this ContainerBuilder builder, string descriptor, PipelinePhase phase, Action <ResolveRequestContext, Action <ResolveRequestContext> > callback)
 {
     builder.RegisterServiceMiddleware <TService>(descriptor, phase, MiddlewareInsertionMode.EndOfPhase, callback);
 }
Beispiel #5
0
 /// <summary>
 /// Register a resolve middleware for services providing a particular type.
 /// </summary>
 /// <typeparam name="TService">The service type.</typeparam>
 /// <param name="builder">The container builder.</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 <TService>(this ContainerBuilder builder, IResolveMiddleware middleware, MiddlewareInsertionMode insertionMode = MiddlewareInsertionMode.EndOfPhase)
 {
     builder.RegisterServiceMiddleware(typeof(TService), middleware, insertionMode);
 }
Beispiel #6
0
 /// <summary>
 /// Register a resolve middleware for services providing a particular type.
 /// </summary>
 /// <typeparam name="TService">The service type.</typeparam>
 /// <param name="builder">The container builder.</param>
 /// <param name="descriptor">A description for the middleware; this will show up in any resolve tracing.</param>
 /// <param name="phase">The phase of the pipeline the middleware should run at.</param>
 /// <param name="callback">
 /// A callback invoked to run your middleware.
 /// This callback takes a <see cref="ResolveRequestContext"/>, containing the context for the resolve request, plus
 /// a callback to invoke to continue the pipeline.
 /// </param>
 /// <param name="insertionMode">The insertion mode of the middleware (start or end of phase).</param>
 public static void RegisterServiceMiddleware <TService>(this ContainerBuilder builder, string descriptor, PipelinePhase phase, MiddlewareInsertionMode insertionMode, Action <ResolveRequestContext, Action <ResolveRequestContext> > callback)
 {
     builder.RegisterServiceMiddleware(typeof(TService), new DelegateMiddleware(descriptor, phase, callback), insertionMode);
 }