//--------------------------------------------------------------------- // Instance members /// <summary> /// Constructor. /// </summary> /// <param name="database">The underlying <see cref="Lite.Database"/>.</param> /// <exception cref="InvalidOperationException">Thrown if the <paramref name="database"/> has already been associated with another entity database instance.</exception> private EntityDatabase(Database database) { Covenant.Requires <ArgumentNullException>(database != null); this.Base = database; nameToDatabase.Add(database.Name, this); WeakEventController.AddHandler <Database, DatabaseChangeEventArgs>(database, nameof(Base.Changed), OnDatabaseChanged); // Initialize the temporary database folder. This will located in the // same location as the Couchbase Lite database folder and will be // called [NAME.tmp] where NAME is the Couchbase Lite database name. // // Note that we're going to purge any existing files in the database // to ensure that orphaned attachments don't accumulate. // $hack(jeff.lill): // // This relies on [Database.ToString()] embedding the database folder // path like: // // "Database[C:\Users\jeff\AppData\Local\Temp\c840daac-9a1a-4b80-90ae-d903e9a0d090\test.cblite2]" // // and also that the attachment blobs are stored in the [attachments] subdirectory. var dbString = database.ToString(); var posStart = dbString.IndexOf('[') + 1; var posEnd = dbString.LastIndexOfAny(new char[] { '/', '\\' }); var folder = dbString.Substring(posStart, posEnd - posStart); BlobFolderPath = Path.Combine(dbString.Substring(posStart, dbString.Length - posStart - 1), "attachments"); if (!Directory.Exists(BlobFolderPath)) { throw new NotImplementedException(NotCompatibleError); } TempFolderPath = Path.Combine(folder, $"{database.Name}.tmp"); if (Directory.Exists(TempFolderPath)) { Directory.Delete(TempFolderPath, true); } Directory.CreateDirectory(TempFolderPath); }
//--------------------------------------------------------------------- // IDispose implementation /// <inheritdoc/> public void Dispose() { if (Base == null) { return; } var name = Base.Name; WeakEventController.RemoveHandler <Database, DatabaseChangeEventArgs>(Base, nameof(Base.Changed), OnDatabaseChanged); Base.Dispose(); Base = null; lock (syncRoot) { nameToDatabase.Remove(name); } }
public void RemoveListener(EventSource source) { WeakEventController.RemoveHandler <EventSource, EventArgs>(source, nameof(EventSource.Event), OnEvent); }