public void ClearCache() { lock (LockObject) { EntityFieldMetadata.Clear(); EntityMetadata.Clear(); RelationshipMetadata.Clear(); EntityRelationships.Clear(); } }
//------------------------------------------------------------------------- public void RemoveEntity(Entity entity) { Type type = entity.GetType(); if (Entities.ContainsKey(type)) { Entities[type].Remove(entity.Id); } EntityRelationships.RemoveEntity(entity); }
//------------------------------------------------------------------------- public void GetAsXml(XmlElement xml) { //-- Add entities & their dependencies. XmlElement entitiesXml = xml.OwnerDocument.CreateElement("Entities"); xml.AppendChild(entitiesXml); XmlElement dependenciesXml = xml.OwnerDocument.CreateElement("EntityDependencies"); xml.AppendChild(dependenciesXml); // Iterate through each entity type. foreach (Dictionary <int, Entity> pair in Entities.Values) { // Iterate through each entity for the current entity type. foreach (Entity entity in pair.Values) { // Entity xml. XmlElement entityXml = xml.OwnerDocument.CreateElement("Entity"); entitiesXml.AppendChild(entityXml); entity.GetAsXml(entityXml); //-- Dependency xml. // Dependant entity. XmlElement entityDependencyXml = xml.OwnerDocument.CreateElement("Entity"); dependenciesXml.AppendChild(entityDependencyXml); XmlAttribute attrib = xml.OwnerDocument.CreateAttribute("id"); entityDependencyXml.Attributes.Append(attrib); attrib.Value = entity.Id.ToString(); // Entity's dependencies. ReadOnlyCollection <Entity> dependencies; EntityRelationships.GetDependencies(entity, out dependencies); foreach (Entity dependency in dependencies) { XmlElement dependencyXml = xml.OwnerDocument.CreateElement("Dependency"); entityDependencyXml.AppendChild(dependencyXml); attrib = xml.OwnerDocument.CreateAttribute("id"); dependencyXml.Attributes.Append(attrib); attrib.Value = dependency.Id.ToString(); } } } }
//------------------------------------------------------------------------- public void InitialiseFromXml(XmlElement xml) { Reset(); // Load the Entities. if (xml.SelectSingleNode("Entities") == null) { throw new ArgumentException("'Entities' element not found."); } foreach (XmlElement entityXml in xml["Entities"]) { AddEntity(Entity.InstantiateFromXml(EntityFactory, entityXml)); } // Load entity dependencies. if (xml.SelectSingleNode("EntityDependencies") == null) { throw new ArgumentException("'EntityDependencies' element not found."); } foreach (XmlElement entityXml in xml["EntityDependencies"]) { // Dependant entity. if (entityXml.HasAttribute("id") == false) { throw new ArgumentException("Dependant entity 'id' not found."); } int dependantId = int.Parse(entityXml.Attributes["id"].Value); Entity dependant = GetEntity(dependantId); if (dependant == null) { throw new ArgumentException( string.Format("Dependant Entity '{0}' not found.", dependantId)); } // Dependency. foreach (XmlElement dependencyXml in entityXml) { if (dependencyXml.HasAttribute("id") == false) { throw new ArgumentException( string.Format("Dependant Entity '{0}' 'Dependency' element missing 'id' attribute.", dependantId)); } int dependencyId = int.Parse(dependencyXml.Attributes["id"].Value); Entity dependency = GetEntity(dependencyId); if (dependency == null) { throw new ArgumentException( string.Format("Dependant Entity '{0}' dependency '{1}' not found.", dependantId, dependencyId)); } // Create the dependency. EntityRelationships.AddDependency(dependant, dependency); } } // Update the factory's next id. int highestId = 0; foreach (Dictionary <int, Entity> entityByType in Entities.Values) { foreach (Entity entity in entityByType.Values) { if (entity.Id > highestId) { highestId = entity.Id; } } } EntityFactory.SetNextId(highestId + 1); }