Skip to content

ASP.NET Core MVC Controller generation for RequestHandlers

License

Notifications You must be signed in to change notification settings

Smartasses/RequestHandlers.Mvc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

ASP.NET Core MVC Controller generation for RequestHandlers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages