Skip to content

Data Worker

Liam edited this page Mar 26, 2021 · 8 revisions

What is the data worker

The data worker is responsible for managing the operations of storing, retrieving and deleting data records.

What does a data worker have to do

Several methods are needed from a data worker; these are defined on the data worker interface (IDataWorker). We will now take a look at each method and discuss what the intent of the method is.

Write

The write method is intened to stage data ready to be commited to the data storage system. For example if storing data in a file the write method would write the message pack serialized data to the file stream but would not close the stream. The write method is not intended to commit data to the store as there are some operations that might happen after write that means the data write should be discarded. The write method should allow for data to be create new and update existing data if it exists.

Task<bool> Write<TDataType>(int id, TDataType data) where TDataType : DataEntity;

The Id is the id that the data manager has provided for the data in the default data worker this will be the Id property from the data object but other data managers may not use this as the Id when calling the write method.

Commit

The commit method is intened to commit the staged data that has been propered by a write method call for the specific data id.

Task<bool> Commit<TDataType>(int id) where TDataType : DataEntity;

In the case of the file write example from the write method the commit method would close the file stream to flush the staged data onto the disk.

Write and commit

The data manager may decided that both a write and a commit should happen at once to handle this the data worker has a method for preforming this operation. When creating your own data worker it is advicable to have this method use the write method and commit methods from above and to handle the case of an operatio failing gracefully.

Task<bool> WriteAndCommit<TDataType>(int id, TDataType data) where TDataType : DataEntity;

As you can see the write and commit method takes in both the id and the data to be written.

Discard changes

The data manager may decide that the data that has been staged is not to be written due to another part of a write process failing. This method should dispose of any data pending a commit so that it will never be commited.

Task DiscardChanges<TDataType>(int id) where TDataType : DataEntity;

The id is the id of the data that was requested to be stage.

Rollback

This method is used to undo any changes to data that have been commited. The rollback method should override any data stored on the id provided with the data provided, this mean that the method should act like write and commit but the is intended to never fail it should always keep trying to persist the data until it can.

Task Rollback<TDataType>(int id, TDataType data) where TDataType : DataEntity;

Read

This method should retreave the data from the storage system as a desirialized object.

Task<TDataType> Read<TDataType>(int id) where TDataType : DataEntity;

Read all

This method reads all the data for the data type from the data store

IAsyncEnumerable<TDataType> ReadAll<TDataType>() where TDataType : DataEntity;

Exists

The data manager or the user of the data manager may wish to know if the data exists in the storage system. The exists method should return true if the data is commited to the store and false if not.

Task<bool> Exists<TDataType>(int id) where TDataType : DataEntity;

Delete

Data needs to be deleted this method should preform operations that result in the data not being returned by the read method and the exists method not returning the data. Also any data that has been deleted should be allowed to be recreated using the write method.

Task<bool> Delete<TDataType>(int id) where TDataType : DataEntity;

Undelete

Some implmentations of the data worker may wish to allow soft deletion of data in the case you need to implment the undelete method so that is restores the data to it's last known commited state. If you implmentation will not allow data to be soft deleted or restored then you should return false from this method.

Task<bool> Undelete<TDataType>(int id) where TDataType : DataEntity;

The id is the id of the data that is deleted currently that should be restored.

Next id

The next id method should return the next valid id that can be used by the data manager to create a new record in the storage system.

int NextId<TDataType>() where TDataType : DataEntity;