I am trying Nancy with F#. It pretty cool but I couldn't get it few things in F#. First I ll write down both code.
Here is code of C#
public class HomeModule : NancyModule
{
public HomeModule()
{
this.Get["/"] = (parameters) => View["Index.html"];
}
}
public class AboutModule : NancyModule
{
public AboutModule():base("/about")
{
this.Get["/"] = (parameters) => "Hello About";
}
}
Here is code of F#
type HomeModule() =
inherit NancyModule()
let get = ``base``.Get
let view = ``base``.View
do get.["/"] <- fun parameters -> view.["Index.html"] :> obj
type AboutModule() =
inherit NancyModule("/about")
let get = ``base``.Get
do get.["/"] <- fun parameters -> "Hello About" :> obj
Few things I already get, I am putting here just in case someone read this question as reference then s/he will get it.
Do parameter is used as force constructor. It should define after all private or let member of type and before any member of type. As per MSDN.
base
is equivalent of base in C#. One can use base also but when I did formatting it changed..
So, why base
is preferable instead of base.
When I tried to called base.View.["Index.html"] within function call it force me to add private member, why? As in C# it just allowed.
Now here is the thing that I don't get.
In C# I can directly write Get["/"] but in F# I need to write Get.["/"]. Means I have to call another function instead of property constructor.
Even I don't know how "this" use in constructor internally worked in C#, so if someone explain that, it will great... and if there is any equivalent in F#.
I am also putting nancy code of route builder for reference
public class RouteBuilder : IHideObjectMembers
{
public RouteBuilder(string method, NancyModule parentModule);
public Func<dynamic, dynamic> this[string path] { set; }
public Func<dynamic, dynamic> this[string path, Func<NancyContext, bool> condition] { set; }
protected void AddRoute(string path, Func<NancyContext, bool> condition, Func<dynamic, dynamic> value);
}
Please let me know if any further details required.