Skip to content

Latest commit

 

History

History
121 lines (84 loc) · 4.5 KB

RefactoringTutorial.md

File metadata and controls

121 lines (84 loc) · 4.5 KB

Refactoring To Async Tutorial

Contents

Prep

Step by Step

Notes

Pipeline-based functions have the following stucture:

  1. All pipeline setup
  2. Then PipelineApprovals.Verify()
  3. Then send input through the pipeline and extract the result.

Template to copy into your code to guide you through the refactoring

// Set up Pipeline objects
// `PipelineApprovals.Verify()`
// Send input through pipeline
// Original code

1. Call the method in question from a test

Call the method you wish to refactor from a test. Don't think of this as a traditional unit test. Think of it more as a specialized main() method

2. Take the 1st thing to move to async and create an InputPipe of it's parameters directly above it.

var inputNamePipe = new InputPipe<InputType>("InputName");

3. Place a ApprovalTests to get insight into the pipeline.

Place this right in the middle of the production code you want to refactor. It is a temporary step.

PipelineApprovals.Verify(inputNamePipe);

With a DotReporter (on the test)

[UseReporter(typeof(DotReporter))]

4. Inspect result in VsCode

5. Add a process as a delegate

If the code isn't in a method, use a lambda instead

 var methodCallPipe = startingPoint.ProcessFunction(TheMethodCall);
 var methodCallPipe = startingPoint.Process(x => x * 2);

Do this above the PipelineApprovals.Verify(). Run it again for feedback.

6. Add a collector and send input in

 var methodCallCollector = methodCallPipe.Collect();
 inputNamePipe.Send(firstParameter);
 var variable = methodCallCollector.SingleResult;

7. Process and Collect the next step in the pipeline

8. Delete any newly-dead code

Previously you were using .Collect() to pull out values from intermediate steps, but as they become unnecessary you can delete them. ReSharper is good for finding dead code.

9. Repeat until all code is converted

10. Use GetInputs<> to collect all the inputs and outputs in a single object:

var inputsAndOutputs = bestSandwichCollector.GetInputs<ZipCode>().AndOutputs<Sandwich>().AsTuple();
inputsAndOutputs.Send(zipCode);
inputsAndOutputs.Outputs...

10. Extract a CreatePipe() function

11. Move the PipelineApprovals.Verify() call into a unit test

var pipe = SimpleCalls.CreatePipe();
PipelineApprovals.Verify(pipe.Input1);

Handling multiple parameters

Every function has a single input and a single output. To use multiple input parameters, you'll have to join them into Tuples. If this requires new inputs, you'll need multiple InputPipes.