コード例 #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;
            }
        }