Example #1
0
        /// <summary>
        /// Map the record values to the entity.
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="entityMappingDefinition"></param>
        /// <param name="columnNames"></param>
        /// <returns></returns>
        public object MapToEntity(SqlDataReader reader,
                                  EntityMappingDefinition entityMappingDefinition,
                                  List <string> columnNames)
        {
            if (columnNames == null)
            {
                // TODO : Throw the appropriate exception.
            }

            var instance = Activator.CreateInstance(entityMappingDefinition.EntityType);

            for (var i = 0; i < columnNames.Count; i++)
            {
                var columnName       = columnNames[i];
                var columnDefinition = entityMappingDefinition.ColumnDefinitions.FirstOrDefault(c => c.ColumnName == columnName);
                if (columnDefinition == null)
                {
                    continue;
                }

                FillInTheInstance(instance, columnName, columnDefinition, reader, i);
            }

            return(instance);
        }
Example #2
0
        /// <summary>
        /// Execute the sql script and returns the list
        /// </summary>
        /// <param name="sqlScript"></param>
        /// <param name="entityMappingDefinition"></param>
        /// <returns></returns>
        public object ExecuteReaderAndReturnList(string sqlScript, EntityMappingDefinition entityMappingDefinition)
        {
            _connectionManager.Open();

            var   listType            = typeof(List <>);
            var   constructedListType = listType.MakeGenericType(entityMappingDefinition.EntityType);
            IList result = (IList)Activator.CreateInstance(constructedListType);

            var command = new SqlCommand();

            command.CommandText = sqlScript;
            command.CommandType = CommandType.Text;
            command.Connection  = _connectionManager.Connection;

            var reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                var columnNames = GetColumnNames(reader);
                while (reader.Read())
                {
                    var instance = _recordReader.MapToEntity(reader, entityMappingDefinition, columnNames);

                    result.Add(instance);
                }
            }

            reader.Close();
            _connectionManager.Close();

            return(result);
        }
        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);
        }
Example #4
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);
        }