lundi 19 octobre 2015

Implemeting "Cancel" functionality Pattern C#

I wrote a c# program which performs a system backup.

The backup process basically reads data from a database and creates a backup in a file system.

The backup process takes something like 45-75 seconds to complete, and my product manager had requested that I implement a cancel functionality.

So here is what I did:

  1. If cancel was applied, I keep a "cancel requested" record in the database.

  2. Throughout the backup process (which consists of several steps), i'm calling a method ThrowIfCancelRequested() and rolls back the process if so.

Now the thing is it makes my code ugly, now I got this ThrowIfCancelRequested() all over the place, I mean it gets the job done but its ugly.

I also thought of maybe redesigning my backup process structure so that my main backup process would be injected with backup steps that would contain a cancel action.

E.g.

interface IBackupStep
{
    void Execute();

    void OnCancel();
}

void main(IEnumerable<IBackupStep> steps)
{
    foreach (var step in steps)
    {
        step.Execute()
    }
}

The problem here that this is almost impossible for me to do, the backup process was not initially written by me, and its very difficult to refactor it's god objects code.

So my question is, maybe there is another alternative I'm missing that would also make my code work and be better designed as well?

Aucun commentaire:

Enregistrer un commentaire