コード例 #1
0
 public TypeResolutionTests()
 {
     _container = new Container();
     _container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();
     _container.Register <ISomeService>(() => new FooService(5), Lifestyle.Transient);
     _container.AddAutoMapper(typeof(Source));
 }
コード例 #2
0
    public void CanUseDefaultInjectedIMapperInSingletonService()
    {
        // Arrange
        using var container = new Container();
        container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();
        container.Register <ISomeService>(() => new FooService(5), Lifestyle.Transient);
        container.Register <ISingletonService, TestSingletonService>(Lifestyle.Singleton);
        container.AddAutoMapper(
            cfg =>
        {
            cfg.WithMapperAssemblyMarkerTypes(typeof(ServiceLifetimeTests));
            cfg.WithMapperConfigurationExpressionAction((_, expression) => expression.CreateMap <Foo, Bar>().ReverseMap());
        });
        Bar actual;

        // Act
        using (ThreadScopedLifestyle.BeginScope(container))
        {
            var service = container.GetInstance <ISingletonService>();
            actual = service.DoTheThing(new Foo {
                TheValue = 1
            });
        }

        // Assert
        actual.Should().NotBeNull();
        actual.TheValue.Should().Be(1);
    }
        public static void Main()
        {
#pragma warning disable CC0022 // Should dispose object
            using var container = new Container();
#pragma warning restore CC0022 // Should dispose object
            container.Register <ISomeService>(() => new FooService(5), Lifestyle.Transient);
            container.AddAutoMapper(
                cfg =>
            {
                cfg.WithMapperAssemblyMarkerTypes(typeof(Program));
            });
            var mapper = container.GetInstance <IMapper>();

            foreach (var typeMap in mapper.ConfigurationProvider.GetAllTypeMaps())
            {
                Console.WriteLine($"{typeMap.SourceType.Name} -> {typeMap.DestinationType.Name}");
            }

            foreach (var service in container.GetCurrentRegistrations())
            {
                Console.WriteLine($"{service.ServiceType} - {service.Registration.ImplementationType}");
            }

            var dest = mapper.Map <Dest2>(new Source2());
            Console.WriteLine(dest.ResolvedValue);

            Console.ReadKey();
        }
    public void Cannot_correctly_resolve_scoped_services_as_singleton()
    {
        using var container = new Container();
        container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();
        container.AddAutoMapper(
            cfg =>
        {
            cfg.WithMapperAssemblyMarkerTypes(typeof(Source));
            cfg.AsSingleton();
        });

        container.Register <ISomeService, MutableService>(Lifestyle.Scoped);

        using (ThreadScopedLifestyle.BeginScope(container))
        {
            var mutableService = (MutableService)container.GetInstance <ISomeService>();
            mutableService.Value = 10;

            var mapper = container.GetInstance <IMapper>();

            var dest = mapper.Map <Dest2>(new Source2 {
                ConvertedValue = 5
            });

            dest.ConvertedValue.Should().Be(15);
        }
    }
 public DependencyTests()
 {
     _container = new Container();
     _container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();
     _container.Register <ISomeService>(() => new FooService(5), Lifestyle.Transient);
     _container.AddAutoMapper(typeof(Source), typeof(Profile));
     _container.GetInstance <IConfigurationProvider>().AssertConfigurationIsValid();
 }
    public void Should_not_register_static_instance_when_configured()
    {
        using var container = new Container();
        container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();
        container.Register <ISomeService>(() => new FooService(5), Lifestyle.Transient);
        container.AddAutoMapper(typeof(Source3));

        var mapper = container.GetInstance <IMapper>();

        var source = new Source3 {
            Value = 3
        };

        var dest = mapper.Map <Dest3>(source);

        dest.Value.Should().Be(source.Value);
    }
    public void AddAutoMapperExtensionTransientWithAssemblySingleDelegateArgCollection()
    {
        using var container = new Container();
        container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();

        // act
        container.AddAutoMapper(
            cfg => cfg.AsTransient());
        var serviceDescriptor = container.GetCurrentRegistrations()
                                .FirstOrDefault(r => r.ServiceType == typeof(IMapper));

        // assert
        using (new AssertionScope())
        {
            serviceDescriptor.Should().NotBeNull();

            // ReSharper disable once PossibleNullReferenceException
            serviceDescriptor !.Lifestyle.Should().Be(Lifestyle.Transient);
        }
    }
    public void AddAutoMapperExtensionDefaultWithAssemblyCollection()
    {
        // arrange
        using var container = new Container();
        container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();

        // act
        container.AddAutoMapper(typeof(ServiceLifetimeTests).GetTypeInfo().Assembly);
        var serviceDescriptor = container.GetCurrentRegistrations()
                                .FirstOrDefault(r => r.ServiceType == typeof(IMapper));

        // assert
        using (new AssertionScope())
        {
            serviceDescriptor.Should().NotBeNull();

            // ReSharper disable once PossibleNullReferenceException
            serviceDescriptor !.Lifestyle.Should().Be(Lifestyle.Singleton);
        }
    }
コード例 #9
0
    public void ShouldThrowExceptionWhenNullMapperConfigurationPassed()
    {
        // Arrange
        using var container = new Container();
        const Action <Container, IMapperConfigurationExpression>?mapperConfigurationExpressionAction = null;
        Action action = () =>
        {
            // ReSharper disable once AccessToDisposedClosure
            container.AddAutoMapper(
                cfg =>
            {
                cfg.WithMapperAssemblyMarkerTypes(typeof(ServiceLifetimeTests));
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
                cfg.WithMapperConfigurationExpressionAction(mapperConfigurationExpressionAction);
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
            });
        };

        // Act & Assert
        action.Should().Throw <ArgumentNullException>();
    }
 public DuplicateAssemblyResolutionTests()
 {
     _container = new Container();
     _container.AddAutoMapper(typeof(IMapper), typeof(IMapper));
 }
コード例 #11
0
 public void ShouldResolveConfigurationUsingAssemblyParams()
 {
     _container.AddAutoMapper(typeof(Source).GetTypeInfo().Assembly);
     _container.GetInstance <IConfigurationProvider>().Should().NotBeNull();
 }