Exemple #1
0
        public void ShouldMapWithDifferentDependenciesResolvedByName()
        {
            var foo = new FooX()
            {
                Property1 = 10, Property2 = 100
            };
            var resolver = new ResolverMock();

            resolver.Add(() => new Dependency1()
            {
                GlobalProperty1 = 1
            }, "dependency1");
            resolver.Add(() => new Dependency1()
            {
                GlobalProperty1 = 2
            }, "dependency2");

            var mapper = CreateMapper(resolver, builder =>
            {
                builder.CreateMap <Foo, Bar>()
                .WithDependencies <Dependency1>("dependency1")
                .MapProperty((x, dep) => dep.Item1.GlobalProperty1 + x.Property1, x => x.Property3);

                builder.CreateMap <FooX, BarX>()
                .WithDependencies <Dependency1>("dependency2")
                .MapProperty((x, dep) => dep.Item1.GlobalProperty1 + x.Property2, x => x.Property4);
            });

            var bar = (BarX)mapper.Map <Bar>(foo);

            Assert.AreEqual(11, bar.Property3);
            Assert.AreEqual(102, bar.Property4);
        }
Exemple #2
0
        public void ShouldUseSameDependencyDuringMappingOperationOfComposedChildren()
        {
            var fooC = new FooC()
            {
                Foos = new Foo[] { new Foo(), new Foo(), new Foo() }
            };
            var resolver = new ResolverMock();

            resolver.Add(() => new Dependency1());

            var mapper = CreateMapper(resolver, builder =>
            {
                builder.CreateMap <Foo, Bar>()
                .WithDependencies <Dependency1>()
                .MapProperty((x, d) => d.Item1.RandomProperty, x => x.Property3);

                builder.CreateMap <FooC, BarC>()
                .MapCollection(x => x.Foos, x => x.BarArray);
            });

            var barC         = mapper.Map <BarC>(fooC);
            var randomNumber = barC.BarArray.Select(x => x.Property3).Distinct().ToArray();

            Assert.AreEqual(1, randomNumber.Length, "Expected all Bars to have the same Property3 because they should have used the same dependency");
            Assert.AreNotEqual(0, randomNumber[0], "Should be a random int - not equal to zero");
        }
Exemple #3
0
        public void ShouldUseSameDependencyDuringMappingOperation()
        {
            //While Mapping, if requesting the same dependency on multiple maps, the same dependency should be
            //used (instead of going back to the IOC container).
            //The idea is that mapping an object is a single function (and therefore the dependencies should be resolved only once).

            var foo      = new FooX();
            var resolver = new ResolverMock();

            resolver.Add(() => new Dependency1());

            var mapper = CreateMapper(resolver, builder =>
            {
                builder.CreateMap <FooX, BarX>()
                .WithDependencies <Dependency1>()
                .MapProperty((x, dep) => dep.Item1.RandomProperty, x => x.Property4)
                .MapProperty((x, dep) => dep.Item1.RandomProperty, x => x.Property3);
            });

            var bar1 = mapper.Map <BarX>(foo);
            var bar2 = mapper.Map <BarX>(foo);

            Assert.AreEqual(bar1.Property3, bar1.Property4, "Those properties must be the same!");
            Assert.AreEqual(bar2.Property3, bar2.Property4, "Those properties must be the same!");
            Assert.AreNotEqual(bar1.Property3, bar2.Property3, "Those properties cannot be the same!");
            Assert.AreNotEqual(bar1.Property4, bar2.Property4, "Those properties cannot be the same!");
        }
Exemple #4
0
        public void ShouldUseMappingFunctionWithDependencies()
        {
            var foo = new Foo()
            {
                Property1 = 18
            };
            var resolver = new ResolverMock();

            resolver.Add(() => new Dependency1()
            {
                GlobalProperty1 = 10
            });

            var mapper = CreateMapper(resolver, builder =>
            {
                builder.CreateMap <Foo, Bar>()
                .WithDependencies <Dependency1>()
                .MapFunction((src, tgt, dependencies) => tgt.Property3 = src.Property1 + dependencies.Item1.GlobalProperty1);
            });

            var bar = mapper.Map <Bar>(foo);

            Assert.AreEqual(28, bar.Property3);
        }
Exemple #5
0
        public void ShouldMapWithResolverByName()
        {
            var foo = new Foo()
            {
                Property1 = 891
            };
            var resolver = new ResolverMock();

            resolver.Add(() => new Dependency1()
            {
                GlobalProperty1 = 5
            }, "dependency1");

            var mapper = CreateMapper(resolver, builder =>
            {
                builder.CreateMap <Foo, Bar>()
                .WithDependencies <Dependency1>("dependency1")
                .MapProperty((x, dep) => dep.Item1.GlobalProperty1 + x.Property1, x => x.Property3);
            });

            var bar = mapper.Map <Bar>(foo);

            Assert.AreEqual(896, bar.Property3);
        }