C# 使用 NancyFx 的好处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11425095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Benefits of using NancyFx?
提问by Jaggu
There is yet another framework for making HTTP calls called NancyFx. My question is what are the benefits of using it. I had quick look at the documentation:
还有另一个框架用于进行 HTTP 调用,称为 NancyFx。我的问题是使用它有什么好处。我快速查看了文档:
https://github.com/NancyFx/Nancy/wiki/Documentation
https://github.com/NancyFx/Nancy/wiki/Documentation
and it looks like there is no outstanding feature due to which I would like to use this. What are the benefits of using it over WebHttp?
并且看起来没有突出的功能,因此我想使用它。在 WebHttp 上使用它有什么好处?
P.S:I keep reading about some strange phrase that keep repeating again and again "super-duper-happy-path". Is there anything apart from this "super-duper-happy-path"? Any real features implemented?
PS:我一直在读一些奇怪的短语,这些短语一遍又一遍地重复“super-duper-happy-path”。除了这条“超级快乐之路”之外还有什么吗?实现了任何真正的功能吗?
采纳答案by Glenn Ferrie
It appears that it offers a different approach to defining "routes" (in the MVC sense) using lambdas to identify relative paths, arguments, and the implementation of the response.
它似乎提供了一种不同的方法来定义“路由”(在 MVC 意义上),使用 lambda 来识别相对路径、参数和响应的实现。
Ultimately, the framework's key benefit is its expressiveness. In ASP.NET MVC the RouteTable is in the global.asax and the implementation is in the Control. It appears that in NancyFx, this is the pattern that prevails:
最终,该框架的主要优势在于其表现力。在 ASP.NET MVC 中,RouteTable 在 global.asax 中,而实现在 Control 中。似乎在 NancyFx 中,这是普遍存在的模式:
Action["/path"] = args => { return your_implementation_here; }
Example implementation:
示例实现:
Get["/products"] = id => { return GetRepository().Products.Single( q => q.Id == id); };
Explanation: An HTTP Get to the relative endpoint '/products' with an argument of 'Id' will return a single product from the repository where the Id argument matches the product's Id.
说明:以“Id”为参数对相对端点“/products”的 HTTP Get 将从存储库中返回单个产品,其中 Id 参数与产品的 Id 匹配。
Expressive and Concise.
表现力和简洁。
回答by superjos
Disclaimer: I'm not a supporter of NancyFx :)
I'm in the process of evaluating if I should go with NancyFx or with ASP.NET Web API for the REST part of a project.
免责声明:我不是 NancyFx 的支持者 :)
我正在评估是否应该使用 NancyFx 或 ASP.NET Web API 来作为项目的 REST 部分。
Apart from simplicity and expressiveness (which do have a value on their own, I think) already mentioned by GlennFerrieLive, I think I've grasped another couple of nice bits:
除了 GlennFerrieLive 已经提到的简单性和表现力(我认为它们本身就具有价值)之外,我想我还掌握了另外几个不错的地方:
It's easy to perform operations beforeand afterany API request processing, in a kind of an Aspect Orientedway, so to say.
By default the framework takes care of the accepted returned type, so it will appropriately convert output in JSON, XML, ...
Lambdas implementing requests do not return actual filleddata, but still in the form of a query. After that it's still possible to easily add filtering, sorting, and other operations before actually executing the query, hitting the DB, and returning the actual data.
They have somehow wrapped the HttpRequest and made available to the developer an equivalent of that, with the difference that this new object is injected and you can of course substitute it with a mock ... So easier and cleaner testing.
可以这么说,在任何 API 请求处理之前和之后执行操作都很容易,以一种面向方面的方式。
默认情况下,框架会处理接受的返回类型,因此它会适当地将输出转换为 JSON、XML、...
实现请求的 Lambda 不会返回实际填充的数据,但仍以查询的形式返回。之后,在实际执行查询、命中数据库和返回实际数据之前,仍然可以轻松添加过滤、排序和其他操作。
他们以某种方式包装了 HttpRequest 并提供给开发人员一个等价物,不同的是这个新对象被注入,你当然可以用模拟代替它......所以更容易和更清晰的测试。
Maybe some of those (all?) are already available in ASP.NET Web API and with the same ease, I don't know for sure.
HTH
也许其中一些(全部?)已经在 ASP.NET Web API 中可用,并且同样简单,我不确定。
HTH

