Пример #1
0
        public static T ParseSqlRow <T>(IDataReader row)
        {
            if (row == null)
            {
                return(default(T));
            }

            T instance = Activator.CreateInstance <T>();

            foreach (var propertyInfo in typeof(T).GetProperties())
            {
                string column = null;

                var resultColumnAttribute = propertyInfo.GetMyAttribute <SqlResultColumnAttribute>();
                if (resultColumnAttribute != null)
                {
                    column = resultColumnAttribute.FieldName;
                }

                if (column.ExIsNullOrEmpty())
                {
                    column = row.ColumnExists(propertyInfo.Name) ? propertyInfo.Name : null;
                }

                if (column == null)
                {
                    continue;
                }

                var tt    = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
                var value = AdoHelpers.SafeConvertType(row[column], tt);

                propertyInfo.SetValue(instance, value, null);
            }

            return(instance);
        }
Пример #2
0
        public static T ParseSqlRow <T>(DataRow row, bool useSmartPropertyNameMatching = false)
        {
            if (row == null)
            {
                return(default(T));
            }

            if (row.ItemArray.Length == 1 && AdoTypeMap.IsItAppropriateForSingleColumn(typeof(T)))
            {
                return((T)row[0]);
            }

            var instance = Activator.CreateInstance <T>();

            foreach (var propertyInfo in typeof(T).GetProperties())
            {
                var tt = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
                //eğer dto'daki property dizi ise devam et.
                if (tt.IsArray)
                {
                    var multipleColumnAttribute = propertyInfo.GetMyAttribute <SqlParserMultipleColumnAttribute>();
                    if (multipleColumnAttribute != null)
                    {
                        //We are searching for same prefixed property count.
                        //For example if result was like this:
                        //   Result =>
                        //              Name
                        //              Surname
                        //              Prop1
                        //              Prop2
                        //              Prop3
                        //The property prefix would then be Prop.
                        //Because you can set start number, counting would start at that property. Default = 1.
                        //In the result we would have an array of type (which is inferred from the properties)
                        //amounting 3 items.
                        var currentColumn = multipleColumnAttribute.StartNumber;
                        var columnCount   = 0;
                        while (row.Table.Columns.Contains(multipleColumnAttribute.PropertyPrefix + currentColumn))
                        {
                            currentColumn++;
                            columnCount++;
                        }

                        var arrayInstance = Activator.CreateInstance(propertyInfo.PropertyType, columnCount);
                        var array         = arrayInstance as Array;
                        if (array != null)
                        {
                            for (var i = 0; i < columnCount; i++)
                            {
                                array.SetValue(AdoHelpers.SafeConvertType(row[multipleColumnAttribute.PropertyPrefix +
                                                                              (multipleColumnAttribute.StartNumber + i)],
                                                                          propertyInfo.PropertyType.GetElementType()), i);
                            }

                            propertyInfo.SetValue(instance, array);
                        }
                    }
                }
                else
                {
                    string column = null;

                    //if attribute is used, then this is high priority.
                    var resultColumnAttribute = propertyInfo.GetMyAttribute <SqlResultColumnAttribute>();
                    if (resultColumnAttribute != null)
                    {
                        column = resultColumnAttribute.FieldName;
                    }

                    //if still we dont a have a field name then search with property name
                    if (column.ExIsNullOrEmpty())
                    {
                        column = row.Table.Columns.Contains(propertyInfo.Name)
                            ? propertyInfo.Name
                            : null;
                    }

                    //and if still dont have a field name and if smart matching is enabled
                    //then search the property name in the result set's columns collection.
                    //But this search may not always yield the best result. Because search is done
                    //in substrings and case insensitive.
                    if (column.ExIsNullOrEmpty() && useSmartPropertyNameMatching)
                    {
                        var columnNameMatch = smartMatch(row, propertyInfo.Name);
                        if (columnNameMatch != null)
                        {
                            column = columnNameMatch.ColumnName;
                        }
                    }

                    //At this point just give up. Go to next property.
                    if (column == null)
                    {
                        continue;
                    }

                    var value = AdoHelpers.SafeConvertType(row[column], tt);

                    propertyInfo.SetValue(instance, value);
                }
            }

            return(instance);
        }