コード例 #1
0
        public static void Main(string[] args)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <AuthorModel, AuthorDTO>()
                .ForMember(dest => dest.Age, opts => opts.MapFrom(src => CaclulateYears(src.BirthDay)));
            });
            IMapper iMapper = config.CreateMapper();
            var     source  = new AuthorModel();

            source.Id        = 1;
            source.FirstName = "Joydip";
            source.LastName  = "Kanjulal";
            source.Address   = "India";
            source.BirthDay  = DateTime.Parse("30/12/1993");


            var destination = iMapper.Map <AuthorModel, AuthorDTO>(source);

            Console.Write("Author Name: " + destination.FirstName + " " + destination.LastName);
            Console.WriteLine(" was born " + destination.BirthDay.ToString() + ", so the author is "
                              + destination.Age);
            Console.Read();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            ApplicationDBContext db = new ApplicationDBContext();

            var lv_config = new MapperConfiguration(cfg => {
                cfg.CreateMap <AuthorModel, AuthorDTO>();
            });


            IMapper iMapper = lv_config.CreateMapper();
            var     source  = new AuthorModel();

            source.Id        = 1;
            source.FirstName = "Eduardo";
            source.LastName  = "Linares";
            source.Address   = "Santiago";
            var destination = iMapper.Map <AuthorModel, AuthorDTO>(source);

            Console.WriteLine("Author Name: " + destination.FirstName + " " + destination.LastName);

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <JObject, CorporateRatesInfo>();
                cfg.AddProfile <CorporateRatesProfile>();
            });

            //config.AssertConfigurationIsValid();
            var mapper = config.CreateMapper();

            var jsonText = @"
                            {
                                ""conum"" : 1001,
                                ""name"" : ""CLN Industries Corporation"",
                                ""agencyName"" : ""Murphy, Holmes & Associates, LLC"",
                                ""sAA"" : [{
                                        ""code"" : 247,
                                        ""description"" : ""Mechanic\u0027s lien - Bond to Discharge - Fixed penalty - where principal has posted Performance and Pa""
                                    }, {
                                        ""code"" : 277,
                                        ""description"" : ""Mechanic\u0027s lien - Bond to Discharge - Open Penalty - where principal has posted Performance and Paym""
                                    }, {
                                        ""code"" : 505,
                                        ""description"" : ""Indemnity Bonds - Contractor\u0027s Indemnity Against Damages where there is a performance bond and addit""
                                    }
                                ]
                            }
                        ";

            var jsonoObj = JObject.Parse(jsonText);

            CorporateRatesInfo dto = mapper.Map <CorporateRatesInfo>(jsonoObj);

            try
            {
                db.CorporateRatesInfo.Add(dto);
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }

            Console.ReadKey();
        }