Rails 中的业务逻辑在哪里?

发布于 2024-10-09 19:34:48 字数 177 浏览 0 评论 0原文

我是一名 ASP.NET MVC 开发人员,刚刚开始我的第一个 Rails 大型项目,但是我很困惑将业务逻辑放在哪里?在 ASP.NET 上,我创建了一个包含处理业务逻辑的服务(域驱动设计)的库,我听说 Rails 使用胖模型瘦控制器的概念,但我在 ASP.NET 中有一些项目,它们将所有逻辑添加到控制器会造成很大的混乱,还有其他方法吗?

I'm an ASP.NET MVC developer just starting with my first big project on rails however Im confused as where to put your business logic? on ASP.NET I create a library which contains services(Domain driven design) which handle business logic, I have heard that rails uses a concept of fat model skinny controller but I have some projects in ASP.NET which adding all the logic to the controller would create a big mess, is there any other way?

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

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

发布评论

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

评论(4

酒绊 2024-10-16 19:34:48

遵循 FatModels 和 SkinnyControllers 的概念。你的模型应该知道他们的行为方式以及他们应该做什么。

当你的模型变得太胖时,将它们提取到可重复使用的模块中,并将它们包含在你的模块中。

您可以使用 RSpec(或 test/unit 或 Shoulda)轻松测试模型的行为。然后您可以使用 Cucumber 测试应用程序的行为是否正确。

Go with the concept of FatModels and SkinnyControllers. Your models should know how they behave and what they should do.

When your models get too fat, extract them out into re-usuable modules and include them in your module.

You can easily test behavior of models using RSpec (or test/unit or shoulda). Then you can test that the application behaves correctly using Cucumber.

再可℃爱ぅ一点好了 2024-10-16 19:34:48

“业务逻辑”或者有些人可能称之为“域逻辑”不属于 Rails 和/或 .NET MVC 项目附近的任何地方。 Rails 和 MVC 应该取决于您的域,而不是相反。我建议阅读 Jeffery Palermo 的《洋葱建筑》或观看 Robert Martin 的《建筑失落的岁月》。 (我认为无论如何,这就是谈话)。可能有比这更多的资源,但稍后您会感谢自己将 Rails 和 .NET MVC 视为第 3 方框架,而不是应用程序的主要框架。

"Business Logic" or some might call it "Domain Logic" doesn't belong anywhere near Rails and/or your .NET MVC project. Rails and MVC should depend on your Domain not the other way around. I would recommend reading up on the Onion Architecture from Jeffery Palermo or watch "Architecture the Lost Years" by Robert Martin. (I think that's that talk anyway). There are probably more resources than that, but you'll thank yourself later for treating both Rails and .NET MVC like the 3rd party frameworks they are, and not the main house of your application.

半透明的墙 2024-10-16 19:34:48

我认为这篇博客文章很好地概述了将领域驱动设计与 Rails 框架相结合的策略:https://www.smashingboxes.com/post/domain-logic-in-rails

TL;DR

将经典 Rails 模型重构到存储库中,并在控制器中使用外观层与你的域模型。

我自己也在努力解决这个问题,尽管胖控制器模式似乎很盛行,但软件中的任何“胖”似乎都是一种气味,违反了单一责任。

I think this blog article provides a good overview of a strategy of incorporating domain driven design with in the rails framework: https://www.smashingboxes.com/post/domain-logic-in-rails

TL;DR

Refactor your classic rails models into repositories, and use a facade layer in the controllers to interact with your domain model.

I'm struggling with this a little my self, and as much as the Fat Controller pattern seems to prevail, anything "fat" in software seems to be a smell, violating single responsibility.

只为一人 2024-10-16 19:34:48

您可以将业务逻辑放在任何您想要的地方(甚至在视图中!尽管这是一个坏主意)。

我想说,如果逻辑与现实世界的对象相关,那么就把它放在模型上。否则,请使用控制器。但如何为您的应用程序执行此操作取决于您。模型用于建模事物,控制器用于控制事物。

You can put business logic anywhere you want (even in views! though that's a bad idea).

I'd say if the logic is tied to a real-world object, then put it on the model. Otherwise, use the controller. But it's up to you to determine how to do it for your app. Models are for modeling things, and Controllers are for controlling things.

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