示例#1
0
        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;
        }
示例#2
0
 protected abstract void OnDatabaseInitializing(DatabaseConfigurationBuilder <TDatabase> configurator);