Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does business logic go in rails?

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?

like image 361
ryudice Avatar asked Dec 29 '10 23:12

ryudice


People also ask

Where should business logic be in MVC?

A1: Business Logic goes to Model part in MVC . Role of Model is to contain data and business logic. Controller on the other hand is responsible to receive user input and decide what to do.

What kind of logic doesn't belong to the model in Rails?

That logic belongs in app/models where our models are stored. A model is the M in MVC. Models contain business logic that interacts with the database. Business logic that doesn't interact with a database doesn't belong in a model.

What is MVC business logic?

MVC enables the application to be extensible and modular by separating the application into three parts: the business logic part, which implements data retrieval and manipulation. the user interface part, which is what the application users see. the controller part, which routes requests to the proper objects.

Should model classes contain logic?

Models should contain logic that deals with manipulating data. For example, say we have a post model, the model should contain methods that get the comments of a particular post.


2 Answers

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.

  • 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.

like image 174
Jesse Wolgamott Avatar answered Nov 11 '22 04:11

Jesse Wolgamott


"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.

like image 32
rmontgomery429 Avatar answered Nov 11 '22 05:11

rmontgomery429