The magic of Dynamic in Nancy Fx
-
Upload
michele-capra -
Category
Technology
-
view
831 -
download
4
description
Transcript of The magic of Dynamic in Nancy Fx
![Page 1: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/1.jpg)
C# Unleashed
Michele Capra a.k.a. @piccoloaiutanteSoftware Contractor @ [email protected]
The magic of Dynamic in Nancy FX
@dnlombardia#cunleashed
![Page 2: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/2.jpg)
Who I am Software contractor :
- C# Asp.net mvc, Wpf, WinRT, Windows Phone..
- Python, Django
![Page 3: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/3.jpg)
What are we talking about today
• Dynamic and DLR
• Introduction to Nancy FX
• Some dynamic features of Nancy FX
![Page 4: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/4.jpg)
Dynamic Language Runtime (DLR)• .NET Framework 4 • It provides the infrastructure that supports the
dynamic type in C#, and also the implementation of dynamic programming languages.
• System.Dynamic:• DynamicObject
• IronPython and IronRuby.
![Page 5: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/5.jpg)
Dynamic Language Runtime (DLR)
![Page 6: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/6.jpg)
The dynamic keyword
• New keyword introduced in VS2010.
• Behaves like type object in most circumstances.
• Objects of type dynamic are neither resolved or type checked by the compiler.
![Page 7: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/7.jpg)
What is Nancy FX
Nancy is a lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono.
The goal of the framework is to stay out of the way as much as possible and provide a super-duper-happy-path to all interactions.
![Page 8: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/8.jpg)
Nancy FX philosophy• Extensive use of defaults and conventions
• Designed to handle DELETE, GET, HEAD, OPTIONS, POST, PUT and PATCH request.
• Provides a simple, elegant, Domain Specific Language (DSL) for returning a response
• Built with .NET Framework Client Profile, can be hosted with Asp.Net, WCF, Azure, Umbraco, Nginx on Ubuntu with Mono, self hosted…
![Page 9: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/9.jpg)
Nancy Fx – Basic Examplepublic class HelloModule: Nancy.NancyModule{ public HelloModule () { Get["/"] = _ => "Hello World!"; }}
![Page 10: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/10.jpg)
Nancy Fx – Basic Examplepublic class HelloModule: Nancy.NancyModule{ public HelloModule () { Get["/"] = _ => "Hello World!"; }}
![Page 11: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/11.jpg)
Sinatra (ruby)require 'sinatra'
get ’/' do "Hello World!"end
![Page 12: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/12.jpg)
Flask (python)from flask import Flaskapp = Flask(“HelloApp”)
@app.route("/”)def hello(): return "Hello World!"
![Page 13: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/13.jpg)
WebApi (controller)using System.Web.Http;
public class HelloWorldController : ApiController { public string Get() {
return "Hello World"; }
}
![Page 14: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/14.jpg)
WebApi (global.asax.cs)public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }
![Page 15: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/15.jpg)
Example 1 – Post & QueryString parampublic class HelloModule : Nancy.NancyModule{ public HelloModule() { Get[“/hello/{name}”] = p => “Hello “ + p.name; }}
![Page 16: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/16.jpg)
Example 1 – Post & QueryString parampublic class HelloModule : Nancy.NancyModule{ public HelloModule() { Get[“/hello/{name}”] = p => “Hello “ + p.name; }}
DynamicDictionary
![Page 17: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/17.jpg)
Dynamic Dictionary from Nancy/// A dictionary that supports dynamic access.
public class DynamicDictionary : DynamicObject, IEquatable<DynamicDictionary>, IHideObjectMembers, IEnumerable<string>, IDictionary<string, object>{
…..}
![Page 18: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/18.jpg)
Dynamic Dictionary from Nancy dynamic user = new DynamicDictionary();
user[”Years"] = 5;
int years = user.Years;
![Page 19: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/19.jpg)
Example 1 – Post & QueryString parampublic class HelloModule : NancyModule{ public HelloModule() { Get[“/hello/{name}”] = p => “Hello “ + p.name; Post["/hello"] = p => “Hello “ + Request.Form.Name; }}
![Page 20: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/20.jpg)
Example 2- Lambda structurepublic class HelloModule : NancyModule{ public HelloModule() { Get[“/hello/{name}”] = p => “Hello “ + p.name; }}
![Page 21: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/21.jpg)
Example 2- Lambda structurepublic abstract class NancyModule : IHideObjectMembers{
….public RouteBuilder Get
{ get { return new RouteBuilder("GET", this); } }
….}
![Page 22: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/22.jpg)
Example 2- Lambda structure public class RouteBuilder : IHideObjectMembers { /// Defines a Nancy route for the specified
/// <value>A delegate that is used to invoke the route.</value> public Func<dynamic, dynamic> this[string path] { set { this.AddRoute(path, null, value); } }
…}
![Page 23: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/23.jpg)
Example 3 – Response type castpublic class DefaultRouteInvoker : IRouteInvoker { /// Invokes the route with the provided parameters. /// <returns>A Response instance.</returns> public dynamic Invoke(DynamicDictionary parameters) { return this.Action.Invoke(parameters); }}
![Page 24: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/24.jpg)
Example 3 – Response type castpublic class DefaultRouteInvoker : IRouteInvoker { private Response InvokeRouteWithStrategy(dynamic result, NancyContext context) { var isResponse =(CastResultToResponse(result) != null);
return (isResponse) ? ProcessAsRealResponse(result, context) : this.ProcessAsNegotiator(result, context); }}
![Page 25: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/25.jpg)
Example 3 – Response type castpublic class Response: IDisposable{ public string ContentType { get; set; } public Action<Stream> Contents { get; set; } public IDictionary<string, string> Headers { get; set; } public HttpStatusCode StatusCode { get; set; } public IList<INancyCookie> Cookies { get; private set; }}
Not Dynamic
![Page 26: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/26.jpg)
Example 3 – Response type castpublic class DefaultRouteInvoker : IRouteInvoker { private Response InvokeRouteWithStrategy(dynamic result, NancyContext context) { var isResponse = (CastResultToResponse(result) != null);
return (isResponse) ? ProcessAsRealResponse(result, context) : this.ProcessAsNegotiator(result, context); }}
![Page 27: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/27.jpg)
Example 3 – Response type cast private static Response CastResultToResponse(dynamic result) { try { return (Response)result; } catch (Exception e) { return null; } }
![Page 28: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/28.jpg)
Example 3 – Response type cast public static implicit operator Response(HttpStatusCode statusCode) { return new Response { StatusCode = statusCode }; }
public static implicit operator Response(int statusCode) { return new Response { StatusCode = (HttpStatusCode)statusCode }; } public static implicit operator Response(string contents){ return new Response { Contents = GetStringContents(contents) }; }
public static implicit operator Response(Action<Stream> streamFactory){ return new Response { Contents = streamFactory }; }
![Page 29: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/29.jpg)
Demo Time
![Page 30: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/30.jpg)
Example 4 – Model binding public class Customer { public int Id { get; set; }
public string Name { get; set; }
public DateTime RenewalDate { get; set; }}
![Page 31: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/31.jpg)
Example 4 – Model binding public class CustomersModule : NancyModule { public CustomersModule() : base("/customers") { Post["/"] = _ => { Customer model = this.Bind(); return 200; }; } }
![Page 32: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/32.jpg)
Example 4 – Model bindingpublic static dynamic Bind(this NancyModule module, BindingConfig configuration, params string[] blacklistedProperties){ return new DynamicModelBinderAdapter(
module.ModelBinderLocator, module.Context, null, configuration,
blacklistedProperties);}
![Page 33: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/33.jpg)
Example 4 – Model binding public class DynamicModelBinderAdapter : DynamicObject { … public DynamicModelBinderAdapter(IModelBinderLocator locator,…) { … this.locator = locator; … }
public override bool TryConvert(ConvertBinder binder, out object result) { var modelBinder = this.locator.GetBinderForType(binder.Type, this.context); if (modelBinder == null) { throw new ModelBindingException(binder.Type); } }}
Provides implementation for type conversion operations. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations that convert an object from one type to another.
![Page 34: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/34.jpg)
Example 4 – Model binding public class CustomersModule : NancyModule { public CustomersModule() : base("/customers") { Post["/"] = _ => { Customer model = this.Bind(); return 200; }; } }
![Page 35: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/35.jpg)
Recap
• DLR and dynamic keyword
• How dynamic features are used in Nancy
• Little demo on type conversion from dynamic
![Page 36: The magic of Dynamic in Nancy Fx](https://reader035.fdocuments.in/reader035/viewer/2022081519/555cf32ed8b42a08668b4d18/html5/thumbnails/36.jpg)
That’s allSpecial thanks to Christian Horsdal and The Nancy Fx team !
Nancy repos:https://github.com/NancyFx/Nancy
Twitter:Http://twitter.com/piccoloaiutante