Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I have logic in my model class or other classes

I just want to have other opinions about this one that I have been debating in my head, for example I have class user_controller, and class user


class User
   attr_accessor :name, :username   
end

class UserController
   // do something about anything about users
end


The question would be should I have logic in my User class so it would be


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 quite like the first one so for example it would read like


john = User.new
john.accept_friend(jane)

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


What are pros and cons of those patterns? This is not just specific to Ruby, it's because I thing ruby is easier in typing.

Edit: There is really good conversion going on, but I quite like to here more from people. Thanks everyone.

like image 812
toy Avatar asked Mar 27 '12 07:03

toy


People also ask

Should models have business 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. In Sails models, each model has static methods like find , findOne , update , etc.

Why do we need model class?

Model class represents the business side of the application, as In your case you have a UserModel class which is having information of user and the collection with roles of it, which the Data Model is specific to repository i.e. database table in your case, so Data Model normally represents the table of the database ...

Can model classes have methods?

A model should contain all logic related to the model (this is DSL) so, yes it can update itself each hour. and when u define auto properties, it's same as defining setters and getter, so of course you can add methods, and constructors as well.

What is class and OBJ?

A class is a user-defined type that describes what a certain type of object will look like. A class description consists of a declaration and a definition. Usually these pieces are split into separate files. An object is a single instance of a class. You can create many objects from the same class type.


2 Answers

Yes, you should keep logic in your model! That is, if you do actual object oriented programming (and it looks like you do). To quote Wikipedia:

Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs.

This is especially true if you're trying to do domain driven design (which your tags imply). DDD is all about expressing your domain with objects.

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

like image 87
Dennis Laumen Avatar answered Oct 01 '22 21:10

Dennis Laumen


Most people would say that you should not keep logic in your model-classes. Exceptions might include:

  • helper functions accessing a contained Collection (addToList(Object o), getFromList(int index), etc etc)
  • Standard Object and similar overrides (equals, hashCode, toString, clone, compareTo, etc)
  • Data pre/post processing (like fixing strings to uppercase or stuff like that)

Since people won't expect there to be logic in model classes, you should probably avoid it too. It will confuse other developers who might have to look at and maintain your code in the future. After all, that is why there are patterns - to help other developers recognize and maintain your code.

like image 42
pap Avatar answered Oct 01 '22 19:10

pap