/// <summary> /// Handles file changed events. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e) { if (!lastFileChanged.Equals(e.Name)) { string fileName = e.Name; if (fileName.Contains(".xml")) { fileName = fileName.Replace(".xml", ""); if (fileName.Contains("Database")) { OnDatabaseChanged?.Invoke(this, new OnDatabaseChangedEventArgs(fileName, DateTime.Now)); } } lastFileChanged = e.Name; } else { lastFileChanged = string.Empty; } Console.WriteLine("Changed " + e.FullPath); }
public void Add <T>(T t) where T : IDbEntity { AddType <T>(); Type type = typeof(T); int id = ++IndexTracker[type]; t.EntityId = id; Database[type].Add(t); ApplyChanges(); OnDatabaseChanged?.Invoke(this); OnEntityAdded?.Invoke(t); }
/// <summary> /// Saves a selection of items in its respective database. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="items"></param> public void Save <T>(ICollection <T> items) where T : ICollectableObject { if (items.IsNull()) { throw new ArgumentNullException("The parameter items cannot be null."); } DatabaseItemInfo <T> dbInfo = GetDatabaseItemInfo <T>(); var db = GetType().GetMethod("GetDatabase", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(dbInfo.DatabaseType).Invoke(this, null); Type typeOfCollection = db.GetType().GetProperty(dbInfo.CollectionName).PropertyType; object newItems = EnumerateCollection <T>(items); if (typeOfCollection.BaseType == typeof(Array)) { newItems = (newItems as ICollection <T>).ToArray(); } else if (typeOfCollection.BaseType == typeof(Collection <T>)) { newItems = (newItems as ICollection <T>).ToObservableCollection(); } db.GetType().GetProperty(dbInfo.CollectionName).SetValue(db, newItems); string path = GetDatabasePath(dbInfo.DatabaseType); lock (lockObject) { var xml = db.Serialize(); try { try { if (!mutex.IsNull()) { mutex.WaitOne(); } xml.Save(path); //Will fire the database changed event when the FileSystemWatcher is unavailable. //This should happen only when using Xamarin.Forms as there is no native support for that class. if (fileSystemWatcher.IsNull()) { OnDatabaseChanged?.Invoke(this, new OnDatabaseChangedEventArgs(dbInfo.DatabaseType.Name, DateTime.Now)); } } catch { throw; } finally { if (!mutex.IsNull()) { mutex.ReleaseMutex(); } } } catch { throw; } finally { } } }
/// <summary> /// Remove entity from Db /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public void Remove <T>(T t) where T : IDbEntity { AddType <T>(); Type type = typeof(T); Database[type].Remove(t); // check entity references foreach (KeyValuePair <Type, HashSet <IDbEntity> > kv in Database) { Type entityType = kv.Key; EntityInfo infos = GetEntityInfo(entityType); foreach (IDbEntity entity in kv.Value) { bool changed = false; // single refs foreach (SingleReferenceProperty referenceProperty in infos.SingleReferenceInjectors) { IDbReference dbReference = (IDbReference)referenceProperty.PropertyAccessor.GetValue(entity); if (dbReference.ReferenceId == t.EntityId && dbReference.EntityType == type) { dbReference.ReferenceId = null; dbReference.Entity = null; changed = true; } } // multiple refs foreach (MultipleReferenceProperty referenceProperty in infos.MultipleReferenceInjectors) { IList dbReference = (IList)referenceProperty.PropertyAccessor.GetValue(entity); if (dbReference == null) { continue; } for (int i = dbReference.Count - 1; i >= 0; i--) { IDbReference reference = (IDbReference)dbReference[i]; if (reference.ReferenceId == t.EntityId && reference.EntityType == type) { dbReference.RemoveAt(i); changed = true; } } } if (changed) { OnEntityChanged?.Invoke(entity); } } } OnDatabaseChanged?.Invoke(this); OnEntityRemoved?.Invoke(t); }