Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have created a simple MVC Java application that adds records through data forms to a database.

My app collects data, it also validates it and stores it. This is because the data is being sourced online from different users. the data is mostly numeric in nature.

Now on the numeric data being stored into database (SQL server), I want my app to perform computations and display the results. The user is not interested in how computations are done so they must be encapsulated. The user must only be able to view the simple computed data (for example, A column data minus B Column data divided by C column data). I know how to write stored procedures for same but I want a three-tier app.

I want the data that I put into the database as a record, worked upon by performing calculations on it. The original data should remain unaffected, while the new data, post-calculations, must be stored as a new entity record into the database.

Where should I write the code for this background calculation? As it is the rules and business logic, should I put it in new JavaBeans files?

share|improve this question

3 Answers 3

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers.

As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

In the model, we may easily create helper/service classes where the application business rules or calculations can be validated.

A conceptual summary

  • The controller is for application logic. The logic which is specific to how your application wants to interact with the "domain of knowledge" it belongs.

  • The model is for logic that is independent of the application. This logic should be valid in all possible applications of the "domain of knowledge" it belongs.

  • Thus, it is logical to place all business rules in the model.

share|improve this answer
2  
nice clear and concise answer.. –  hanzolo May 15 '13 at 22:46
    
@hanzolo - thanks for a feedback :) –  Yusubov May 16 '13 at 14:01

Putting the business logic inside the model might sound the best way to go. The controller receives a call from the remote web app. The controller on the MVC web service takes the call and redirects the execution to a method in BL. Now, Business Logic can be contained in the 'Model', but can also be positioned in some other folder, say, 'Business Logic'. So there's no hard-and-fast rule on where the business logic is going to be.

I've been using a web service built on MVC 3.0 and the container of business logic is the MVC MODEL.

share|improve this answer

As always, it depends on the complexity of the project.

In trivial applications, where the domain model complexity is relatively small, you can put the logic in the models and call it a day.

However, for non trivial applications with complex models and lots of business rules, it's better to separate things a little bit more.

If you put the business logic that involves more than one model in a model, you are introducing a tight coupling between those models. As applications continues to grow, these models tends to turn into god models, knowing too much. And this will quickly turns into a big mess that is hard to test and maintain. So in that case, it is beneficial to put the logic in a separate layer.

When deciding about abstraction, always take your app complexity and purposes into account, and avoid over-engineering. For trivial/small applications, introducing more layers than it needs increases complexity instead of reducing it.

Robert Martin(Uncle Bob) has a good blog post on this subject: The Clean Architecture.

share|improve this answer
    
the question was specific for MVC. business logic should always be in the model. The controller is just an adapter. –  jgauffin Sep 20 '12 at 5:49
4  
MVC is one of the most overloaded terms in the industry. I don't want to get in the quirks of this term, as it warrants a book. Just, using MVC doesn't imply that you must put every logic in the models. –  Hakan Deryal Sep 20 '12 at 5:58
1  
From the definition by Steve Burbeck (Smalltalk team): The controller interprets the mouse and keyboard inputs from the user, commanding the model and/or the view to change as appropriate. That's an adapter definition. –  jgauffin Sep 20 '12 at 6:11
1  
If you put all the logic in the model, you will end up with thousands of lines of unmaintainable mess. Been there. It's not a sin to have utility classes and a service layer. –  syrion Sep 20 '12 at 10:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.