예제 #1
0
        /// <inheritdoc />
        public Task Delete(TKey key, string concurrencyToken)
        {
            Logger.LogDebug("Deleting model with primary key '{Pk}'", key);

            using (var file = VisionFileSystem.GetVisionFile(FileName))
            {
                // Read the record with lock
                file.Open(FileOpenMode.InputOutput);
                var record = file.GetNewRecord();

                // Check for concurrency
                MapPrimaryKeyToVisionRecord(key, record);
                var oldRecord = file.ReadLock(record);
                if (oldRecord == null)
                {
                    return(Task.CompletedTask);
                }
                var oldModel = GetModel(oldRecord);
                if (concurrencyToken != oldModel.ConcurrencyToken)
                {
                    throw new ConcurrencyException($"Data for model with primary key '{key}' are changed since the last read. Cannot delete the record");
                }

                // Delete the record
                file.Delete(oldRecord);

                file.Close();
                Logger.LogDebug("Deleted record {Record}", oldRecord);
            }
            return(Task.CompletedTask);
        }
예제 #2
0
        /// <inheritdoc />
        public Task Insert(TModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            Logger.LogDebug("Inserting model {Model}", model);

            using (var file = VisionFileSystem.GetVisionFile(FileName))
            {
                file.Open(FileOpenMode.InputOutput);
                var record = file.GetNewRecord();
                MapModelToVisionRecord(model, record);
                file.Write(record);
                file.Close();
                Logger.LogDebug("Inserted record {Record}", record);
            }
            return(Task.CompletedTask);
        }
예제 #3
0
        /// <inheritdoc />
        public Task Update(TModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            Logger.LogDebug("Updating model {Model}", model);

            using (var file = VisionFileSystem.GetVisionFile(FileName))
            {
                // Read the record with lock
                file.Open(FileOpenMode.InputOutput);
                var record = file.GetNewRecord();

                // Check for concurrency
                MapPrimaryKeyToVisionRecord(model.Pk, record);
                var oldRecord = file.ReadLock(record);
                if (oldRecord == null)
                {
                    throw new ConcurrencyException($"Data for model with primary key '{model.Pk}' are deleted. Cannot update the record");
                }
                var oldModel = GetModel(oldRecord);
                if (model.ConcurrencyToken != oldModel.ConcurrencyToken)
                {
                    throw new ConcurrencyException($"Data for model with primary key '{model.Pk}' are changed since the last read. Cannot update the record");
                }

                // Update the record
                MapModelToVisionRecord(model, oldRecord);
                file.Rewrite(oldRecord);


                file.Close();
                Logger.LogDebug("Updated record {Record}", oldRecord);
            }
            return(Task.CompletedTask);
        }