-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE]: Improve builder pattern (#35)
* Refactor builders to eliminate reliance on partial interfaces and classes --------- Co-authored-by: Brenton Farmer <[email protected]>
- Loading branch information
1 parent
e5a01f2
commit de4801b
Showing
17 changed files
with
468 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,47 @@ | ||
using Hyperbee.Pipeline.Binders; | ||
using Hyperbee.Pipeline.Extensions.Implementation; | ||
|
||
namespace Hyperbee.Pipeline; | ||
|
||
public partial interface IPipelineBuilder<TInput, TOutput> | ||
public static class CallBlockBuilder | ||
{ | ||
IPipelineBuilder<TInput, TOutput> Call( Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ); | ||
IPipelineBuilder<TInput, TOutput> Call( bool inheritMiddleware, Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ); | ||
} | ||
|
||
public partial class PipelineBuilder<TInput, TOutput> | ||
{ | ||
// Call an inner builder discarding the final result. Acts like an Action. | ||
public static IPipelineBuilder<TInput, TOutput> Call<TInput, TOutput>( | ||
this IPipelineBuilder<TInput, TOutput> parent, | ||
Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder | ||
) | ||
{ | ||
return CallBlockBuilder<TInput, TOutput>.Call( parent, true, builder ); | ||
} | ||
|
||
public IPipelineBuilder<TInput, TOutput> Call( Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ) | ||
public static IPipelineBuilder<TInput, TOutput> Call<TInput, TOutput>( | ||
this IPipelineBuilder<TInput, TOutput> parent, | ||
bool inheritMiddleware, | ||
Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder | ||
) | ||
{ | ||
return Call( true, builder ); | ||
return CallBlockBuilder<TInput, TOutput>.Call( parent, inheritMiddleware, builder ); | ||
} | ||
} | ||
|
||
public IPipelineBuilder<TInput, TOutput> Call( bool inheritMiddleware, Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ) | ||
internal static class CallBlockBuilder<TInput, TOutput> | ||
{ | ||
public static IPipelineBuilder<TInput, TOutput> Call( | ||
IPipelineBuilder<TInput, TOutput> parent, | ||
bool inheritMiddleware, | ||
Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder | ||
) | ||
{ | ||
ArgumentNullException.ThrowIfNull( builder ); | ||
|
||
var block = PipelineFactory.Start<TOutput>( inheritMiddleware ? Middleware : null ); | ||
var (parentFunction, parentMiddleware) = parent.GetPipelineFunction(); | ||
|
||
var block = PipelineFactory.Start<TOutput>( inheritMiddleware ? parentMiddleware : null ); | ||
var function = builder( block ).CastFunction<TOutput, object>(); // cast because we don't know the final Pipe output value | ||
|
||
return new PipelineBuilder<TInput, TOutput> | ||
{ | ||
Function = new CallBlockBinder<TInput, TOutput>( Function ).Bind( function ), | ||
Middleware = Middleware | ||
Function = new CallBlockBinder<TInput, TOutput>( parentFunction ).Bind( function ), | ||
Middleware = parentMiddleware | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,50 @@ | ||
using Hyperbee.Pipeline.Binders; | ||
using Hyperbee.Pipeline.Extensions.Implementation; | ||
|
||
namespace Hyperbee.Pipeline; | ||
|
||
public partial interface IPipelineBuilder<TInput, TOutput> | ||
public static class CallIfBuilder | ||
{ | ||
IPipelineBuilder<TInput, TOutput> CallIf( Function<TOutput, bool> condition, Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ); | ||
IPipelineBuilder<TInput, TOutput> CallIf( Function<TOutput, bool> condition, bool inheritMiddleware, Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ); | ||
} | ||
|
||
public partial class PipelineBuilder<TInput, TOutput> | ||
{ | ||
public IPipelineBuilder<TInput, TOutput> CallIf( Function<TOutput, bool> condition, Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ) | ||
public static IPipelineBuilder<TInput, TOutput> CallIf<TInput, TOutput>( | ||
this IPipelineBuilder<TInput, TOutput> parent, | ||
Function<TOutput, bool> condition, | ||
Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder | ||
) | ||
{ | ||
return CallIfBuilder<TInput, TOutput>.CallIf( parent, condition, true, builder ); | ||
} | ||
public static IPipelineBuilder<TInput, TOutput> CallIf<TInput, TOutput>( | ||
this IPipelineBuilder<TInput, TOutput> parent, | ||
Function<TOutput, bool> condition, | ||
bool inheritMiddleware, | ||
Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder | ||
) | ||
{ | ||
return CallIf( condition, true, builder ); | ||
return CallIfBuilder<TInput, TOutput>.CallIf( parent, condition, inheritMiddleware, builder ); | ||
} | ||
} | ||
|
||
public IPipelineBuilder<TInput, TOutput> CallIf( Function<TOutput, bool> condition, bool inheritMiddleware, Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder ) | ||
internal static class CallIfBuilder<TInput, TOutput> | ||
{ | ||
public static IPipelineBuilder<TInput, TOutput> CallIf( | ||
IPipelineBuilder<TInput, TOutput> parent, | ||
Function<TOutput, bool> condition, | ||
bool inheritMiddleware, | ||
Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder> builder | ||
) | ||
{ | ||
ArgumentNullException.ThrowIfNull( builder ); | ||
ArgumentNullException.ThrowIfNull( condition ); | ||
|
||
var block = PipelineFactory.Start<TOutput>( inheritMiddleware ? Middleware : null ); | ||
var (parentFunction, parentMiddleware) = parent.GetPipelineFunction(); | ||
|
||
var block = PipelineFactory.Start<TOutput>( inheritMiddleware ? parentMiddleware : null ); | ||
var function = builder( block ).CastFunction<TOutput, object>(); // cast because we don't know the final Pipe output value | ||
|
||
return new PipelineBuilder<TInput, TOutput> | ||
{ | ||
Function = new CallIfBlockBinder<TInput, TOutput>( condition, Function ).Bind( function ), | ||
Middleware = Middleware | ||
Function = new CallIfBlockBinder<TInput, TOutput>( condition, parentFunction ).Bind( function ), | ||
Middleware = parentMiddleware | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,49 @@ | ||
using Hyperbee.Pipeline.Binders; | ||
using Hyperbee.Pipeline.Extensions.Implementation; | ||
|
||
namespace Hyperbee.Pipeline; | ||
|
||
public partial interface IPipelineBuilder<TInput, TOutput> | ||
public static class ForEachBlockBuilder | ||
{ | ||
IPipelineBuilder<TInput, TOutput> ForEach<TElement>( Func<IPipelineStartBuilder<TElement, TElement>, IPipelineBuilder> builder ); | ||
IPipelineBuilder<TInput, TOutput> ForEachAsync<TElement>( bool inheritMiddleware, Func<IPipelineStartBuilder<TElement, TElement>, IPipelineBuilder> builder ); | ||
} | ||
public static ForEachBlockBuilderWrapper<TInput, TOutput> ForEach<TInput, TOutput>( this IPipelineBuilder<TInput, TOutput> parent ) | ||
{ | ||
return new ForEachBlockBuilderWrapper<TInput, TOutput>( parent ); | ||
} | ||
|
||
public partial class PipelineBuilder<TInput, TOutput> | ||
{ | ||
public IPipelineBuilder<TInput, TOutput> ForEach<TElement>( Func<IPipelineStartBuilder<TElement, TElement>, IPipelineBuilder> builder ) | ||
public class ForEachBlockBuilderWrapper<TInput, TOutput>( IPipelineBuilder<TInput, TOutput> parent ) | ||
{ | ||
return ForEachAsync( true, builder ); | ||
public IPipelineBuilder<TInput, TOutput> Type<TElement>( Func<IPipelineStartBuilder<TElement, TElement>, IPipelineBuilder> builder ) | ||
{ | ||
return ForEachBlockBuilder<TInput, TOutput, TElement>.ForEach( parent, true, builder ); | ||
} | ||
|
||
public IPipelineBuilder<TInput, TOutput> Type<TElement>( bool inheritMiddleware, Func<IPipelineStartBuilder<TElement, TElement>, IPipelineBuilder> builder ) | ||
{ | ||
return ForEachBlockBuilder<TInput, TOutput, TElement>.ForEach( parent, inheritMiddleware, builder ); | ||
} | ||
} | ||
} | ||
|
||
public IPipelineBuilder<TInput, TOutput> ForEachAsync<TElement>( bool inheritMiddleware, Func<IPipelineStartBuilder<TElement, TElement>, IPipelineBuilder> builder ) | ||
internal static class ForEachBlockBuilder<TInput, TOutput, TElement> | ||
{ | ||
public static IPipelineBuilder<TInput, TOutput> ForEach( | ||
IPipelineBuilder<TInput, TOutput> parent, | ||
bool inheritMiddleware, | ||
Func<IPipelineStartBuilder<TElement, TElement>, IPipelineBuilder> builder | ||
) | ||
{ | ||
ArgumentNullException.ThrowIfNull( builder ); | ||
|
||
var block = PipelineFactory.Start<TElement>( inheritMiddleware ? Middleware : null ); | ||
var (parentFunction, parentMiddleware) = parent.GetPipelineFunction(); | ||
|
||
var block = PipelineFactory.Start<TElement>( inheritMiddleware ? parentMiddleware : null ); | ||
var function = builder( block ).CastFunction<TElement, object>(); // cast because we don't know the final Pipe output value | ||
|
||
return new PipelineBuilder<TInput, TOutput> | ||
{ | ||
Function = new ForEachBlockBinder<TInput, TOutput, TElement>( Function ).Bind( function ), | ||
Middleware = Middleware | ||
Function = new ForEachBlockBinder<TInput, TOutput, TElement>( parentFunction ).Bind( function ), | ||
Middleware = parentMiddleware | ||
}; | ||
} | ||
} | ||
|
Oops, something went wrong.