public SqlStorage.DatabaseObject Convert(ResolutionContext context) { var source = context.SourceValue as DatabaseObject; if (source == null) { throw new ArgumentException(nameof(context)); } if (!source.Id.HasValue) { throw new ArgumentNullException(nameof(source.Id)); } var databaseObject = new SqlStorage.DatabaseObject { ObjectType = source.ObjectType, FullyQualifiedName = source.FullyQualifiedName, DatabaseObjectId = source.Id.Value }; foreach (var objectProperty in source.Properties) { var targetProperty = DatabaseObjectConverter.Convert(objectProperty); if (targetProperty != null) { databaseObject.Properties.Add(targetProperty); } } return(databaseObject); }
// SaveState wipes out graph but keeps DatabaseObjects because: // TODO: We could try updating modified vertices only. public void SaveState(DatabaseObjectsGraph dg) { DatabaseObject[] symbolTable = dg.CreateMemento().State; int[][] jaggedArray = dg.Digraph.CreateMemento().State; // Save using (var ctx = new DependencyGraphContext(_connectionString)) { ctx.Database.ExecuteSqlCommand("TRUNCATE TABLE DatabaseObjectsGraph;"); foreach (var databaseObject in symbolTable) { switch (databaseObject.ObjectState) { case ObjectState.VertexAdded: var sqlStorageDatabaseObject = _engine.Map <SqlStorage.DatabaseObject>(databaseObject); ctx.DatabaseObjects.Add(sqlStorageDatabaseObject); break; case ObjectState.PropertiesModified: // Properties can be addedd, deleted or change values var objectInDatabase = ctx.DatabaseObjects.SingleOrDefault(f => f.FullyQualifiedName == databaseObject.FullyQualifiedName); if (objectInDatabase == null) { // We can't add a property to an object that hasn't been saved yet. throw new ArgumentException(nameof(objectInDatabase)); } objectInDatabase.Properties.Clear(); foreach (var property in databaseObject.Properties) { objectInDatabase.Properties.Add(DatabaseObjectConverter.Convert(property)); } ctx.Entry(objectInDatabase).State = EntityState.Modified; break; } } for (int i = 0; i < jaggedArray.Length; i++) { var json = JsonConvert.SerializeObject(jaggedArray[i]); ctx.DatabaseObjectVertices.Add(new DatabaseObjectVertex() { VertexId = i, AdjacencyListJson = json }); } ctx.SaveChanges(); } }