Moq、Ninject、Fluent NHibernate 风格的库?

发布于 2024-10-04 22:31:02 字数 1007 浏览 3 评论 0原文

这是一个头脑风暴的问题。任何意见将不胜感激。

最近我观察到一些具有相似模式的源框架。

起订量

mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
    .Returns(true)
    .AtMostOnce();

Ninject

Bind<IService>().To<RedImpl>().WhenMemberHas<RedAttribute>();

Fluent NHibernate

   HasManyToMany(x => x.Products)
     .Cascade.All()
     .Table("StoreProduct");

我注意到它们都与某种配置相关。与普通的属性设置配置相比,使用起来更加方便。

我可以说我喜欢这种风格,但不知道到底为什么。

谁能告诉我它有什么优点以及这种风格什么时候有用?另外,这种风格有名字吗?这是如何在幕后实现的?如果有关于如何编写 Moq 风格的教程,我们将不胜感激。

更新

感谢您的所有帮助。现在我知道这种风格称为Fluent Interface,是否有关于如何创建示例 Fluent 界面的教程?

This is a brain storming question. Any comments would be appreciated.

Recently I've observed a few source frameworks with a pattern similar to each other.

Moq

mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
    .Returns(true)
    .AtMostOnce();

Ninject

Bind<IService>().To<RedImpl>().WhenMemberHas<RedAttribute>();

and Fluent NHibernate

   HasManyToMany(x => x.Products)
     .Cascade.All()
     .Table("StoreProduct");

I've noticed that they are all related to some sort of configuration. And it is more convenient to use compared to normal property-setting configuration.

I can tell I like this style, but can't figure out exactly why.

Can anyone tell me what advantage it has and when this style would be useful? Also, Is there a name for such a style? How is this implemented under the hood? If there is a tutorial on how to write Moq-styled, that would be much appreciated.

Update

Thanks for all the help. Now that I know this style is called Fluent Interface, is there any tutorial on how to create a sample Fluent interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅冇旳回忆 2024-10-11 22:31:02

该模式称为 Fluent Interface,在 http://www.martinfowler.com 中有更详细的描述/bliki/FluentInterface.html

通常,当完整的领域特定语言对于任务来说是过度杀伤力时,最好使用它。基本上,您返回在编写的每个方法中操作的对象,并尝试使方法名称与意图紧密匹配。

虽然不一定有联系,但您可能希望比较构建器模式以获取构建复杂对象的替代方法,因为一些激励场景是相似的。

The pattern is called Fluent Interface, and is described in greater detail at http://www.martinfowler.com/bliki/FluentInterface.html

It's generally best used when a full-on domain specific language for a task is overkill. Basically, you return the object that you are manipulating in each of the methods you write, and try to make the method names closely match the intent.

Though not necessarily connected, you may wish to compare the Builder pattern for an alternate approach to building complex objects, as some of the motivating scenarios are similar.

爱冒险 2024-10-11 22:31:02

它被称为 Fluent 界面

这种风格的主要优点是您可以轻松阅读和编写用于构建复杂对象的代码。

It is called a Fluent interface

The main advantage of this style is that you can easily read and write the code for building complex objects.

笑梦风尘 2024-10-11 22:31:02

它们是流畅接口,通常用于为某些任务(例如配置)创建小型 DSL。它们比文本具有优势,因为它们可以由编译器进行类型检查。

例如,许多 IoC 容器可以使用 xml 进行配置 - 这具有一些优点,例如易于部署配置修改,但在进行某些重构(例如类型重命名)时会很脆弱。

They are fluent interfaces and are generally used to create small DSLs for some task (such as configuration). They have advantages over text since they can be type-checked by the compiler.

As an example, many IoC containers can be configured using xml - this has some advantages such as easy deployment of configuration modifications, but is brittle in the event of some refactorings such as type renames.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文