jeudi 13 février 2020

Reuse builder step across different builders

For a C# data access layer I want to use the builder design pattern to build queries against DBContexts (for greater flexiblity in the buisiness layer but without direct access to IQueryable) in a fluent API way.

Many queries have a similar structure:

AccountQueryBuilder.GetAccounts().ForClient("clienta").WithBalanceOver(1943.4).QueryAsync();
/// ...
ContractQueryBuilder.GetContracts().ForClient("clienta").ExpiresAfter(2020-04-01).QueryAsync();

Here the ForClient(string) is reused between different builders. How would I design the builder, so that I can reuse different builder steps across different builders, by still restricting the next available builder steps and not having to implement ForClient(string) for each builder again? There will be multiple reusable steps in different combinations

Currently I have something like this:

class BuilderA : IBuilderReady, IBuilderStepA, IBuilderStepB, IBuilderStepC, IReusableStepA<IBuilderStepA>
{
    IBuilderStepA ReusableA() {/*Don't want to reimplement in every builder*/}
    IBuilderStepB A() {...}
    IBuilderStepC B() {...}
    IBuilderReady C() {...}
    IEnumerable<EntitySomething> Query() {...}
}

Aucun commentaire:

Enregistrer un commentaire