Should I have logic in the model class or other classes

I just want to have other opinions about this, which I discussed in my head, for example, I have a class user_controller and a class user

class User attr_accessor :name, :username end class UserController // do something about anything about users end 

The question is should I have logic in my User class, so this will be

 user = User.new user.do_something(user1) or it should be user_controller = UserController.new user_controller.do_something(user1, user2)
user = User.new user.do_something(user1) or it should be user_controller = UserController.new user_controller.do_something(user1, user2) 

I'm not sure which one is the best design, I personally really like the first one, so for example, he would read how

 john = User.new john.accept_friend(jane) instead of user_controller = UserController.new user_controller.accept_friend(john, jane)
john = User.new john.accept_friend(jane) instead of user_controller = UserController.new user_controller.accept_friend(john, jane) 

What are the pros and cons of these templates? This is not just typical of Ruby, it is because it’s easier for me to type in ruby.

Edit: A really good transformation is going on, but I really like it more here from people. Thanks to all.

+4
source share
4 answers

Yes, you must keep the logic in your model! That is, if you are doing the actual object-oriented programming (and it looks like you are doing it). To quote Wikipedia :

Object-oriented programming (OOP) is a programming paradigm that uses "objects" - data structures consisting of data fields and methods together with their interaction - for designing applications and program computers.

This is especially true if you are trying to create a domain-based design (which implies your tags). DDD is everything to express your domain with objects.

Martin Fowler says that logic outside your model is an anti-pattern.

+7
source

Most people will say that you shouldn't keep logic in your model classes. Exceptions may include:

  • helper functions for accessing the contained collection ( addToList(Object o) , getFromList(int index) , etc. etc.)
  • Standard object and similar overrides ( equals , hashCode , toString , clone , compareTo , etc.)
  • Pre-processing / post-processing of data (for example, fixing strings in upper case or the like)

Since people will not expect logic in the models, you should probably avoid this. This will confuse other developers who may have to watch and maintain your code in the future. In the end, that's why there are patterns - to help other developers recognize and maintain your code.

+1
source

I believe that the first one is better, you have a model and a class that has all the information necessary to work with this model, and for this model some other information may be needed to perform some operations.

try to learn more about the Information Expert.

0
source

In such scenarios, a trade-off should be considered. It’s good to add accept_friend to the user class if you are sure that the user class will not grow in the future.

On the other hand, it is preferable to move accept_friend to service classes such as UserController in the following scenarios.

  • To avoid increasing the size of the custom class. Such logic can be moved to these subclasses (Usercontroller), which simplifies the classes.

  • To ensure accessibility. Tomorrow, if there is a class called superuser that also needs the accept_friend function, then the UserController class can be rewritten as

user_controller = UserController.new

user_controller.accept_friend (Superuser1, Superuser2)

0
source

Source: https://habr.com/ru/post/1403599/


All Articles