Skip to content

A lightweight web framework built around the Mediator pattern. Use as an alternative to ASP.NET for sites that only require simple web services.

License

Notifications You must be signed in to change notification settings

decoy/PingPongr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PingPongr Build status NuGet

A lightweight web service framework.

The framework itself is extremely lean, with minimal dependencies and most of the configuration burden pushed down to the IoC container and other middlwares. This allows for more flexibility when infrastructure concerns change without having to change your own business logic.

The core router concept was originally based off the infinitely useful MediatR by Jimmy Bogard. See his blog post "Tackling cross-cutting concerns with a mediator pipeline" for more information on using this pattern.

Getting Started

To get started with the common defaults, install these nuget packages. This includes the "core" PingPongr abstractions, extensions for working with AspNetCore's hosting, and a default JSON serializer.

Install-Package PingPongr
Install-Package PingPongr.Extensions.AspNetCore
Install-Package PingPongr.Serialization.JsonNet

Example

The basic API consists of the request, the response, and the core handler that processes the request.

Here's a fully functional and self hosted example.

namespace Examples.Simple
{
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using PingPongr;
    using PingPongr.Serialization.JsonNet;
    using System.Threading;
    using System.Threading.Tasks;

    // A request is unqiue per route.
    // It defines what response type is expected.
    public class Ping : IRouteRequest<Pong>
    {
        public string Message { get; set; }
    }

    // Response types can be shared between requests.
    public class Pong
    {
        public string Reply { get; set; }
    }

    // A handler processes a request and returns a response.
    public class PingHandler : IRouteRequestHandler<Ping, Pong>
    {
        public Task<Pong> Handle(Ping request, CancellationToken cancellationToken)
        {
            // Do something cool here. (This example is not cool.  Be cooler.  Be amazing.)
            return Task.FromResult(new Pong() { Reply = "re: " + request.Message });
        }
    }

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            // add the route handlers to the service registrations.
            services.AddRouteHandlers();

            // Using the PingPongr.JsonNet media handler.
            services.AddSingleton<IMediaTypeHandler, JsonNetMediaHandler>();
        }

        public void Configure(IApplicationBuilder app)
        {
            // Add PingPongr to the app pipeline.
            app.UsePingPongr();

            // By default, the endpoint will be available at:  Examples/Simple/Ping
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) => new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .Build();
    }
}

In this default example, sending { Message: 'Hello' } to http//:localhost:5000/Examples/Simple/Ping, would result in the response { Reply: 're: Hello'}.

Check out the Examples.Complex project to see a more fleshed out sample with custom routes, logging decorators and service injection.

About

A lightweight web framework built around the Mediator pattern. Use as an alternative to ASP.NET for sites that only require simple web services.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages