Giter Club home page Giter Club logo

nancy.restmodule's Introduction

Build status Coverage Status

Nancy.RestModule

Nancy.RestModule is designed to create REST resources in a fast and simple way with NancyFx. The RestModule decouples the NancyModule from your self-implemented controller, so the controller can be tested easily.

Nancy.RestModule can handle DELETE, GET, POST and PUT requests and provides a simple JSON. With the generic request handlers you are able to define your API interface by simple RequestModels which are bind automatically from NancyFx.

Installation

Install via nuget https://www.nuget.org/packages/Nancy.RestModule

PM> Install-Package Nancy.RestModule

How to use

  1. Create your RestModule in your NancyFx-Application.

    public class CustomerModule : RestModule
    {
        public CustomerModule() 
            : base("/customer")
        {
        }
    }
  2. Create your custom Controller and register it to the DIContainer. Now the Controller can be easily tested with UnitTests without Nancy.Testing

    public class CustomerController : ICustomerController
    {
        private readonly ICustomerService _service;
    
        public CustomerController(ICustomerService service) {
            _service = service;
        }
    
        public ResponseModel GetList() {
            IEnumerable<ICustomer> customers = _service.FindAll();
            return customers.CreateResponse();
        }
    
        public ResponseModel Get(GetCustomerRequest customerRequest) {
            ICustomer customer = _service.Find(customerRequest.Id);
            return customer.CreateResponse();
        }
        
        // ... etc.
    }
  3. Inject the controller in the module and define your routes and models. Now you can define your REST interface without pushing everything in the NancyModule.

    public class CustomerModule : RestModule
    {
        public CustomerModule(ICustomerController controller) 
            : base("/customer")
        {
            GetHandler("/", controller.GetList);
    
            GetHandler<GetCustomerRequest>("/{id}", controller.Get);
    
            PostHandler<PostCustomerRequest>("/", controller.Post);
    
            PutHandler<PutCustomerRequest>("/{id}", controller.Put);
        }
    }

Compile, run and enjoy the simplicity!

Demo

In the repository you can find Nancy.RestModule.Demo which is a simple demo to show how to use Nancy.RestModule.

  1. Compile and run the demo application.
  2. Now you can list all customers and should get the following response.
    [
        {
            "id": <Guid>,
            "firstName": "Jeff",
            "lastName": "Dunham",
            "age": 56,
            "created": <current datetime>
        },
        {
            "id": <Guid>,
            "firstName": "Lee",
            "lastName": "Evans",
            "age": 53,
            "created": <current datetime>
        },
        {
            "id": <Guid>,
            "firstName": "John",
            "lastName": "Cleese",
            "age": 79,
            "created": <current datetime>
        }
    ]
  3. (Optional) Change the resource with PUT and POST

Copyright

Copyright © 2017 Sean Roddis

License

Nancy.RestModule is licensed under MIT. Refer to license.txt for more information.

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.