- Notifications
You must be signed in to change notification settings - Fork0
License
SRoddis/Nancy.RestModule
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Nancy.RestModule is designed to create REST resources in a fast and simple way with NancyFx. TheRestModule
decouples theNancyModule
from your self-implemented controller, so the controller can be tested easily.
Nancy.RestModule can handleDELETE
,GET
,POST
andPUT
requests and provides a simple JSON.With the generic request handlers you are able to define your API interface by simpleRequestModels
which are bind automatically from NancyFx.
Install via nugethttps://www.nuget.org/packages/Nancy.RestModule
PM> Install-Package Nancy.RestModule
Create your
RestModule
in your NancyFx-Application.publicclassCustomerModule:RestModule{publicCustomerModule():base("/customer"){}}
Create your custom
Controller
and register it to theDIContainer
.Now theController
can be easily tested withUnitTests
withoutNancy.Testing
publicclassCustomerController:ICustomerController{privatereadonlyICustomerService_service;publicCustomerController(ICustomerServiceservice){_service=service;}publicResponseModelGetList(){IEnumerable<ICustomer>customers=_service.FindAll();returncustomers.CreateResponse();}publicResponseModelGet(GetCustomerRequestcustomerRequest){ICustomercustomer=_service.Find(customerRequest.Id);returncustomer.CreateResponse();}// ... etc.}
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
.publicclassCustomerModule:RestModule{publicCustomerModule(ICustomerControllercontroller):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!
In the repository you can findNancy.RestModule.Demo which is a simple demo to show how to use Nancy.RestModule.
- Compile and run the demo application.
- Now you canlist 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> }]
(Optional)
Change the resource withPUT
andPOST
Copyright © 2017 Sean Roddis
Nancy.RestModule is licensed underMIT. Refer to license.txt for more information.