Exemple #1
0
        //----------------------------------------------------
        public CEntiteDeMemoryDb ImporteObjet(
            CEntiteDeMemoryDb valeur,
            bool bAvecFils,
            bool bMiseAJourElementsExistants,
            HashSet <string> elementsDejaImportés)
        {
            bool bOldEnforceConstrainte = EnforceConstraints;

            try
            {
                EnforceConstraints = false;

                DataTable table = GetTable(valeur.GetType());
                if (elementsDejaImportés.Contains(valeur.ObjectKey))
                {
                    DataRow row = table.Rows.Find(valeur.Id);
                    if (row == null)
                    {
                        return(null);
                    }
                    return(Activator.CreateInstance(valeur.GetType(), new object[] { row }) as CEntiteDeMemoryDb);
                }
                DataRow rowExistante = table.Rows.Find(valeur.Id);
                if (rowExistante != null && !(bool)rowExistante[c_champIsToRead] && !bMiseAJourElementsExistants)
                {
                    return(Activator.CreateInstance(valeur.GetType(), new object[] { rowExistante }) as CEntiteDeMemoryDb);
                }

                elementsDejaImportés.Add(valeur.ObjectKey);
                foreach (PropertyInfo info in valeur.GetType().GetProperties())
                {
                    object[] attrs = info.GetCustomAttributes(typeof(MemoryParentAttribute), true);
                    if (attrs != null && attrs.Length > 0)
                    {
                        MemoryParentAttribute parentAtt = attrs[0] as MemoryParentAttribute;
                        string strChampFils             = parentAtt.NomChampFils;
                        if (strChampFils == "")
                        {
                            CEntiteDeMemoryDb entite = Activator.CreateInstance(info.PropertyType, new object[] { this }) as CEntiteDeMemoryDb;
                            strChampFils = entite.GetChampId();
                        }
                        CEntiteDeMemoryDb parent = valeur.GetParent(info.PropertyType, strChampFils);
                        if (parent != null)
                        {
                            ImporteObjet(parent, true, bMiseAJourElementsExistants, elementsDejaImportés);
                        }
                    }
                }

                if (rowExistante == null)
                {
                    table.ImportRow(valeur.Row.Row);
                }
                else
                {
                    if ((bool)valeur.Row.Row[c_champIsToRead] || bMiseAJourElementsExistants)
                    {
                        foreach (DataColumn col in valeur.Row.Row.Table.Columns)
                        {
                            if (table.Columns.Contains(col.ColumnName))
                            {
                                rowExistante[col.ColumnName] = valeur.Row.Row[col.ColumnName];
                            }
                        }
                    }
                }

                if (bAvecFils)
                {
                    foreach (DataRelation dr in valeur.Database.Relations)
                    {
                        if (dr.ParentTable.TableName == table.TableName)
                        {
                            Type tpFils = valeur.Database.GetTypeForTable(dr.ChildTable.TableName);
                            if (tpFils != null)
                            {
                                PropertyInfo prop = tpFils.GetProperty(dr.ExtendedProperties[c_ExtPropRelationNomProprieteFils].ToString());
                                if (prop != null)
                                {
                                    object[] attrs = prop.GetCustomAttributes(typeof(MemoryParentAttribute), true);
                                    if (attrs != null && attrs.Length > 0)
                                    {
                                        MemoryParentAttribute parentAtt = attrs[0] as MemoryParentAttribute;
                                        if (parentAtt.IsComposition)
                                        {
                                            Type        tpListe     = typeof(CListeEntitesDeMemoryDb <>);
                                            Type        tpListeType = tpListe.MakeGenericType(tpFils);
                                            IEnumerable lst         = Activator.CreateInstance(tpListeType, new object[] { valeur.Database, new CFiltreMemoryDb(dr.ChildColumns[0].ColumnName + "=@1", valeur.Id) }) as IEnumerable;
                                            foreach (CEntiteDeMemoryDb entite in lst)
                                            {
                                                ImporteObjet(entite, bAvecFils, bMiseAJourElementsExistants, elementsDejaImportés);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                return(Activator.CreateInstance(valeur.GetType(), new object[] { table.Rows.Find(valeur.Id) }) as CEntiteDeMemoryDb);
            }
            finally
            {
                EnforceConstraints = bOldEnforceConstrainte;
            }
        }
Exemple #2
0
        //---------------------------------------
        public bool CreateTable(Type type)
        {
            MemoryTableAttribute tableAttr = type.GetCustomAttributes(typeof(MemoryTableAttribute), true)[0] as MemoryTableAttribute;
            string strNomTable             = tableAttr.NomTable;
            string strChampId = tableAttr.ChampId;

            m_dicPrivateNomTableToType[strNomTable] = type;
            m_dicPrivateTypeToNomTable[type]        = strNomTable;

            DataTable table = Tables[strNomTable];

            if (table != null)
            {
                return(true);
            }
            table = new DataTable(strNomTable);

            //Création du champ Id
            DataColumn col = new DataColumn(strChampId, typeof(string));

            col.AllowDBNull = true;
            table.Columns.Add(col);
            table.PrimaryKey = new DataColumn[] { col };



            //Création des champs de propriété
            PropertyInfo[] proprietes = type.GetProperties();
            foreach (PropertyInfo prop in proprietes)
            {
                object[] attrs = prop.GetCustomAttributes(typeof(MemoryFieldAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    MemoryFieldAttribute field = attrs[0] as MemoryFieldAttribute;
                    string strNomChamp         = field.NomChamp;
                    if (strNomChamp == "")
                    {
                        strNomChamp = prop.Name;
                    }
                    bool bNullable = prop.PropertyType.IsClass;
                    Type tpProp    = prop.PropertyType;
                    if (!bNullable)
                    {
                        if (prop.PropertyType.IsGenericType)
                        {
                            if (prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                tpProp = prop.PropertyType.GetGenericArguments()[0];
                            }
                        }
                    }
                    if (table.Columns[strNomChamp] == null)
                    {
                        col             = new DataColumn(strNomChamp, tpProp);
                        col.AllowDBNull = true;
                        table.Columns.Add(col);
                    }
                }
            }
            Tables.Add(table);

            //Création des relations parentes
            foreach (PropertyInfo prop in proprietes)
            {
                object[] attrs = prop.GetCustomAttributes(typeof(MemoryParentAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    MemoryParentAttribute relation     = attrs[0] as MemoryParentAttribute;
                    DataTable             tableParente = GetTable(prop.PropertyType);
                    string strNomChamp = relation.NomChampFils;
                    if (strNomChamp == "")
                    {
                        strNomChamp = tableParente.PrimaryKey[0].ColumnName;
                    }
                    col             = new DataColumn(strNomChamp, typeof(string));
                    col.AllowDBNull = true;
                    table.Columns.Add(col);
                    DataRelation dtRelation = new DataRelation(GetNomRelation(table.TableName, prop.Name),
                                                               tableParente.PrimaryKey[0],
                                                               col,
                                                               true);
                    Relations.Add(dtRelation);
                    dtRelation.ExtendedProperties[c_ExtPropRelationNomProprieteFils] = prop.Name;
                    foreach (Constraint cst in table.Constraints)
                    {
                        ForeignKeyConstraint fk = cst as ForeignKeyConstraint;
                        if (fk != null && fk.ConstraintName == dtRelation.RelationName)
                        {
                            fk.DeleteRule = relation.IsComposition?Rule.Cascade:Rule.None;
                        }
                    }
                }
            }

            ///Création des tables filles
            foreach (PropertyInfo prop in proprietes)
            {
                object[] attrs = prop.GetCustomAttributes(typeof(MemoryChildAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    Type tpFils = prop.PropertyType.GetGenericArguments()[0];
                    GetTable(tpFils);
                }
            }

            if (!table.Columns.Contains(c_champIsToRead))
            {
                DataColumn colIsToRead = new DataColumn(c_champIsToRead, typeof(bool));
                colIsToRead.DefaultValue = false;
                colIsToRead.AllowDBNull  = false;
                table.Columns.Add(colIsToRead);
            }


            return(true);
        }