Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddPooledDbContextFactory <ApplicationDbContext>(options => options.UseNpgsql("Server=192.168.1.253;Database=graphqldemo;User Id=dbuser1;Password=dbuser1;Port=5432;"));

            services.AddGraphQLServer()
            .AddType <Customer>()
            .AddType <Order>()
            .AddQueryType <Query>()
            .AddMutationType <Mutation>();

            services.AddErrorFilter <CustomerNotFoundExceptionFilter>();
            services.AddErrorFilter <CreateOrderExceptionFilter>();
            services.AddErrorFilter <OrderNotFoundExceptionFilter>();



            // Auto Mapper Configurations
            var mapperConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });



            IMapper mapper = mapperConfig.CreateMapper();

            services.AddSingleton(mapper);

            var executionPlan = mapperConfig.BuildExecutionPlan(typeof(UpdateCustomerInput), typeof(Customer));

            mapperConfig.AssertConfigurationIsValid();
        }
Example #2
0
        /// <summary>
        /// Creates new Instance of <see cref="IMapper"/> and configures Mappings
        /// </summary>
        /// <returns>Instance of <see cref="IMapper"/></returns>
        public static IMapper CreateMapper()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AllowNullCollections = true;
                cfg.CreateMap <TableDto, TableDefinition>()
                .ForMember(dest => dest.Columns, opt => opt.Ignore());
                cfg.CreateMap <ColumnDto, VarChar2ColumnDefinition>()
                .ForMember(dest => dest.MaximumLength, opt => opt.MapFrom(src => src.CharLength))
                .ForMember(dest => dest.MaximumLengthUnit, opt => opt.MapFrom(src => LenghtUnits.Char));
                cfg.CreateMap <ColumnDto, NVarChar2ColumnDefinition>()
                .ForMember(dest => dest.MaximumLength, opt => opt.MapFrom(src => src.CharLength))
                .ForMember(dest => dest.MaximumLengthUnit, opt => opt.MapFrom(src => LenghtUnits.Char));
                cfg.CreateMap <ColumnDto, NumberColumnDefinition>();
                cfg.CreateMap <ColumnDto, DateColumnDefinition>();
                cfg.CreateMap <ColumnDto, RawColumnDefinition>()
                .ForMember(dest => dest.MaximumLength, opt => opt.MapFrom(src => src.Length));
                cfg.CreateMap <ColumnDto, CharColumnDefinition>()
                .ForMember(dest => dest.Length, opt => opt.MapFrom(src => src.CharLength))
                .ForMember(dest => dest.LengthUnit, opt => opt.MapFrom(src => LenghtUnits.Char));
                cfg.CreateMap <ColumnDto, NCharColumnDefinition>()
                .ForMember(dest => dest.Length, opt => opt.MapFrom(src => src.CharLength))
                .ForMember(dest => dest.LengthUnit, opt => opt.MapFrom(src => LenghtUnits.Char));
                cfg.CreateMap <ColumnDto, ClobColumnDefinition>();
                cfg.CreateMap <ColumnDto, NClobColumnDefinition>();
            });

#if DEBUG
            var plan = config.BuildExecutionPlan(typeof(TableDto), typeof(TableDefinition));
#endif
            var mapper = config.CreateMapper();

            return(mapper);
        }
Example #3
0
        public void Test()
        {
            // Complex model

            var customer = new Customer
            {
                Name = "George Costanza"
            };
            var order = new Order
            {
                Customer = customer
            };
            var bosco = new Product
            {
                Name  = "Bosco",
                Price = 4.99m
            };

            order.AddOrderLineItem(bosco, 15);

            // Configure AutoMapper

            var configuration = new MapperConfiguration(cfg => cfg.CreateMap <Order, OrderDto>());
            var executionPlan = configuration.BuildExecutionPlan(typeof(Order), typeof(OrderDto));

            // Perform mapping
            var mapper = new Mapper(configuration);

            OrderDto dto = mapper.Map <Order, OrderDto>(order);

            Assert.Equal("George Costanza", dto.CustomerName);
            Assert.Equal(74.85m, dto.Total);
        }
        public IActionResult Post4()
        {
            var config = new MapperConfiguration(cfg => cfg.CreateMap <Order, OrderDTO>());
            var plan   = config.BuildExecutionPlan(typeof(Order), typeof(OrderDTO));

            return(Ok());
        }
