Exemple #1
0
        public static ICollection <KeyValuePair <string, object> > FieldByValueClauseBuilder(Type type, EntityBase obj)
        {
            var propertiesByFieldNamesAttrs = ReflectionWrapper.GetPropertiesByFieldNamesAttrs(type);

            if (propertiesByFieldNamesAttrs.Count == 0)
            {
                throw new Exception(string.Format("invalid db model {0}", type));
            }
            var props =
                propertiesByFieldNamesAttrs.ExceptRelatedType().Where(y => y.Name != IdFieldEntityName).Select(x =>
            {
                var value = x.GetValue(obj, new object[] { });
                return(new KeyValuePair <string, object>(ReflectionWrapper.GetFieldNameAttribute(x).First().Value, value));
            }).Where(x => x.Value != null);

            return(props.ToList());
        }
Exemple #2
0
        private static string CreateBody(string dbName, Type type)
        {
            var tableName = CommonCommandBuilder.GetTableName(type);
            var cmdBulder = new StringBuilder();

            foreach (var prop in ReflectionWrapper.GetPropertiesByFieldNamesAttrs(type))
            {
                var attrs = ReflectionWrapper.GetFieldNameAttribute(prop);
                if (attrs.Count == 0)
                {
                    continue;
                }
                cmdBulder.Append(string.Format("[{0}].[{1}],", tableName, attrs.First().Value));
            }
            return(string.Format("SELECT {0} FROM [{1}].[dbo].[{2}] WITH(NOLOCK)",
                                 cmdBulder.ToString().Trim(','), dbName, tableName));
        }
 public ICollection <FieldNameAttribute> GetFieldNameAttribute(PropertyInfo propertyInfo)
 {
     lock (_syncObj)
     {
         if (!_cacheDictFieldNamesAttrs.ContainsKey(propertyInfo))
         {
             var result = _sourceReflection.GetFieldNameAttribute(propertyInfo);
             _cacheDictFieldNamesAttrs.Add(propertyInfo, result);
         }
         return(_cacheDictFieldNamesAttrs[propertyInfo]);
     }
 }
        public static T MappedToType <T>(this IDataReader reader, IDbConnection connection) where T : class, IEntity, new()
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var nameProps = ReflectionWrapper.GetPropertiesByFieldNamesAttrs(typeof(T));

            if (reader.FieldCount < nameProps.Count)
            {
                throw new ArgumentException("Entity could not be mapped. Entity colums is not exists.");
            }
            var result = new T();


            foreach (var prop in nameProps)
            {
                try
                {
                    var attrs = ReflectionWrapper.GetFieldNameAttribute(prop);

                    var exit = false;
                    var i    = 0;
                    if (attrs.Count != 0)
                    {
                        while (i <= reader.FieldCount - 1 && !exit)
                        {
                            if (reader.GetName(i) == attrs.First().Value)
                            {
                                exit = true;
                                prop.SetValue(result, reader[i].CastDbTypes(prop.PropertyType), null);
                            }
                            i++;
                        }
                    }
                }
                catch (Exception)
                {
                    throw new InvalidCastException(
                              string.Format("Uncorrect mapping fields to .net types in enity:{0}; field name:{1}", typeof(T), prop.Name));
                }
            }

            foreach (var relatedProp in ReflectionWrapper.GetPropertiesByRelatedEntityAttrs(typeof(T)))
            {
                try
                {
                    var relatedAttr = ReflectionWrapper.GetRelatedEntityAttribute(relatedProp);
                    foreach (var resProp in ReflectionWrapper.GetPropertiesByFieldNamesAttrs(result.GetType()))
                    {
                        if (resProp.Name == relatedAttr.First().FieldName)
                        {
                            var id         = Convert.ToInt32(resProp.GetValue(result, null));
                            var fieldName  = relatedAttr.First().RelatedFieldName;
                            var connString = connection.ConnectionString;
                            var type       = relatedProp.PropertyType.GetGenericArguments()[0];

                            var param        = Activator.CreateInstance(type);
                            var propertyInfo = type.GetProperty(fieldName);
                            propertyInfo.SetValue(param, id, null);

                            var instance = Activator.CreateInstance(relatedProp.PropertyType, new object[] { param, connString });
                            relatedProp.SetValue(result, instance, null);
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    throw new InvalidCastException(
                              string.Format("Uncorrect mapping related fields to .net types in enity:{0}; field name:{1}", typeof(T), relatedProp.Name));
                }
            }


            return(result);
        }