/// <summary>
        /// Builds and returns an instance of a service, using the decorator pattern.
        /// </summary>
        /// <returns>The service instance.</returns>
        /// <param name="customizationFunc">A customization function, to build the 'shape' of the decorated service.</param>
        /// <typeparam name="TService">The service type, typically an interface.</typeparam>
        public TService GetDecoratedService <TService>(Func <ICreatesDecorator <TService>, ICustomizesDecorator <TService> > customizationFunc) where TService : class
        {
            var builder        = new AutofacGenericDecoratorBuilder <TService>(resolver);
            var builderAdapter = new GenericDecoratorBuilderAdapter <TService>(builder);
            var customizer     = (IGetsService <TService>)customizationFunc(builderAdapter);

            return(customizer.GetService());
        }
예제 #2
0
        public void UsingInitialImpl_maps_and_passes_parameters([Frozen] ICreatesAutofacDecorator <IServiceInterface> wrapped,
                                                                GenericDecoratorBuilderAdapter <IServiceInterface> sut,
                                                                ICustomizesAutofacDecorator <IServiceInterface> customiser)
        {
            Parameter[] parameters = null;
            Mock.Get(wrapped)
            .Setup(x => x.UsingInitialImpl <ServiceImpl1>(It.IsAny <Parameter[]>()))
            .Callback((Parameter[] @params) => parameters = @params)
            .Returns(customiser);
            sut.UsingInitialImpl <ServiceImpl1>(TypedParam.From("Foo bar"), TypedParam.From(5));

            IEqualityComparer <Parameter> comparer = new TypedParameterComparer();
            var expected = new[]
            {
                TypedParameter.From("Foo bar"),
                TypedParameter.From(5),
            };

            Assert.That(parameters, Is.EqualTo(expected).Using(comparer));
        }
예제 #3
0
 public void UsingInitialImplType_executes_appropriate_functionality_from_wrapped_service([Frozen] ICreatesAutofacDecorator <IServiceInterface> wrapped,
                                                                                          GenericDecoratorBuilderAdapter <IServiceInterface> sut,
                                                                                          Type aType,
                                                                                          ICustomizesAutofacDecorator <IServiceInterface> customiser)
 {
     Mock.Get(wrapped)
     .Setup(x => x.UsingInitialImplType(It.IsAny <Type>(), It.IsAny <Parameter[]>()))
     .Returns(customiser);
     sut.UsingInitialImplType(aType);
     Mock.Get(wrapped).Verify(x => x.UsingInitialImplType(aType), Times.Once);
 }