Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate interface from implementation in nancy2.0?

Tags:

c#

nancy

As you can see at this link, it was possible to separate the rooting and the implementation of the old nancy1.X modules. Now that the way of defining these routes have changed, I would like to know how to code the same separation logic.

For clarity, the old way to define a Get route was:

Get["/{category}"] = parameters => "My category is " + parameters.category;

and the new way is:

Get("/{category}", args => "My category is " + args.category);

The old way to separate interface from implementation was:

Get["/favoriteNumber/{value:int}"] = FavoriteNumber;

private dynamic FavoriteNumber (dynamic parameters)
{
    return "So your favorite number is " + parameters.value + "?";
}

and the new way is:

???

like image 207
LoukMouk Avatar asked Nov 06 '22 16:11

LoukMouk


1 Answers

After experimenting with endpoint definitions, you may use either a Func<dynamic,object> type specification (#0 below) or a wrapper for a method (#1). That's because there are several overloads of Get, so compiler requires these hints to choose proper one. Alternatively if method returns task it can be specified without wrapping (##2,3), e.g. async methods always return task:

    Get("/favoriteNumber0/{value:int}", (Func<dynamic, object>)FavoriteNumberObject);
    Get("/favoriteNumber1/{value:int}", arg => FavoriteNumber(arg));
    Get("/favoriteNumber2/{value:int}", FavoriteNumberTask);
    Get("/favoriteNumber3/{value:int}", FavoriteNumberTaskCt);

...

private object FavoriteNumberObject(dynamic parameters)
{
    return "So your favorite number is " + parameters.value + "?";
}

private string FavoriteNumber(dynamic parameters)
{
    return "So your favorite number is " + parameters.value + "?";
}

private Task<string> FavoriteNumberTask(dynamic parameters)
{
    return Task.FromResult("So your favorite number is " + parameters["value"] + "?");
}

private Task<string> FavoriteNumberTaskCt(dynamic parameters, CancellationToken ctx)
{
    return Task.FromResult("So your favorite number is " + parameters["value"] + "?");
}
like image 61
Renat Avatar answered Nov 14 '22 14:11

Renat