public static void Run()
        {
            var def = new EntityMappingDefinition <CustomerResult, Customer>("Customer");

            // simple field mapping: same type on either end
            def.From(z => z.Desc).To(z => z.Name);
            def.From(z => z.Age).To(z => z.Age);

            // To() end supports multiple layer of member expression such as z.Preference.Hobby
            def.From(z => z.Leisure).To(z => z.Preference.Hobby);

            // complex field mapping: intermediate transformation via Then(), in which transformation func GetLoyalty is called.
            def.From(z => z.YearsWithUs).Then(GetLoyalty).To(z => z.Loyalty);

            // define string representation through predefined Stringify() func. The end result will be a dictionary.
            def.From(z => z.Account).To(z => z.Account).Stringify(z =>
                                                                  $"AccountId:{z.AccountId}, AccountNumber:{z.AccountNumber}");

            var customerResult = new CustomerResult
            {
                Desc        = "Joe",
                Leisure     = "GO",
                Age         = 73,
                YearsWithUs = 8,
                Account     = new Account
                {
                    AccountId     = 123,
                    AccountNumber = "978654321"
                },
            };

            // covert from customerResult to customer
            var customer = def.Convert(customerResult);

            var stringResult = def.Stringify(customerResult);

            // show how we copy the same type of object
            var newCustomer = new Customer
            {
                Loyalty = Loyalty.Level1,
                Account = new Account
                {
                    AccountId     = 125,
                    AccountNumber = "123456789"
                },
                Mobile = "0455467568"
            };

            // only copy defined properties of EntityMappingDefinition from converted customer to new customer object,
            // other properties in existing object will be kept.
            def.Copy(customer, newCustomer);
        }
Beispiel #2
0
        public static void Run()
        {
            var child = new Child
            {
                FirstName = "Adam",
                LastName  = "Hwang",
                Age       = 12,
                Readings  = new Dictionary <string, Book>
                {
                    { "StarWars", new Book {
                          BookName = "StarWars", Isbn = "1234"
                      } },
                    { "Gone With the Wind", new Book {
                          BookName = "Gone With the Wind", Isbn = "5678"
                      } }
                }
            };

            var employee = new Employee
            {
                FirstName = "Bill",
                LastName  = "Hwang",
                Age       = 41,
                Children  = new List <Child>
                {
                    child
                },
                Sex     = SexEnum.Male,
                Account = new Account
                {
                    AccountNumber = "1234"
                }
            };

            var def = new EntityMappingDefinition <Employee, Customer>("");

            def.From(z => z.Account.AccountNumber).To(z => z.Account.AccountNumber);

            var customer = def.Convert(employee);
        }