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?
我是一个ASP.NET MVC开发人员,刚开始我的第一个关于rails的大项目,但我很困惑,因为在哪里放置你的业务逻辑?在ASP.NET上我创建了一个包含处理业务逻辑的服务(域驱动设计)的库,我听说rails使用了胖模型瘦控制器的概念,但我在ASP.NET中有一些项目,它们将所有逻辑添加到控制器会造成很大的混乱,还有其他方法吗?
4 个解决方案
#1
13
Go with the concept of FatModels and SkinnyControllers. Your models should know how they behave and what they should do.
使用FatModels和SkinnyControllers的概念。您的模型应该知道它们的行为方式和应该做的事情。
When your models get too fat, extract them out into re-usuable modules and include them in your module.
当您的模型太胖时,将它们提取到可重复使用的模块中并将它们包含在您的模块中。
- Example of taking a fat controller (with logic) and moving to a model
- Example of taking code from the views and moving into the model
采用胖控制器(带逻辑)并移动到模型的示例
从视图中获取代码并移入模型的示例
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.
您可以使用RSpec(或test / unit或shoulda)轻松测试模型的行为。然后,您可以使用Cucumber测试应用程序的行为是否正确。
#2
7
"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.
“业务逻辑”或某些人可能称之为“域逻辑”并不属于Rails和/或.NET MVC项目附近的任何地方。 Rails和MVC应该依赖于你的域,而不是相反。我建议您阅读杰弗里·巴勒莫的洋葱建筑,或者观看罗伯特·马丁的“建筑失落的岁月”。 (无论如何,我认为那是那个话题)。可能有更多的资源,但你会感谢以后将Rails和.NET MVC视为第三方框架,而不是应用程序的主要内容。
#3
3
I think this blog article provides a good overview of a strategy of incorporating domain driven design with in the rails framework: http://www.smashingboxes.com/domain-logic-in-rails/
我认为这篇博客文章很好地概述了将域驱动设计纳入rails框架的策略:http://www.smashingboxes.com/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.
将经典rails模型重构为存储库,并使用控制器中的Facade层与域模型进行交互。
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.
我正在努力解决这个问题,尽管胖控制器模式似乎占上风,软件中的任何“胖”似乎都是一种气味,违反了单一的责任。
#4
0
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.
我会说逻辑是否与现实世界的对象联系在一起,然后将其放在模型上。否则,请使用控制器。但是由您决定如何为您的应用程序执行此操作。模型用于建模事物,控制器用于控制事物。
#1
13
Go with the concept of FatModels and SkinnyControllers. Your models should know how they behave and what they should do.
使用FatModels和SkinnyControllers的概念。您的模型应该知道它们的行为方式和应该做的事情。
When your models get too fat, extract them out into re-usuable modules and include them in your module.
当您的模型太胖时,将它们提取到可重复使用的模块中并将它们包含在您的模块中。
- Example of taking a fat controller (with logic) and moving to a model
- Example of taking code from the views and moving into the model
采用胖控制器(带逻辑)并移动到模型的示例
从视图中获取代码并移入模型的示例
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.
您可以使用RSpec(或test / unit或shoulda)轻松测试模型的行为。然后,您可以使用Cucumber测试应用程序的行为是否正确。
#2
7
"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.
“业务逻辑”或某些人可能称之为“域逻辑”并不属于Rails和/或.NET MVC项目附近的任何地方。 Rails和MVC应该依赖于你的域,而不是相反。我建议您阅读杰弗里·巴勒莫的洋葱建筑,或者观看罗伯特·马丁的“建筑失落的岁月”。 (无论如何,我认为那是那个话题)。可能有更多的资源,但你会感谢以后将Rails和.NET MVC视为第三方框架,而不是应用程序的主要内容。
#3
3
I think this blog article provides a good overview of a strategy of incorporating domain driven design with in the rails framework: http://www.smashingboxes.com/domain-logic-in-rails/
我认为这篇博客文章很好地概述了将域驱动设计纳入rails框架的策略:http://www.smashingboxes.com/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.
将经典rails模型重构为存储库,并使用控制器中的Facade层与域模型进行交互。
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.
我正在努力解决这个问题,尽管胖控制器模式似乎占上风,软件中的任何“胖”似乎都是一种气味,违反了单一的责任。
#4
0
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.
我会说逻辑是否与现实世界的对象联系在一起,然后将其放在模型上。否则,请使用控制器。但是由您决定如何为您的应用程序执行此操作。模型用于建模事物,控制器用于控制事物。