Giter VIP home page Giter VIP logo

responsibilitychain's Issues

Should be able to cancel an asynchronous operation

Current IAsyncHandler<TIn, TOut> definition:

public interface IAsyncHandler<TIn, TOut> : IHandler
{
    Task<TOut> HandleAsync(TIn input, Func<TIn, Task<TOut>> next);
}

HandleAsync(), once started, couldn't be cancelled.

It should look like this:

public interface IAsyncHandler<TIn, TOut> : IHandler
{
    Task<TOut> HandleAsync(TIn input, Func<TIn, CancellationToken, Task<TOut>> next, CancellationToken cancellationToken);
}

Have a `Handle` method, which accepts `input` object only and no `next` delegate

This is the current interface definition of IHandler<TIn, TOut> and IAsyncHandler<TIn, TOut>

public interface IHandler<TIn, TOut> : IHandler
{
    TOut Handle(TIn input, Func<TIn, TOut> next);
}

public interface IAsyncHandler<TIn, TOut> : IHandler
{
    Task<TOut> HandleAsync(TIn input, Func<TIn, Task<TOut>> next);
}

When client consumes this API via a composite handler, she often has to pass null as the next handler. This is inconvenient and adds noise to the code base. Having an additional Handle method on the abstract composite, which requires the input object only and no next delegate, improves readability.

All nested handlers and their dependencies should be injected via constructor

Framework currently supports in resolving dependencies via an injected IServiceProvider instance in the composite handler's constructor like this

public class WorkLogParser : Handler<string, int>
    public WorkLogParser(IServiceProvider serviceProvider) : base(serviceProvider)
    {
        AddHandler<NestedHandler1>();
        AddHandler<NestedHandler2>();
    }
}

This makes SUT (WorkLogParser) hard to test in case one wants to mock out a nested handler (such as logging, database access,...) or a dependency of a nested handler.
So a handler (and composite handler) should expose all dependencies via constructor parameters instead of the service locator anti-pattern.

public class WorkLogParser : Handler<string, int>
    public WorkLogParser(NestedHandler1 nestedHandler1, NestedHandler2 nestedHandler2)
    {
        AddHandler(nestedHandler1);
        AddHandler(nestedHandler2);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.