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










share|improve this question

















This question has an open bounty worth +50
reputation from user2693928 ending in 5 days.


This question has not received enough attention.




















    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










    share|improve this question

















    This question has an open bounty worth +50
    reputation from user2693928 ending in 5 days.


    This question has not received enough attention.


















      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










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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.


























          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)






          share|improve this answer























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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)






          share|improve this answer























          • 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















          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)






          share|improve this answer























          • 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













          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)






          share|improve this answer














          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)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory