Web framework object oriented architecture question
up vote
2
down vote
favorite
In most of the frameworks you have model classes that represents row in the database.
For example php code:
class User extends Model {}
I'm giving Laravel eloquent example but this is true for most php frameworks.
Then you add the relationships in the class:
public function pictures()
{
return $this->hasMany('AppPicture');
}
Then you add some methods like this:
public function deleteComments()
{
// delete comments code here
}
My first question is: Is this good design architecture, because after years when the project becomes large you will have many relationships (pictures, comments, posts, subscribtions, etc connected to the user).
The class could become 10k lines of code or so.
In that case the class will become very large and hard to maintain.
Also the Single Responsible Principle maybe is violated because you have too many methods in one class.
Also if I want to use the class it in another app I cannot, simply because I'll have to pull also the pictures, comments and etc in the second application(website).
If I make other classes like "UserPictures", "UserPictureDeleter" the code will get more complicated.
So is this good practice and if not do you have any suggestions how to make the code not bloated with so many methods but easy to use.
Do you agree that all these methods belong to the User class.
Thank you
php oop frameworks
This question has an open bounty worth +50
reputation from user2693928 ending in 5 days.
This question has not received enough attention.
add a comment |
up vote
2
down vote
favorite
In most of the frameworks you have model classes that represents row in the database.
For example php code:
class User extends Model {}
I'm giving Laravel eloquent example but this is true for most php frameworks.
Then you add the relationships in the class:
public function pictures()
{
return $this->hasMany('AppPicture');
}
Then you add some methods like this:
public function deleteComments()
{
// delete comments code here
}
My first question is: Is this good design architecture, because after years when the project becomes large you will have many relationships (pictures, comments, posts, subscribtions, etc connected to the user).
The class could become 10k lines of code or so.
In that case the class will become very large and hard to maintain.
Also the Single Responsible Principle maybe is violated because you have too many methods in one class.
Also if I want to use the class it in another app I cannot, simply because I'll have to pull also the pictures, comments and etc in the second application(website).
If I make other classes like "UserPictures", "UserPictureDeleter" the code will get more complicated.
So is this good practice and if not do you have any suggestions how to make the code not bloated with so many methods but easy to use.
Do you agree that all these methods belong to the User class.
Thank you
php oop frameworks
This question has an open bounty worth +50
reputation from user2693928 ending in 5 days.
This question has not received enough attention.
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
In most of the frameworks you have model classes that represents row in the database.
For example php code:
class User extends Model {}
I'm giving Laravel eloquent example but this is true for most php frameworks.
Then you add the relationships in the class:
public function pictures()
{
return $this->hasMany('AppPicture');
}
Then you add some methods like this:
public function deleteComments()
{
// delete comments code here
}
My first question is: Is this good design architecture, because after years when the project becomes large you will have many relationships (pictures, comments, posts, subscribtions, etc connected to the user).
The class could become 10k lines of code or so.
In that case the class will become very large and hard to maintain.
Also the Single Responsible Principle maybe is violated because you have too many methods in one class.
Also if I want to use the class it in another app I cannot, simply because I'll have to pull also the pictures, comments and etc in the second application(website).
If I make other classes like "UserPictures", "UserPictureDeleter" the code will get more complicated.
So is this good practice and if not do you have any suggestions how to make the code not bloated with so many methods but easy to use.
Do you agree that all these methods belong to the User class.
Thank you
php oop frameworks
In most of the frameworks you have model classes that represents row in the database.
For example php code:
class User extends Model {}
I'm giving Laravel eloquent example but this is true for most php frameworks.
Then you add the relationships in the class:
public function pictures()
{
return $this->hasMany('AppPicture');
}
Then you add some methods like this:
public function deleteComments()
{
// delete comments code here
}
My first question is: Is this good design architecture, because after years when the project becomes large you will have many relationships (pictures, comments, posts, subscribtions, etc connected to the user).
The class could become 10k lines of code or so.
In that case the class will become very large and hard to maintain.
Also the Single Responsible Principle maybe is violated because you have too many methods in one class.
Also if I want to use the class it in another app I cannot, simply because I'll have to pull also the pictures, comments and etc in the second application(website).
If I make other classes like "UserPictures", "UserPictureDeleter" the code will get more complicated.
So is this good practice and if not do you have any suggestions how to make the code not bloated with so many methods but easy to use.
Do you agree that all these methods belong to the User class.
Thank you
php oop frameworks
php oop frameworks
edited 2 days ago


Maxim Fedorov
1,535214
1,535214
asked Nov 17 at 6:43
user2693928
1,714715
1,714715
This question has an open bounty worth +50
reputation from user2693928 ending in 5 days.
This question has not received enough attention.
This question has an open bounty worth +50
reputation from user2693928 ending in 5 days.
This question has not received enough attention.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Laravel and some another framework provide Active Record conception in their base classes of model. The main idea of Active Record is a representation of table row as an object that includes data of a row and methods of work with databases.
Using of Active Record pattern is fully justified in small simple applications because this pattern gives an ability to fast develop your application. But if your application has a lot of code and difficult business logic, Active Record will make many problems in the architecture of your application. Active Record can make the following problems:
- violation of the Single Responsibility Principle that makes bloated code. Active Record always violates this principle, because it always has two responsibility: it implements business logic and methods of database work
- violation of low coupling principle (GRASP) that makes code reuse more difficult
- сreation of qualified abstraction is difficult if you use Active Record pattern
The solution to those problems is using of OOP abstractions instead of using of table rows abstractions. For example, you can use Domain Model and Domain-driven design. This approach is better than Active Record pattern for large applications.
Unfortunately, those concepts are too large-volume for explanation in this post, but you can read "Domain Driven Design" by Eric Evans. It is a good book about application design. Also, you can find many articles about those concepts in google. For example Building a Domain Model, Implementing Domain-Driven Design in PHP (Laravel)
Thanks, nice answer, but with Domain Model doesn't it mean i will have more code reuse? Instead of Active record "joins" for example i will use custom ones all over the domain models (user, pictures, etc). can you provide very simple way with code (php, java, whatever) just to get the idea. Thank you
– user2693928
2 days ago
I added a few articles with examples to the post
– Maxim Fedorov
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Laravel and some another framework provide Active Record conception in their base classes of model. The main idea of Active Record is a representation of table row as an object that includes data of a row and methods of work with databases.
Using of Active Record pattern is fully justified in small simple applications because this pattern gives an ability to fast develop your application. But if your application has a lot of code and difficult business logic, Active Record will make many problems in the architecture of your application. Active Record can make the following problems:
- violation of the Single Responsibility Principle that makes bloated code. Active Record always violates this principle, because it always has two responsibility: it implements business logic and methods of database work
- violation of low coupling principle (GRASP) that makes code reuse more difficult
- сreation of qualified abstraction is difficult if you use Active Record pattern
The solution to those problems is using of OOP abstractions instead of using of table rows abstractions. For example, you can use Domain Model and Domain-driven design. This approach is better than Active Record pattern for large applications.
Unfortunately, those concepts are too large-volume for explanation in this post, but you can read "Domain Driven Design" by Eric Evans. It is a good book about application design. Also, you can find many articles about those concepts in google. For example Building a Domain Model, Implementing Domain-Driven Design in PHP (Laravel)
Thanks, nice answer, but with Domain Model doesn't it mean i will have more code reuse? Instead of Active record "joins" for example i will use custom ones all over the domain models (user, pictures, etc). can you provide very simple way with code (php, java, whatever) just to get the idea. Thank you
– user2693928
2 days ago
I added a few articles with examples to the post
– Maxim Fedorov
yesterday
add a comment |
up vote
3
down vote
accepted
Laravel and some another framework provide Active Record conception in their base classes of model. The main idea of Active Record is a representation of table row as an object that includes data of a row and methods of work with databases.
Using of Active Record pattern is fully justified in small simple applications because this pattern gives an ability to fast develop your application. But if your application has a lot of code and difficult business logic, Active Record will make many problems in the architecture of your application. Active Record can make the following problems:
- violation of the Single Responsibility Principle that makes bloated code. Active Record always violates this principle, because it always has two responsibility: it implements business logic and methods of database work
- violation of low coupling principle (GRASP) that makes code reuse more difficult
- сreation of qualified abstraction is difficult if you use Active Record pattern
The solution to those problems is using of OOP abstractions instead of using of table rows abstractions. For example, you can use Domain Model and Domain-driven design. This approach is better than Active Record pattern for large applications.
Unfortunately, those concepts are too large-volume for explanation in this post, but you can read "Domain Driven Design" by Eric Evans. It is a good book about application design. Also, you can find many articles about those concepts in google. For example Building a Domain Model, Implementing Domain-Driven Design in PHP (Laravel)
Thanks, nice answer, but with Domain Model doesn't it mean i will have more code reuse? Instead of Active record "joins" for example i will use custom ones all over the domain models (user, pictures, etc). can you provide very simple way with code (php, java, whatever) just to get the idea. Thank you
– user2693928
2 days ago
I added a few articles with examples to the post
– Maxim Fedorov
yesterday
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Laravel and some another framework provide Active Record conception in their base classes of model. The main idea of Active Record is a representation of table row as an object that includes data of a row and methods of work with databases.
Using of Active Record pattern is fully justified in small simple applications because this pattern gives an ability to fast develop your application. But if your application has a lot of code and difficult business logic, Active Record will make many problems in the architecture of your application. Active Record can make the following problems:
- violation of the Single Responsibility Principle that makes bloated code. Active Record always violates this principle, because it always has two responsibility: it implements business logic and methods of database work
- violation of low coupling principle (GRASP) that makes code reuse more difficult
- сreation of qualified abstraction is difficult if you use Active Record pattern
The solution to those problems is using of OOP abstractions instead of using of table rows abstractions. For example, you can use Domain Model and Domain-driven design. This approach is better than Active Record pattern for large applications.
Unfortunately, those concepts are too large-volume for explanation in this post, but you can read "Domain Driven Design" by Eric Evans. It is a good book about application design. Also, you can find many articles about those concepts in google. For example Building a Domain Model, Implementing Domain-Driven Design in PHP (Laravel)
Laravel and some another framework provide Active Record conception in their base classes of model. The main idea of Active Record is a representation of table row as an object that includes data of a row and methods of work with databases.
Using of Active Record pattern is fully justified in small simple applications because this pattern gives an ability to fast develop your application. But if your application has a lot of code and difficult business logic, Active Record will make many problems in the architecture of your application. Active Record can make the following problems:
- violation of the Single Responsibility Principle that makes bloated code. Active Record always violates this principle, because it always has two responsibility: it implements business logic and methods of database work
- violation of low coupling principle (GRASP) that makes code reuse more difficult
- сreation of qualified abstraction is difficult if you use Active Record pattern
The solution to those problems is using of OOP abstractions instead of using of table rows abstractions. For example, you can use Domain Model and Domain-driven design. This approach is better than Active Record pattern for large applications.
Unfortunately, those concepts are too large-volume for explanation in this post, but you can read "Domain Driven Design" by Eric Evans. It is a good book about application design. Also, you can find many articles about those concepts in google. For example Building a Domain Model, Implementing Domain-Driven Design in PHP (Laravel)
edited yesterday
answered 2 days ago


Maxim Fedorov
1,535214
1,535214
Thanks, nice answer, but with Domain Model doesn't it mean i will have more code reuse? Instead of Active record "joins" for example i will use custom ones all over the domain models (user, pictures, etc). can you provide very simple way with code (php, java, whatever) just to get the idea. Thank you
– user2693928
2 days ago
I added a few articles with examples to the post
– Maxim Fedorov
yesterday
add a comment |
Thanks, nice answer, but with Domain Model doesn't it mean i will have more code reuse? Instead of Active record "joins" for example i will use custom ones all over the domain models (user, pictures, etc). can you provide very simple way with code (php, java, whatever) just to get the idea. Thank you
– user2693928
2 days ago
I added a few articles with examples to the post
– Maxim Fedorov
yesterday
Thanks, nice answer, but with Domain Model doesn't it mean i will have more code reuse? Instead of Active record "joins" for example i will use custom ones all over the domain models (user, pictures, etc). can you provide very simple way with code (php, java, whatever) just to get the idea. Thank you
– user2693928
2 days ago
Thanks, nice answer, but with Domain Model doesn't it mean i will have more code reuse? Instead of Active record "joins" for example i will use custom ones all over the domain models (user, pictures, etc). can you provide very simple way with code (php, java, whatever) just to get the idea. Thank you
– user2693928
2 days ago
I added a few articles with examples to the post
– Maxim Fedorov
yesterday
I added a few articles with examples to the post
– Maxim Fedorov
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348905%2fweb-framework-object-oriented-architecture-question%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown