We create the first application on NancyFX. Part Four We continue to work with modules

    In the previous article, We create the first application on NancyFX. Part three. Nancy Modules. We met with Nancy modules. In this article, we will continue to study them.


    As I mentioned in a previous article, Nancy uses the Request.Form request property. Form is a DynamicDictionary whose values ​​are taken from the request body. In order for Nancy to correctly parse the values ​​transmitted in the request body, you must correctly set the Content-Type: application / x-www-form-urlencoded. If the Content-Type was set incorrectly, do not be surprised if you get nulls as values. Nancy parses only x-www-form-urlencoded data automatically. And so let's try to create a module that processes Post request. First, 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 Dishes = new List
                    {
                        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 GetAllDishes()
            {
                return Dishes;
            }
            public static void AddDish(Dish dish)
            {
                Dishes.Add(dish);
            }
        }
    }
    


    next we define the new Nancy module

    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 navigating the Dish / GetAll route, we get the following result
    [
        {"Id":1,"Name":"Porridge","IsSpicy":false},
        {"Id":2,"Name":"Chili","IsSpicy":true},
        {"Id":3,"Name":"Icecream","IsSpicy":false}
    ]
    


    Now we’ll use the feeddler and make a Post request to the Dish / Add route.



    And again, we will get a request for the Dish / GetAll route 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 is not suitable for complex data. To transfer 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 Ingridientses { get; set; }
        }
    


    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 expanded to work with 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();
                        DishesRepository.AddDish(dish);
                        return new Response { StatusCode = HttpStatusCode.Accepted };
                    };
                Get["/GetAll"] = ctx => Response.AsJson(DishesRepository.GetAllDishes());
            }
        }
    }
    


    Bind method - an extension method that is located in the Nancy.ModelBinding namespace. It allows you to map your JSON to your business object. Now if you run the application and execute the following POST request to the "/ Add" route in the feeddler



    and then execute Dish / GetAl we will 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 recorded in our fake repository.
    In conclusion, I would like to make a reservation about XML deserialization. To use the Bind properties of a business object, it is necessary to mark with attributes as we do with ordinary XML serialization. In this case, we will have a guarantee that the object will be disposed of correctly.

    In the next article, we will discuss with you the Super Simple View Engine graphics engine. Waiting for your comments and questions.

    Also popular now: