Home .NET Creating the first application on NancyFX. Part four. Continuing work with modules

Creating the first application on NancyFX. Part four. Continuing work with modules

by admin

In the previous article Creating the first app on NancyFX. Part Three. Nancy Modules. We have got acquainted with the modules of Nancy. In this article we will continue to learn more about them.
As I mentioned in a previous article, Nancy uses the Request.Form. Form is a DynamicDictionary whose values are taken from the body of the request. In order for Nancy to render the values in the request body correctly Content-Type: application/x-www-form-urlencoded must be set correctly. If the Content-Type was set incorrectly, don’t be surprised if you get ppl as the values. Nancy automatically parses only x-www-form-urlencoded data. So let’s try to create a module which will process the Post request. First of all let’s create the following class :

namespace NancyFxApplication{public class Dish{public int Id { get; set; }public string Name { get; set; }public bool IsSpicy { get; set; }}}

and class

using System.Collections.Generic;namespace NancyFxApplication{public static class DishesRepository{public static List<Dish> Dishes = new List<Dish>{new Dish { Id=1, Name = "Porridge", IsSpicy = false}, new Dish {Id = 2, Name = "Chili", IsSpicy = true}, new Dish {Id = 3, Name = "Icecream", IsSpicy = false}};public static List<Dish> GetAllDishes(){return Dishes;}public static void AddDish(Dish dish){Dishes.Add(dish);}}}

Next, we define a new module Nancy

using Nancy;namespace NancyFxApplication{public class DishModule : NancyModule{public DishModule() : base("/Dish"){Post["/Add"] = ctx =>{var dish = new Dish {Id = Request.Form.Id, Name = Request.Form.Name, IsSpicy = Request.Form.IsSpicy};DishesRepository.AddDish(dish);return new Response { StatusCode = HttpStatusCode.Accepted };};Get["/GetAll"] = ctx => Response.AsJson(DishesRepository.GetAllDishes());}}}

Now, after starting the application and going to the Dish/GetAll router, we get the following result

[{"Id":1, "Name":"Porridge", "IsSpicy":false}, {"Id":2, "Name":"Chili", "IsSpicy":true}, {"Id":3, "Name":"Icecream", "IsSpicy":false}]

Now let’s use the feedler and make a Post request to the Dish/Add router
Creating the first application on NancyFX. Part four. Continuing work with modules
And again we make a Get query about the Dish/GetAll router and get

[{"Id":1, "Name":"Porridge", "IsSpicy":false}, {"Id":2, "Name":"Chili", "IsSpicy":true}, {"Id":3, "Name":"Icecream", "IsSpicy":false}, {"Id":1, "Name":"Borsh", "IsSpicy":false}]

As you can see everything works. However, x-www-form-urlencoded is only good for simple data formats. But it’s not good for complex data. For complex data formats, JSON and XML will help. Let’s modify the Dish class as follows :

public class Dish{public int Id { get; set; }public string Name { get; set; }public bool IsSpicy { get; set; }public List<Ingridients> Ingridientses { get; set; }}

Let’s add a class to our project :

public class Ingridients{public int Id { get; set; }public string Name { get;set; }}

Nancy includes two deserializers for two families of content types. For JSON("application/json", "text/json", "application/vnd…+json") and XML("application/xml", "text/xml", "application/vnc…+xml"). Nancy can also be extended to handle other types of content, such as Protocol Buffers, CSV, etc. You can even write your own deserializer for your content type.
And so let’s modify our module so that it can receive data in JSON format

using Nancy;using Nancy.ModelBinding;namespace NancyFxApplication{public class DishModule : NancyModule{public DishModule() : base("/Dish"){Post["/Add"] = ctx =>{var dish = this.Bind<Dish> ();DishesRepository.AddDish(dish);return new Response { StatusCode = HttpStatusCode.Accepted };};Get["/GetAll"] = ctx => Response.AsJson(DishesRepository.GetAllDishes());}}}

The Bind method is an action method found in the Nancy.ModelBinding namespace. It allows you to map your JSON to your business object. Now if you run your application and run the following POST request in the feedler to root "/Add"
Creating the first application on NancyFX. Part four. Continuing work with modules
And then we execute Dish/GetAl and get

[{"Id":1, "Name":"Porridge", "IsSpicy":false, "Ingridientses":null}, {"Id":2, "Name":"Chili", "IsSpicy":true, "Ingridientses":null}, {"Id":3, "Name":"Icecream", "IsSpicy":false, "Ingridientses":null}, {"Id":12, "Name":"Salad", "IsSpicy":false, "Ingridientses":[{"Id":1, "Name":"Salad"}]}]

As you can see our model is accepted by the application and written to our fake repository.
Finally we want to mention about XML format serialization. To use Bind, you have to label business object properties with attributes, just like we do it with normal XML serialization. It’s guaranteed that object will be distributed correctly.
In the next article we will take a look at the Super Simple View Engine. I look forward to your Comments and questions.

You may also like