Dynamic proxy client generation for Nancy using Nancy.Rest.Module.
A server using Nancy & Nancy.Rest.Module.
It is recommended you read the Nancy.Rest.Module documentation to understand how the server mount the services before continuing reading.
- Add Nancy.Rest.Client and Nancy.Rest.Annotations to your client project.
Or
- Add the Nuget package Nancy.Rest.Client
Add your server models and interface with the method signatures to use.
####Your Server signatures:
namespaceNancy.Rest.ExampleServer{[RestBasePath("/api")]publicinterfaceIExample{[Rest("Person",Verbs.Get)]List<Person>GetAllPersons();[Rest("Person/{personid}",Verbs.Get)]PersonGetPerson(intpersonid);[Rest("Person",Verbs.Post)]boolSavePerson(Personperson);[Rest("Person/{personid}",Verbs.Delete)]boolDeletePerson(intpersonid);}}####Your Server Models
namespaceNancy.Rest.ExampleServer{publicclassPerson{publicstringFirstName{get;set;}publicstringLastName{get;set;}[Level(1)]publicboolIsProgrammer{get;set;}[Tags("Attr")]publicList<string>Attributes{get;set;}}}namespaceNancy.Rest.ExampleClient{publicclassExample{publicvoidRun(){IExampleserver=ClientFactory.Create<IExample>("http://yourserver/api"); `
List<Person>persons=server.GetPersons();}}}##Advanced Usage
###Transversal Filtering
Nancy.Rest.Client includes this interface.
usingSystem.Collections.Generic;namespaceNancy.Rest.Client.Interfaces{publicinterfaceIFilter<T>{TFilterWithLevel(intlevel);TFilterWithTags(IEnumerable<string>tags);TFilterWithLevelAndTags(intlevel,IEnumerable<string>tags);}}Create a new interface in your client that includes, both, IFilter interface and your server interface.
namespaceNancy.Rest.ExampleClient{publicinterfaceIExampleWithFiltering:IExample,IFilter<IExample>{}}Then you can use the transversal filtering capabilities of the server like this:
namespaceNancy.Rest.ExampleClient{publicclassExample{publicvoidRun(){IExampleWithFilteringserver=ClientFactory.Create<IExampleWithFiltering>("http://yourserver/api"); `
//Per example, you can ask the server to not serialize any property with level bigger than the number provided.//Here we can filter the IsProgrammer property using levels.List<Person>persons=server.FilterWithLevel(0).GetPersons();//Or remove the Attributes property using Tags. List<Person>persons=server.FilterWithTags(newstring[]{"Attr"}).GetPersons();}}}###Controlable deserialization
Imagine you have your poco models from the server, but you need to add some properties, methods or INotifyPropertyChanged to that objects, you create a child class from the model, and add all those things. The problem is, the deserialzer will deserialize your poco model, so you have to create a new child class, and copy all properties to your child. Nancy.Rest.Client have the capability of deserializing the objects to child objects.
####Client model
namespaceNancy.Rest.ExampleClient{publicclassClientPerson:Person{publicboolIsSuperman{get;set;}publicvoidDoSomeNastyThings(){//Kidding}}}####Example
namespaceNancy.Rest.ExampleClient{publicclassExample{publicvoidRun(){Dictionary<Type,Type>mappings=newDictionary<Type,Type>();//Here we add the mapping, we want every person to be deserialized as ClientPersonmappings.Add(typeof(Person),typeof(ClientPerson));IExampleserver=ClientFactory.Create<IExample>("http://yourserver/api",mappings); `
//Here is your list of client personsList<ClientPerson>persons=server.GetPersons().Cast<ClientPerson>.ToList();}}}##WARNING
THIS IS AN BETA VERSION, so far it works on my machine ;)
- Fill the blanks :P
1.4.3-Beta: Removed bugs, published nugets. 1.4.3-Alpha: First Release
- Maximo Piva - maxpiva
This project is licensed under the MIT License - see the LICENSE.md file for details