public void Initialize() { if (_initialized) { return; } var configurator = new DatabaseConfigurationBuilder <TDatabase>(); OnDatabaseInitializing(configurator); if (configurator.DatabaseFactory == null) { throw new InvalidOperationException("database factory is undefined"); } _databaseEncryption = configurator.DatabaseEncryption ?? new NoDatabaseEncryption(); _databaseSerializer = configurator.DatabaseSerializer ?? new DataContractDatabaseSerializer <TDatabase>(); var file = _directoryContext.GetFile(_fileName); if (file != null) { try { _database = _databaseSerializer.Deserialize(_databaseEncryption.Decrypt(file.Content)); } catch (CryptographicException) { _database = configurator.DatabaseFactory.Create(); } catch (FormatException) { _database = configurator.DatabaseFactory.Create(); } catch (SerializationException) { _database = configurator.DatabaseFactory.Create(); } catch (InvalidCastException) { _database = configurator.DatabaseFactory.Create(); } } else { _database = _database = configurator.DatabaseFactory.Create(); } _initialized = true; }
protected abstract void OnDatabaseInitializing(DatabaseConfigurationBuilder <TDatabase> configurator);