/// <summary>
        /// Gets 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 <ICreatesAutofacDecorator <TService>, ICustomizesAutofacDecorator <TService> > customizationFunc) where TService : class
        {
            var builder    = new AutofacGenericDecoratorBuilder <TService>(resolver);
            var customizer = (IGetsService <TService>)customizationFunc(builder);

            return(customizer.GetService());
        }
Exemplo n.º 2
0
        public void UsingInitialImplType_resolves_impl_from_resolver(IResolver resolver, ServiceImpl1 impl)
        {
            var sut = new AutofacGenericDecoratorBuilder <IServiceInterface>(resolver);

            Mock.Get(resolver)
            .Setup(x => x.Resolve(typeof(ServiceImpl1), It.IsAny <IEnumerable <Parameter> >()))
            .Returns(impl);

            var result = (AutofacGenericDecoratorCustomizer <IServiceInterface>)sut.UsingInitialImplType(typeof(ServiceImpl1));

            Assert.That(result?.Implementation, Is.SameAs(impl));
        }
Exemplo n.º 3
0
        public void UsingInitialImplType_passes_params_to_resolver(IResolver resolver,
                                                                   ServiceImpl1 impl,
                                                                   Parameter[] parameters)
        {
            var sut = new AutofacGenericDecoratorBuilder <IServiceInterface>(resolver);

            Mock.Get(resolver)
            .Setup(x => x.Resolve(typeof(ServiceImpl1), parameters))
            .Returns(impl);

            var result = (AutofacGenericDecoratorCustomizer <IServiceInterface>)sut.UsingInitialImplType(typeof(ServiceImpl1), parameters);

            Assert.That(result?.Implementation, Is.SameAs(impl));
        }
Exemplo n.º 4
0
        public void UsingInitialImplType_throws_exception_if_impl_type_does_not_derive_from_service(IResolver resolver)
        {
            var sut = new AutofacGenericDecoratorBuilder <IServiceInterface>(resolver);

            Assert.That(() => sut.UsingInitialImplType(typeof(DifferentImpl)), Throws.ArgumentException);
        }