Design problems of ActiveRecord for large application





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







8















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, subscriptions, 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 on 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?










share|improve this question































    8















    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, subscriptions, 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 on 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?










    share|improve this question



























      8












      8








      8


      2






      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, subscriptions, 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 on 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?










      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, subscriptions, 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 on 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?







      php laravel oop design-patterns eloquent






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 16 at 15:50









      Maxim Fedorov

      4,5732425




      4,5732425










      asked Nov 17 '18 at 6:43









      user2693928user2693928

      1,9211716




      1,9211716
























          4 Answers
          4






          active

          oldest

          votes


















          13





          +50









          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
            Nov 19 '18 at 14:38











          • I added a few articles with examples to the post

            – Maxim Fedorov
            Nov 20 '18 at 7:11











          • I think the active record vs doctrine discussion is just a matter of personal choice. Claiming that Active Record is meant for small/simple apps is just wrong! You would be surprised at the "scale" of companies that use Active Record. Unless you're Facebook or Google, it doesn't even matter. And if you are, well then you're probably going to develop your own framework

            – Paras
            Nov 25 '18 at 19:35











          • Any choice of patterns or choice of any technologies is always a personal choice. But this fact doesn't deprive problems of Active Record pattern. Size of a company doesn't show situations in which pattern is applied

            – Maxim Fedorov
            Nov 26 '18 at 6:48











          • So you admit that your comment about ActiveRecord meant for small apps is wrong? The Doctrine pattern also has shortcomings / problems. It's a matter of choice but both have pros and cons. What Doctrine does in thousands of lines of code, ActiveRecord achieves in hundreds.

            – Paras
            Nov 27 '18 at 8:05





















          2















          The class could become 10k lines of code or so




          Each relationship function is 2-4 lines of code so unless you have 2500-5000 relationships, this class is not going to be 10k lines of code. If you do, you already have bad database design.




          Also the Single Responsible Principle maybe is violated because you
          have too many methods in one class.




          Nowhere does the SRP state that you cant have too many methods in one class. It states that a class should have only a single responsibility/reason to exist.




          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).




          This class is a Model class. Its responsibility is to represent one database table. If you have the same table structure in other apps, then yes, you should pull in this class and you would need to pull in pictures, comments, etc. as well since you have the same table structure. If not, you shouldn't pull it in. I don't see any problem here.






          share|improve this answer
























          • Thanks, i'm pointing also other methods belonging in the model not only the relationships. For example: deleteUser(), getFullname(), activateUser(), and so on. The class becomes large and hard to support. If I had somehow only the basic functionality for User without relationships and these methods, I can pull this simple class and use it in another app, but that's maybe complicated because you have to think complex structure and even then you cannot thing of all the possibilities.

            – user2693928
            Nov 26 '18 at 7:28











          • I don't think deleteUser, activateUser should even exist in a class that's not User. If you have a post that's linked to a user and you want to perform an action on the post's user, you would rather do $post->user->activate() rather than $post->activateUser(). Also, there would be only limited actions per model, not thousands.

            – Paras
            Nov 27 '18 at 8:15











          • Yeah, this was just an example, the actions are limited but in a large project they could be hundreds. Some of them are hard to tell if they belongs to the User class or not. For example: $user->disablePayments(), $user->blockAccount() and imagine if you have hundreds of similar methods. Some can say they dont belong to the user class, but I would say why not? $user->blockAccount() looks correct to me.

            – user2693928
            Nov 27 '18 at 8:27






          • 1





            blockAccount seems fine but how many such methods do you have on the model? Don't think they would run in the thousands for a table with a handful of columns.

            – Paras
            Nov 27 '18 at 8:29











          • These yes, but there are methods that are hard to say if they should be in user model or separate class. $user->sendActivationEmail() for example. I suppose they could be extracted in different classes, but either way it gets complicated. You have one class with 500 methods or you have 100 classes with 5 methods.

            – user2693928
            Nov 27 '18 at 9:08



















          1














          You could improve your model with Interfaces.
          If you need one class to inherit a certain behavior common to another, having designed the interface, you need the new class to only implement the interface.



          Interfase based programming



          coding to interfase in PHP






          share|improve this answer































            1














            Consider the following talk from Adam Wathan. He addresses an issue in naming, which leads to bloating of classes. If you consider his methodology of naming methods and shifting the responsibilities, you'll end up with smaller classes and easier to read code.



            Besides don't let yourself get indoctrinated by some rules and 'laws' that exist in programming. If you need a class that is 10k lines, but it's readable then go for it.






            share|improve this answer
























              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',
              autoActivateHeartbeat: false,
              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%2fdesign-problems-of-activerecord-for-large-application%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              13





              +50









              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
                Nov 19 '18 at 14:38











              • I added a few articles with examples to the post

                – Maxim Fedorov
                Nov 20 '18 at 7:11











              • I think the active record vs doctrine discussion is just a matter of personal choice. Claiming that Active Record is meant for small/simple apps is just wrong! You would be surprised at the "scale" of companies that use Active Record. Unless you're Facebook or Google, it doesn't even matter. And if you are, well then you're probably going to develop your own framework

                – Paras
                Nov 25 '18 at 19:35











              • Any choice of patterns or choice of any technologies is always a personal choice. But this fact doesn't deprive problems of Active Record pattern. Size of a company doesn't show situations in which pattern is applied

                – Maxim Fedorov
                Nov 26 '18 at 6:48











              • So you admit that your comment about ActiveRecord meant for small apps is wrong? The Doctrine pattern also has shortcomings / problems. It's a matter of choice but both have pros and cons. What Doctrine does in thousands of lines of code, ActiveRecord achieves in hundreds.

                – Paras
                Nov 27 '18 at 8:05


















              13





              +50









              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
                Nov 19 '18 at 14:38











              • I added a few articles with examples to the post

                – Maxim Fedorov
                Nov 20 '18 at 7:11











              • I think the active record vs doctrine discussion is just a matter of personal choice. Claiming that Active Record is meant for small/simple apps is just wrong! You would be surprised at the "scale" of companies that use Active Record. Unless you're Facebook or Google, it doesn't even matter. And if you are, well then you're probably going to develop your own framework

                – Paras
                Nov 25 '18 at 19:35











              • Any choice of patterns or choice of any technologies is always a personal choice. But this fact doesn't deprive problems of Active Record pattern. Size of a company doesn't show situations in which pattern is applied

                – Maxim Fedorov
                Nov 26 '18 at 6:48











              • So you admit that your comment about ActiveRecord meant for small apps is wrong? The Doctrine pattern also has shortcomings / problems. It's a matter of choice but both have pros and cons. What Doctrine does in thousands of lines of code, ActiveRecord achieves in hundreds.

                – Paras
                Nov 27 '18 at 8:05
















              13





              +50







              13





              +50



              13




              +50





              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 Nov 20 '18 at 7:15

























              answered Nov 19 '18 at 14:18









              Maxim FedorovMaxim Fedorov

              4,5732425




              4,5732425













              • 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
                Nov 19 '18 at 14:38











              • I added a few articles with examples to the post

                – Maxim Fedorov
                Nov 20 '18 at 7:11











              • I think the active record vs doctrine discussion is just a matter of personal choice. Claiming that Active Record is meant for small/simple apps is just wrong! You would be surprised at the "scale" of companies that use Active Record. Unless you're Facebook or Google, it doesn't even matter. And if you are, well then you're probably going to develop your own framework

                – Paras
                Nov 25 '18 at 19:35











              • Any choice of patterns or choice of any technologies is always a personal choice. But this fact doesn't deprive problems of Active Record pattern. Size of a company doesn't show situations in which pattern is applied

                – Maxim Fedorov
                Nov 26 '18 at 6:48











              • So you admit that your comment about ActiveRecord meant for small apps is wrong? The Doctrine pattern also has shortcomings / problems. It's a matter of choice but both have pros and cons. What Doctrine does in thousands of lines of code, ActiveRecord achieves in hundreds.

                – Paras
                Nov 27 '18 at 8:05





















              • 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
                Nov 19 '18 at 14:38











              • I added a few articles with examples to the post

                – Maxim Fedorov
                Nov 20 '18 at 7:11











              • I think the active record vs doctrine discussion is just a matter of personal choice. Claiming that Active Record is meant for small/simple apps is just wrong! You would be surprised at the "scale" of companies that use Active Record. Unless you're Facebook or Google, it doesn't even matter. And if you are, well then you're probably going to develop your own framework

                – Paras
                Nov 25 '18 at 19:35











              • Any choice of patterns or choice of any technologies is always a personal choice. But this fact doesn't deprive problems of Active Record pattern. Size of a company doesn't show situations in which pattern is applied

                – Maxim Fedorov
                Nov 26 '18 at 6:48











              • So you admit that your comment about ActiveRecord meant for small apps is wrong? The Doctrine pattern also has shortcomings / problems. It's a matter of choice but both have pros and cons. What Doctrine does in thousands of lines of code, ActiveRecord achieves in hundreds.

                – Paras
                Nov 27 '18 at 8:05



















              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
              Nov 19 '18 at 14:38





              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
              Nov 19 '18 at 14:38













              I added a few articles with examples to the post

              – Maxim Fedorov
              Nov 20 '18 at 7:11





              I added a few articles with examples to the post

              – Maxim Fedorov
              Nov 20 '18 at 7:11













              I think the active record vs doctrine discussion is just a matter of personal choice. Claiming that Active Record is meant for small/simple apps is just wrong! You would be surprised at the "scale" of companies that use Active Record. Unless you're Facebook or Google, it doesn't even matter. And if you are, well then you're probably going to develop your own framework

              – Paras
              Nov 25 '18 at 19:35





              I think the active record vs doctrine discussion is just a matter of personal choice. Claiming that Active Record is meant for small/simple apps is just wrong! You would be surprised at the "scale" of companies that use Active Record. Unless you're Facebook or Google, it doesn't even matter. And if you are, well then you're probably going to develop your own framework

              – Paras
              Nov 25 '18 at 19:35













              Any choice of patterns or choice of any technologies is always a personal choice. But this fact doesn't deprive problems of Active Record pattern. Size of a company doesn't show situations in which pattern is applied

              – Maxim Fedorov
              Nov 26 '18 at 6:48





              Any choice of patterns or choice of any technologies is always a personal choice. But this fact doesn't deprive problems of Active Record pattern. Size of a company doesn't show situations in which pattern is applied

              – Maxim Fedorov
              Nov 26 '18 at 6:48













              So you admit that your comment about ActiveRecord meant for small apps is wrong? The Doctrine pattern also has shortcomings / problems. It's a matter of choice but both have pros and cons. What Doctrine does in thousands of lines of code, ActiveRecord achieves in hundreds.

              – Paras
              Nov 27 '18 at 8:05







              So you admit that your comment about ActiveRecord meant for small apps is wrong? The Doctrine pattern also has shortcomings / problems. It's a matter of choice but both have pros and cons. What Doctrine does in thousands of lines of code, ActiveRecord achieves in hundreds.

              – Paras
              Nov 27 '18 at 8:05















              2















              The class could become 10k lines of code or so




              Each relationship function is 2-4 lines of code so unless you have 2500-5000 relationships, this class is not going to be 10k lines of code. If you do, you already have bad database design.




              Also the Single Responsible Principle maybe is violated because you
              have too many methods in one class.




              Nowhere does the SRP state that you cant have too many methods in one class. It states that a class should have only a single responsibility/reason to exist.




              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).




              This class is a Model class. Its responsibility is to represent one database table. If you have the same table structure in other apps, then yes, you should pull in this class and you would need to pull in pictures, comments, etc. as well since you have the same table structure. If not, you shouldn't pull it in. I don't see any problem here.






              share|improve this answer
























              • Thanks, i'm pointing also other methods belonging in the model not only the relationships. For example: deleteUser(), getFullname(), activateUser(), and so on. The class becomes large and hard to support. If I had somehow only the basic functionality for User without relationships and these methods, I can pull this simple class and use it in another app, but that's maybe complicated because you have to think complex structure and even then you cannot thing of all the possibilities.

                – user2693928
                Nov 26 '18 at 7:28











              • I don't think deleteUser, activateUser should even exist in a class that's not User. If you have a post that's linked to a user and you want to perform an action on the post's user, you would rather do $post->user->activate() rather than $post->activateUser(). Also, there would be only limited actions per model, not thousands.

                – Paras
                Nov 27 '18 at 8:15











              • Yeah, this was just an example, the actions are limited but in a large project they could be hundreds. Some of them are hard to tell if they belongs to the User class or not. For example: $user->disablePayments(), $user->blockAccount() and imagine if you have hundreds of similar methods. Some can say they dont belong to the user class, but I would say why not? $user->blockAccount() looks correct to me.

                – user2693928
                Nov 27 '18 at 8:27






              • 1





                blockAccount seems fine but how many such methods do you have on the model? Don't think they would run in the thousands for a table with a handful of columns.

                – Paras
                Nov 27 '18 at 8:29











              • These yes, but there are methods that are hard to say if they should be in user model or separate class. $user->sendActivationEmail() for example. I suppose they could be extracted in different classes, but either way it gets complicated. You have one class with 500 methods or you have 100 classes with 5 methods.

                – user2693928
                Nov 27 '18 at 9:08
















              2















              The class could become 10k lines of code or so




              Each relationship function is 2-4 lines of code so unless you have 2500-5000 relationships, this class is not going to be 10k lines of code. If you do, you already have bad database design.




              Also the Single Responsible Principle maybe is violated because you
              have too many methods in one class.




              Nowhere does the SRP state that you cant have too many methods in one class. It states that a class should have only a single responsibility/reason to exist.




              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).




              This class is a Model class. Its responsibility is to represent one database table. If you have the same table structure in other apps, then yes, you should pull in this class and you would need to pull in pictures, comments, etc. as well since you have the same table structure. If not, you shouldn't pull it in. I don't see any problem here.






              share|improve this answer
























              • Thanks, i'm pointing also other methods belonging in the model not only the relationships. For example: deleteUser(), getFullname(), activateUser(), and so on. The class becomes large and hard to support. If I had somehow only the basic functionality for User without relationships and these methods, I can pull this simple class and use it in another app, but that's maybe complicated because you have to think complex structure and even then you cannot thing of all the possibilities.

                – user2693928
                Nov 26 '18 at 7:28











              • I don't think deleteUser, activateUser should even exist in a class that's not User. If you have a post that's linked to a user and you want to perform an action on the post's user, you would rather do $post->user->activate() rather than $post->activateUser(). Also, there would be only limited actions per model, not thousands.

                – Paras
                Nov 27 '18 at 8:15











              • Yeah, this was just an example, the actions are limited but in a large project they could be hundreds. Some of them are hard to tell if they belongs to the User class or not. For example: $user->disablePayments(), $user->blockAccount() and imagine if you have hundreds of similar methods. Some can say they dont belong to the user class, but I would say why not? $user->blockAccount() looks correct to me.

                – user2693928
                Nov 27 '18 at 8:27






              • 1





                blockAccount seems fine but how many such methods do you have on the model? Don't think they would run in the thousands for a table with a handful of columns.

                – Paras
                Nov 27 '18 at 8:29











              • These yes, but there are methods that are hard to say if they should be in user model or separate class. $user->sendActivationEmail() for example. I suppose they could be extracted in different classes, but either way it gets complicated. You have one class with 500 methods or you have 100 classes with 5 methods.

                – user2693928
                Nov 27 '18 at 9:08














              2












              2








              2








              The class could become 10k lines of code or so




              Each relationship function is 2-4 lines of code so unless you have 2500-5000 relationships, this class is not going to be 10k lines of code. If you do, you already have bad database design.




              Also the Single Responsible Principle maybe is violated because you
              have too many methods in one class.




              Nowhere does the SRP state that you cant have too many methods in one class. It states that a class should have only a single responsibility/reason to exist.




              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).




              This class is a Model class. Its responsibility is to represent one database table. If you have the same table structure in other apps, then yes, you should pull in this class and you would need to pull in pictures, comments, etc. as well since you have the same table structure. If not, you shouldn't pull it in. I don't see any problem here.






              share|improve this answer














              The class could become 10k lines of code or so




              Each relationship function is 2-4 lines of code so unless you have 2500-5000 relationships, this class is not going to be 10k lines of code. If you do, you already have bad database design.




              Also the Single Responsible Principle maybe is violated because you
              have too many methods in one class.




              Nowhere does the SRP state that you cant have too many methods in one class. It states that a class should have only a single responsibility/reason to exist.




              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).




              This class is a Model class. Its responsibility is to represent one database table. If you have the same table structure in other apps, then yes, you should pull in this class and you would need to pull in pictures, comments, etc. as well since you have the same table structure. If not, you shouldn't pull it in. I don't see any problem here.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 25 '18 at 19:45









              ParasParas

              5,9701035




              5,9701035













              • Thanks, i'm pointing also other methods belonging in the model not only the relationships. For example: deleteUser(), getFullname(), activateUser(), and so on. The class becomes large and hard to support. If I had somehow only the basic functionality for User without relationships and these methods, I can pull this simple class and use it in another app, but that's maybe complicated because you have to think complex structure and even then you cannot thing of all the possibilities.

                – user2693928
                Nov 26 '18 at 7:28











              • I don't think deleteUser, activateUser should even exist in a class that's not User. If you have a post that's linked to a user and you want to perform an action on the post's user, you would rather do $post->user->activate() rather than $post->activateUser(). Also, there would be only limited actions per model, not thousands.

                – Paras
                Nov 27 '18 at 8:15











              • Yeah, this was just an example, the actions are limited but in a large project they could be hundreds. Some of them are hard to tell if they belongs to the User class or not. For example: $user->disablePayments(), $user->blockAccount() and imagine if you have hundreds of similar methods. Some can say they dont belong to the user class, but I would say why not? $user->blockAccount() looks correct to me.

                – user2693928
                Nov 27 '18 at 8:27






              • 1





                blockAccount seems fine but how many such methods do you have on the model? Don't think they would run in the thousands for a table with a handful of columns.

                – Paras
                Nov 27 '18 at 8:29











              • These yes, but there are methods that are hard to say if they should be in user model or separate class. $user->sendActivationEmail() for example. I suppose they could be extracted in different classes, but either way it gets complicated. You have one class with 500 methods or you have 100 classes with 5 methods.

                – user2693928
                Nov 27 '18 at 9:08



















              • Thanks, i'm pointing also other methods belonging in the model not only the relationships. For example: deleteUser(), getFullname(), activateUser(), and so on. The class becomes large and hard to support. If I had somehow only the basic functionality for User without relationships and these methods, I can pull this simple class and use it in another app, but that's maybe complicated because you have to think complex structure and even then you cannot thing of all the possibilities.

                – user2693928
                Nov 26 '18 at 7:28











              • I don't think deleteUser, activateUser should even exist in a class that's not User. If you have a post that's linked to a user and you want to perform an action on the post's user, you would rather do $post->user->activate() rather than $post->activateUser(). Also, there would be only limited actions per model, not thousands.

                – Paras
                Nov 27 '18 at 8:15











              • Yeah, this was just an example, the actions are limited but in a large project they could be hundreds. Some of them are hard to tell if they belongs to the User class or not. For example: $user->disablePayments(), $user->blockAccount() and imagine if you have hundreds of similar methods. Some can say they dont belong to the user class, but I would say why not? $user->blockAccount() looks correct to me.

                – user2693928
                Nov 27 '18 at 8:27






              • 1





                blockAccount seems fine but how many such methods do you have on the model? Don't think they would run in the thousands for a table with a handful of columns.

                – Paras
                Nov 27 '18 at 8:29











              • These yes, but there are methods that are hard to say if they should be in user model or separate class. $user->sendActivationEmail() for example. I suppose they could be extracted in different classes, but either way it gets complicated. You have one class with 500 methods or you have 100 classes with 5 methods.

                – user2693928
                Nov 27 '18 at 9:08

















              Thanks, i'm pointing also other methods belonging in the model not only the relationships. For example: deleteUser(), getFullname(), activateUser(), and so on. The class becomes large and hard to support. If I had somehow only the basic functionality for User without relationships and these methods, I can pull this simple class and use it in another app, but that's maybe complicated because you have to think complex structure and even then you cannot thing of all the possibilities.

              – user2693928
              Nov 26 '18 at 7:28





              Thanks, i'm pointing also other methods belonging in the model not only the relationships. For example: deleteUser(), getFullname(), activateUser(), and so on. The class becomes large and hard to support. If I had somehow only the basic functionality for User without relationships and these methods, I can pull this simple class and use it in another app, but that's maybe complicated because you have to think complex structure and even then you cannot thing of all the possibilities.

              – user2693928
              Nov 26 '18 at 7:28













              I don't think deleteUser, activateUser should even exist in a class that's not User. If you have a post that's linked to a user and you want to perform an action on the post's user, you would rather do $post->user->activate() rather than $post->activateUser(). Also, there would be only limited actions per model, not thousands.

              – Paras
              Nov 27 '18 at 8:15





              I don't think deleteUser, activateUser should even exist in a class that's not User. If you have a post that's linked to a user and you want to perform an action on the post's user, you would rather do $post->user->activate() rather than $post->activateUser(). Also, there would be only limited actions per model, not thousands.

              – Paras
              Nov 27 '18 at 8:15













              Yeah, this was just an example, the actions are limited but in a large project they could be hundreds. Some of them are hard to tell if they belongs to the User class or not. For example: $user->disablePayments(), $user->blockAccount() and imagine if you have hundreds of similar methods. Some can say they dont belong to the user class, but I would say why not? $user->blockAccount() looks correct to me.

              – user2693928
              Nov 27 '18 at 8:27





              Yeah, this was just an example, the actions are limited but in a large project they could be hundreds. Some of them are hard to tell if they belongs to the User class or not. For example: $user->disablePayments(), $user->blockAccount() and imagine if you have hundreds of similar methods. Some can say they dont belong to the user class, but I would say why not? $user->blockAccount() looks correct to me.

              – user2693928
              Nov 27 '18 at 8:27




              1




              1





              blockAccount seems fine but how many such methods do you have on the model? Don't think they would run in the thousands for a table with a handful of columns.

              – Paras
              Nov 27 '18 at 8:29





              blockAccount seems fine but how many such methods do you have on the model? Don't think they would run in the thousands for a table with a handful of columns.

              – Paras
              Nov 27 '18 at 8:29













              These yes, but there are methods that are hard to say if they should be in user model or separate class. $user->sendActivationEmail() for example. I suppose they could be extracted in different classes, but either way it gets complicated. You have one class with 500 methods or you have 100 classes with 5 methods.

              – user2693928
              Nov 27 '18 at 9:08





              These yes, but there are methods that are hard to say if they should be in user model or separate class. $user->sendActivationEmail() for example. I suppose they could be extracted in different classes, but either way it gets complicated. You have one class with 500 methods or you have 100 classes with 5 methods.

              – user2693928
              Nov 27 '18 at 9:08











              1














              You could improve your model with Interfaces.
              If you need one class to inherit a certain behavior common to another, having designed the interface, you need the new class to only implement the interface.



              Interfase based programming



              coding to interfase in PHP






              share|improve this answer




























                1














                You could improve your model with Interfaces.
                If you need one class to inherit a certain behavior common to another, having designed the interface, you need the new class to only implement the interface.



                Interfase based programming



                coding to interfase in PHP






                share|improve this answer


























                  1












                  1








                  1







                  You could improve your model with Interfaces.
                  If you need one class to inherit a certain behavior common to another, having designed the interface, you need the new class to only implement the interface.



                  Interfase based programming



                  coding to interfase in PHP






                  share|improve this answer













                  You could improve your model with Interfaces.
                  If you need one class to inherit a certain behavior common to another, having designed the interface, you need the new class to only implement the interface.



                  Interfase based programming



                  coding to interfase in PHP







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 16:24









                  Jorge SBJorge SB

                  1456




                  1456























                      1














                      Consider the following talk from Adam Wathan. He addresses an issue in naming, which leads to bloating of classes. If you consider his methodology of naming methods and shifting the responsibilities, you'll end up with smaller classes and easier to read code.



                      Besides don't let yourself get indoctrinated by some rules and 'laws' that exist in programming. If you need a class that is 10k lines, but it's readable then go for it.






                      share|improve this answer




























                        1














                        Consider the following talk from Adam Wathan. He addresses an issue in naming, which leads to bloating of classes. If you consider his methodology of naming methods and shifting the responsibilities, you'll end up with smaller classes and easier to read code.



                        Besides don't let yourself get indoctrinated by some rules and 'laws' that exist in programming. If you need a class that is 10k lines, but it's readable then go for it.






                        share|improve this answer


























                          1












                          1








                          1







                          Consider the following talk from Adam Wathan. He addresses an issue in naming, which leads to bloating of classes. If you consider his methodology of naming methods and shifting the responsibilities, you'll end up with smaller classes and easier to read code.



                          Besides don't let yourself get indoctrinated by some rules and 'laws' that exist in programming. If you need a class that is 10k lines, but it's readable then go for it.






                          share|improve this answer













                          Consider the following talk from Adam Wathan. He addresses an issue in naming, which leads to bloating of classes. If you consider his methodology of naming methods and shifting the responsibilities, you'll end up with smaller classes and easier to read code.



                          Besides don't let yourself get indoctrinated by some rules and 'laws' that exist in programming. If you need a class that is 10k lines, but it's readable then go for it.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 8:46









                          BorisuBorisu

                          559412




                          559412






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348905%2fdesign-problems-of-activerecord-for-large-application%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

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python