# Extending Pipelines

Ucommerce Pipelines are the primary extension points where you can execute any custom logic to fit your requirements. These extensions could be changing the existing behavior of the pipelines, adding new steps to be executed, and integrating with other systems.&#x20;

## Implementing a Task

Before implementing a new Pipeline Task, we must [identify which pipeline we want to extend](https://dev.ucommerce.net/readme/how-to/discover-pipelines-and-their-tasks), specifically the input and output parameter types. In the examples below, we'll use XPipelineInput and XPipelineOutput to signify the types, as they follow the naming convention of the Pipeline name followed by Input or Output.

* Create a new Class.
* Inherit from `IPipelineTask<XPipelineInput, XPipelineOutput>`
* Implement the Execute method.

{% hint style="info" %}
**Context.Input** contains information passed to the Pipeline when it is being executed.

**Context.Output** contains information that will be returned once the Pipeline is finished executing.
{% endhint %}

{% hint style="info" %}
A Pipeline will save changes in the current database context as the last task. Any tracked entity changes will be saved.
{% endhint %}

```csharp
public class MyCustomTask : IPipelineTask<XPipelineInput, XPipelineOutput>
{
    public CascadeMode CascadeMode { get; } = CascadeMode.Continue;

    public Task Execute(PipelineContext<XPipelineInput, XPipelineOutput> context, CancellationToken cancellationToken)
    {
        // Your custom logic.
    }
}
```

## Adding a Task

The next step is registering the PipelineTask in the IoC container for the proper Pipeline and position. This can be done via the Ucommerce builder, and it is recommended to use a helper method for reusability and clarity.

### Extension method example

This example displays different ways of modifying a pipeline.

```csharp
/// <summary>
/// Extension for adding my custom pipeline task.
/// </summary>
public static class CustomPipelineExtensions
{
    /// <summary>
    /// Adds Custom Cart Extension.
    /// </summary>
    public static IUcommerceBuilder AddMyCartExtension(this IUcommerceBuilder builder)
    {
        //These examples all modify the default 'CalculateCart' pipeline
        
        //This will insert the task 'MyCustomTask1' just before 'SaveUpdatedCartPipelineTask'
        builder.InsertPipelineTaskBefore<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask1>(
            typeof(SaveUpdatedCartPipelineTask));
            
        //This will insert it just after 'SaveUpdatedCartPipelineTask'
        builder.InsertPipelineTaskAfter<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask2>(
            typeof(SaveUpdatedCartPipelineTask));
            
        //This will insert it as the first task of the pipeline
        builder.InsertPipelineTaskFirst<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask3>();
        
        //This will insert it as the last task of the pipeline
        builder.InsertPipelineTaskLast<IPipelineTask<CalculateCartInput, CalculateCartOutput>, MyCustomTask4>();
        
        //This will remove the default task 'SaveUpdatedCartPipelineTask' from the pipeline
        builder.RemovePipelineTask<IPipelineTask<CalculateCartInput, CalculateCartOutput>, SaveUpdatedCartPipelineTask>();
        
        return builder;
    }
}
```

{% hint style="info" %}
Replace tasks by placing yours before or after the default task, then remove the default task.
{% endhint %}

### Register your custom pipeline tasks, using an extension method

Now you can register all of your customized pipeline tasks using your extension method like this.

```csharp
builder.Services.AddUcommerce(builder.Configuration)
    // ... register other Ucommerce components first
    .AddMyCartExtension()
    .Build();
```

{% hint style="info" %}
When adding custom tasks, remember to do it after adding all of the Ucommerce components. This way, you can be sure that your custom logic is added in the desired order and registered correctly.
{% endhint %}
