Coder Social home page Coder Social logo

requesthandlers.mvc's Introduction

RequestHandlers.Mvc NuGet Build

RequestHandlers is a framework that helps you structure your code into small, easy-to-test units of work.

When you are writing an MVC-application, your controllers sometimes get cluttered with too much actions and code. If you use the classes and conventions given to you by RequestHandlers, your code will be more testable and comprehensable.

This package is specifically built for ASP.NET Core MVC.

Getting Started

We'll split up this section in Setup and Creating a RequestHandler

Setup

  1. Download the NuGet package with the dotnet command
    > dotnet add package RequestHandlers.Mvc
    or via the NuGet Package Manager in Visual Studio
  2. Add RequestHandlers.Mvc to your registered services
    This will also implicitly add Mvc-services
    The assembly passed in the method, is the assembly in which our RequestHandlers are present.
// Inside Startup.cs  
public void ConfigureServices(IServiceCollection services)  
{  
    services  
        .AddRequestHandlers(this.GetType().GetTypeInfo().Assembly);  
}  
  1. Add Mvc to your Application
// Inside Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
   app.UseMvc();
}

Creating a RequestHandler

Now we're all set up to create our first RequestHandler.

Add a file to your application named MyFirstRequestHandler.cs In this file Add a 3 classes: MyFirstRequest, MyFirstResponse, and MyFirstRequestHandler.

Make MyFirstRequest implement IReturn<MyFirstResponse> and give a a GetRequest-attribute with the parameter "api/my-first-request-handler". This represents the url on which you can call the request handler.
and MyFirstRequestHandler should implement IRequestHandler<MyFirstRequest, MyFirstResponse>
This should look something like this:

[GetRequest("api/my-first-request-handler")]
public class MyFirstRequest : IReturn<MyFirstResponse>
{
}
public class MyFirstResponse
{
}
public class MyFirstRequestHandler : IRequestHandler<MyFirstRequest, MyFirstResponse>
{
   public MyFirstResponse Handle(MyFirstRequest request)
   {
       return new MyFirstResponse();
   }
}

At this point, you should be able to run the application and request the configured url. By default in an ASP.NET Core application this should be http://localhost:5000/api/my-first-request-handler.

Adding url-variables and query string paramaters

Now we can call a simple, static url. But you probably want to pass some parameters to your RequestHandler. You can add some properties to the request and response like this:

[GetRequest("api/my-first-request-handler/{myParameter}")]
public class MyFirstRequest : IReturn<MyFirstResponse>
{
   public int MyParameter { get; set; }
}
public class MyFirstResponse
{
   public int BodyProperty { get; set; }
}

Those can be used inside the RequestHandler as so:

public class MyFirstRequestHandler : IRequestHandler<MyFirstRequest, MyFirstResponse>
{
    public MyFirstResponse Handle(MyFirstRequest request)
    {
        return new MyFirstResponse { BodyProperty = request.MyParameter + 10 };
    }
}

If you run and request http://localhost:5000/api/my-request-handler/15 the response will be

{
    "BodyProperty": 25
}

These are the most basic examples of using RequestHandlers.

requesthandlers.mvc's People

Contributors

yentheo avatar glenndierckx avatar bennym avatar danielmertens avatar

Watchers

James Cloos avatar

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.