Example #5
0
        private static void Main(string[] args)
        {
            // arrange
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <MyProfile>();
            });
            var executionPlan = config.BuildExecutionPlan(typeof(Source), typeof(Response));
            var mapper        = config.CreateMapper();
            var source        = new Source()
            {
                Resource = new Resource()
                {
                    Id           = "ResourceId",
                    ConnectionId = "ConnectionId"
                }
            };

            // act
            var result = mapper.Map <Response>(source);

            // assert
            result.Resource.Should().BeEquivalentTo(new Response.ResourcePart
            {
                Id         = source.Resource.Id,
                Connection = new Response.ConnectionPart
                {
                    Id          = "ConnectionId",
                    Application = new Response.ApplicationPart
                    {
                        Id = "ApplicationId",
                    }
                }
            });
        }
        public void Test()
        {
            var configure = new MapperConfigurationExpression();

            configure.CreateMap <SourceClass, TargetClass>()
            .ForMember(d => d.ValueLength, o => o.MapFrom(s => s.Value.Length + 5))
            //.ForMember(d => d.ValueLength, o => o.MapFrom(s => s != null ? s.Value.Length : 0))
            .ForAllMembers(o => o.Condition((src, dest, value) => value != null));     //通过测试不需要加空值判断

            var mapperConfiguration = new MapperConfiguration(configure);
            var express             = mapperConfiguration.BuildExecutionPlan(typeof(SourceClass), typeof(TargetClass));

            var source = new SourceClass {
                Value = null
            };
            var target = new TargetClass();

            var mapper = new Mapper(mapperConfiguration);

            mapper.Map(source, target);
        }
Example #7
0
        public static void Main(string[] args)
        {
            var configuration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Wedding, WeddingDto>();
            });

            // This retrieves the execution plan Expression which can best be viewed
            // by installing the ReadableExpressions Debugger Visualizers from
            // https://marketplace.visualstudio.com/items?itemName=vs-publisher-1232914.ReadableExpressionsVisualizers,
            // mousing over the 'executionPlan' variable and clicking the magnifying glass:
            var executionPlan = configuration.BuildExecutionPlan(typeof(Wedding), typeof(WeddingDto));

            // This generates a string version of the execution
            // plan which can be viewed by mousing over the 'description'
            // variable and clicking the magnifying glass:
            var description = executionPlan.ToReadableString();

            Console.WriteLine(description);
            Console.ReadLine();
        }
Example #8
0
        public void Test()
        {
            var cfg = new MapperConfigurationExpression();

            cfg.CreateMap <Source, Destination>().IncludeMembers(s => s.InnerSource, s => s.OtherInnerSource).ReverseMap();
            cfg.CreateMap <InnerSource, Destination>(MemberList.None);
            cfg.CreateMap <OtherInnerSource, Destination>(MemberList.None);
            cfg.CreateMap <Destination, InnerSource>(MemberList.None);
            cfg.CreateMap <Destination, OtherInnerSource>(MemberList.None);

            var source = new Source
            {
                Name        = "name",
                InnerSource = new InnerSource {
                    Name = "inner name", Description = "description"
                },
                OtherInnerSource = new OtherInnerSource {
                    Name = "other name", Description = "other description", Title = "title"
                }
            };

            var configuration = new MapperConfiguration(cfg);
            var plan          = configuration.BuildExecutionPlan(typeof(Source), typeof(Destination));
            var mapper        = new Mapper(configuration);

            var destination = mapper.Map <Destination>(source);

            Assert.Equal("name", destination.Name);
            Assert.Equal("description", destination.Description);
            Assert.Equal("title", destination.Title);

            var source2 = mapper.Map <Source>(destination);

            Assert.Equal("name", source2.Name);
            Assert.Equal("description", source2.InnerSource.Description);
            Assert.Equal("title", source2.OtherInnerSource.Title);
        }
Example #9
0
        static void Main(string[] args)
        {
            var cfg = new MapperConfigurationExpression();

            cfg.CreateMap <B, A>().ForMember(d => d.astRiNg, expression => expression.Ignore());
            cfg.CreateMap <A, B>().ForMember(d => d.TestStrNumber, expression => expression.MapFrom(s => s.TesTNumber));
            cfg.CreateMap <A, A>().ForAllMembers(d => d.Condition((s, d, v) => v != null));
            cfg.CreateMap <A, C>().ForPath(d => d.BString, expression => expression.MapFrom(s => s.AString))
            .ReverseMap();

            var configuration = new MapperConfiguration(cfg);
            var executionPlan = configuration.BuildExecutionPlan(typeof(A), typeof(A));

            var mapper = new Mapper(configuration);

            #region 直接Copy

            var a = new A
            {
                AString = "asfFDSF345FDSfsdf"
            };

            var a2 = mapper.Map <A>(a);
            Assert.NotEqual(a, a2);
            Assert.Equal(a.AString, a2.AString);

            #endregion

            #region 驼峰转换

            var b = new B
            {
                aString = "daf34f"
            };

            var a3 = mapper.Map <A>(b);
            Assert.Equal(b.aString, a3.AString);
            Assert.NotEqual(b.aString, a3.astRiNg);

            a3.AString = "sdafasf";
            var b3 = mapper.Map <B>(a3);
            Assert.Equal(a3.AString, b3.aString);

            #endregion

            #region  称一样,不同类型映射

            var b4 = new B {
                TestNumber = "56"
            };
            var a4 = mapper.Map <A>(b4);
            Assert.Equal(56, a4.TesTNumber);

            #endregion

            #region 枚举和Int转换

            var b5 = new B {
                Sex = 6
            };
            var a5 = mapper.Map <A>(b5);
            Assert.Equal(TestSex.Female, a5.Sex);

            var b6 = new B {
                Sex = 8
            };
            var a6 = mapper.Map <A>(b6);
            Assert.NotEqual(TestSex.Male, a6.Sex);
            Assert.NotEqual(TestSex.Female, a6.Sex);

            #endregion

            #region 属性名转换

            var a7 = new A {
                TesTNumber = 435
            };
            var b7 = mapper.Map <B>(a7);
            Assert.Equal("435", b7.TestStrNumber);

            var a10 = new A {
                AString = "ddd"
            };
            var c = mapper.Map <C>(a10);
            Assert.Equal("ddd", c.BString);

            c.AString = null;
            var a11 = mapper.Map <A>(c);
            Assert.Equal("ddd", a11.AString);

            #endregion

            #region 空属性转换

            var a8 = new A
            {
                AString = "ggg",
                astRiNg = "ttt",
                bString = "jjj"
            };

            var a9 = new A
            {
                AString = ""
            };
            mapper.Map(a9, a8);
            Assert.Equal(string.Empty, a8.AString);
            Assert.Equal(null, a8.bString);

            #endregion

            #region Profile配置

            var cfgProfile = new MapperConfigurationExpression();
            cfgProfile.AddMaps(typeof(Program));

            var cfgMapperProfile = new MapperConfiguration(cfgProfile);
            var plan             = cfgMapperProfile.BuildExecutionPlan(typeof(ProfileClassA), typeof(ProfileClassB));

            var mapperProfile = new Mapper(cfgMapperProfile);

            var sourceProfile = new ProfileClassA("test", "privateTest")
            {
                Test = "free"
            };
            ProfileClassB destProfile = mapperProfile.Map <ProfileClassB>(sourceProfile);

            Assert.Equal("free", destProfile.Test);

            #endregion

            #region 官方Demo

            var flattening = new Flattening();
            flattening.Test();

            var includeMembers = new IncludeMembers();
            includeMembers.Test();

            var reverseMappingAndUnflattening = new ReverseMappingAndUnflattening();
            reverseMappingAndUnflattening.Test();

            var projection = new Projection();
            projection.Test();

            var configurationValidationcs = new ConfigurationValidationcs.ConfigurationValidationcs();
            configurationValidationcs.Test();

            var listsAndArrarys = new ListsAndArrays();
            listsAndArrarys.Test();

            var customTypeConverters = new CustomTypeConverters();
            customTypeConverters.Test();

            var customValueResolverscs = new CustomValueResolvers.CustomValueResolverscs();
            customValueResolverscs.Test();
            customValueResolverscs.Test2();

            var resolversAndConditions = new ResolversAndConditions();
            resolversAndConditions.Test();

            var nullSubstitution = new NullSubstitution();
            nullSubstitution.Test();

            var construction = new Construction();
            construction.Test();

            #endregion

            Console.WriteLine("Over");
            Console.ReadLine();
        }