Exemplo n.º 1
0
        public T MapWithOutRelationship <T>(SCOSqlConnection cnn, DataRow dr) where T : new()
        {
            T   obj        = new T();
            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                var attributes = property.GetCustomAttributes(false);

                var columnMapping = FirstOrDefault(attributes, typeof(ColumnAttribute));
                if (columnMapping != null)
                {
                    var mapsTo = columnMapping as ColumnAttribute;
                    property.SetValue(obj, dr[mapsTo.Name]);
                }
            }

            return(obj);
        }
Exemplo n.º 2
0
        public List <T> ExecuteQueryWithOutRelationship <T>() where T : new()
        {
            _cmd.CommandText = _query;

            DataTable      dt      = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(_cmd);

            adapter.Fill(dt);

            List <T>         res    = new List <T>();
            SCOSqlConnection cnn    = new SCOSqlConnection(_connectionString);
            SqlMapper        mapper = new SqlMapper();

            foreach (DataRow dr in dt.Rows)
            {
                res.Add(mapper.MapWithOutRelationship <T>(cnn, dr));
            }

            return(res);
        }
Exemplo n.º 3
0
 protected abstract void MapToOne <T>(SCOSqlConnection cnn, DataRow dr, T obj) where T : new();
        protected override void MapToOne <T>(SCOSqlConnection cnn, DataRow dr, T obj)
        {
            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                Type type       = property.PropertyType;
                var  attributes = property.GetCustomAttributes(false);

                var arr1 = GetAll(attributes, typeof(OneToOneAttribute));
                var arr2 = GetAll(attributes, typeof(ManyToOneAttribute));

                var toOneAttributes = new object[arr1.Length + arr2.Length];
                if (toOneAttributes.Length > 0)
                {
                    arr1.CopyTo(toOneAttributes, 0);
                    arr2.CopyTo(toOneAttributes, arr1.Length);
                }

                if (toOneAttributes != null && toOneAttributes.Length != 0)
                {
                    foreach (var attribute in toOneAttributes)
                    {
                        SqlMapper mapper         = new SqlMapper();
                        string    tableName      = string.Empty;
                        string    whereStr       = string.Empty;
                        string    relationshipID = string.Empty;

                        if (attribute.GetType() == typeof(OneToOneAttribute))
                        {
                            relationshipID = (attribute as OneToOneAttribute).RelationshipID;
                            tableName      = (attribute as OneToOneAttribute).TableName;
                        }
                        else
                        {
                            relationshipID = (attribute as ManyToOneAttribute).RelationshipID;
                            tableName      = (attribute as ManyToOneAttribute).TableName;
                        }

                        MethodInfo getForeignKeyAttributeMethod = mapper.GetType().GetMethod("GetForeignKeys")
                                                                  .MakeGenericMethod(typeof(T));
                        List <ForeignKeyAttribute> foreignKeyAttributes = getForeignKeyAttributeMethod.Invoke(mapper, new object[] { relationshipID }) as List <ForeignKeyAttribute>;

                        MethodInfo getColumnAttributeMethod = mapper.GetType().GetMethod("GetColumns")
                                                              .MakeGenericMethod(new Type[] { type });

                        List <ColumnAttribute> columnAttributes = getColumnAttributeMethod.Invoke(mapper, null) as List <ColumnAttribute>;

                        if (foreignKeyAttributes != null)
                        {
                            foreach (ForeignKeyAttribute foreignKeyAttribute in foreignKeyAttributes)
                            {
                                ColumnAttribute column = FindColumn(foreignKeyAttribute.References, columnAttributes);
                                if (column != null)
                                {
                                    string format = "{0} = {1}, ";
                                    if (column.Type == DataType.NCHAR || column.Type == DataType.NVARCHAR)
                                    {
                                        format = "{0} = N'{1}', ";
                                    }
                                    else if (column.Type == DataType.CHAR || column.Type == DataType.VARCHAR)
                                    {
                                        format = "{0} = '{1}', ";
                                    }

                                    whereStr += string.Format(format, foreignKeyAttribute.References, dr[foreignKeyAttribute.Name]);
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(whereStr))
                        {
                            whereStr = whereStr.Substring(0, whereStr.Length - 2);
                            string query = string.Format("SELECT * FROM {0} WHERE {1}", tableName, whereStr);

                            cnn.Open();
                            MethodInfo method = cnn.GetType().GetMethod("ExecuteQueryWithOutRelationship")
                                                .MakeGenericMethod(new Type[] { type });
                            var ienumerable = (IEnumerable)method.Invoke(cnn, new object[] { query });
                            cnn.Close();

                            MethodInfo method2      = mapper.GetType().GetMethod("GetFirst");
                            var        firstElement = method2.Invoke(mapper, new object[] { ienumerable });

                            property.SetValue(obj, firstElement);
                        }
                    }
                }
            }
        }
        protected override void MapOneToMany <T>(SCOSqlConnection cnn, DataRow dr, T obj)
        {
            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                var attributes = property.GetCustomAttributes(false);

                var oneToManyAttributes = GetAll(attributes, typeof(OneToManyAttribute));
                if (oneToManyAttributes != null && oneToManyAttributes.Length != 0)
                {
                    foreach (OneToManyAttribute oneToManyAttribute in oneToManyAttributes)
                    {
                        Type type = property.PropertyType;
                        if (type.IsGenericType)
                        {
                            Type      itemType = type.GetGenericArguments()[0];
                            SqlMapper mapper   = new SqlMapper();

                            MethodInfo getTableNameMethod = mapper.GetType().GetMethod("GetTableName")
                                                            .MakeGenericMethod(new Type[] { itemType });
                            string tableName = getTableNameMethod.Invoke(mapper, null) as string;

                            MethodInfo getForeignKeyAttributeMethod = mapper.GetType().GetMethod("GetForeignKeys")
                                                                      .MakeGenericMethod(new Type[] { itemType });
                            List <ForeignKeyAttribute> foreignKeyAttributes = getForeignKeyAttributeMethod.Invoke(mapper, new object[] { oneToManyAttribute.RelationshipID }) as List <ForeignKeyAttribute>;

                            MethodInfo getColumnAttributeMethod = mapper.GetType().GetMethod("GetColumns")
                                                                  .MakeGenericMethod(typeof(T));
                            List <ColumnAttribute> columnAttributes = getColumnAttributeMethod.Invoke(mapper, null) as List <ColumnAttribute>;

                            string whereStr = string.Empty;
                            if (foreignKeyAttributes != null)
                            {
                                foreach (ForeignKeyAttribute foreignKeyAttribute in foreignKeyAttributes)
                                {
                                    ColumnAttribute column = FindColumn(foreignKeyAttribute.References, columnAttributes);
                                    if (column != null)
                                    {
                                        string format = "{0} = {1}, ";
                                        if (column.Type == DataType.NCHAR || column.Type == DataType.NVARCHAR)
                                        {
                                            format = "{0} = N'{1}', ";
                                        }
                                        else if (column.Type == DataType.CHAR || column.Type == DataType.VARCHAR)
                                        {
                                            format = "{0} = '{1}', ";
                                        }

                                        whereStr += string.Format(format, foreignKeyAttribute.Name, dr[foreignKeyAttribute.References]);
                                    }
                                }
                            }
                            if (!string.IsNullOrEmpty(whereStr))
                            {
                                whereStr = whereStr.Substring(0, whereStr.Length - 2);
                                string query = string.Format("SELECT * FROM {0} WHERE {1}", tableName, whereStr);

                                cnn.Open();
                                MethodInfo method = cnn.GetType().GetMethod("ExecuteQueryWithOutRelationship")
                                                    .MakeGenericMethod(new Type[] { itemType });
                                property.SetValue(obj, method.Invoke(cnn, new object[] { query }));
                                cnn.Close();
                            }
                        }
                    }
                }
            }
        }