Which service class do methods retrieving lists of nested objects belong to?











up vote
1
down vote

favorite
1












This is applicable to any language, but I'm using Java for this so...



public class Egg {
...
}

public class BirdNest {

private List<Egg> eggs;

...
}


I also have database entities which match these classes. Basically, BirdNest has 1:M relationship with Egg.



To perform persistence/retrieval actions on these classes I also have a BirdNestService and an EggService.



Say I want to retrieve a list of eggs from a given bird nest.



I could have a method like List<Egg> getEggs (int birdNestId);



My question is, which service should methods like this belong to?



It's performing operations based on a particular bird nest so you could argue it should be part of the BirdNestService.



Then again you could argue the item it is retrieving are Egg's so it should belong to the EggService.










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    This is applicable to any language, but I'm using Java for this so...



    public class Egg {
    ...
    }

    public class BirdNest {

    private List<Egg> eggs;

    ...
    }


    I also have database entities which match these classes. Basically, BirdNest has 1:M relationship with Egg.



    To perform persistence/retrieval actions on these classes I also have a BirdNestService and an EggService.



    Say I want to retrieve a list of eggs from a given bird nest.



    I could have a method like List<Egg> getEggs (int birdNestId);



    My question is, which service should methods like this belong to?



    It's performing operations based on a particular bird nest so you could argue it should be part of the BirdNestService.



    Then again you could argue the item it is retrieving are Egg's so it should belong to the EggService.










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      This is applicable to any language, but I'm using Java for this so...



      public class Egg {
      ...
      }

      public class BirdNest {

      private List<Egg> eggs;

      ...
      }


      I also have database entities which match these classes. Basically, BirdNest has 1:M relationship with Egg.



      To perform persistence/retrieval actions on these classes I also have a BirdNestService and an EggService.



      Say I want to retrieve a list of eggs from a given bird nest.



      I could have a method like List<Egg> getEggs (int birdNestId);



      My question is, which service should methods like this belong to?



      It's performing operations based on a particular bird nest so you could argue it should be part of the BirdNestService.



      Then again you could argue the item it is retrieving are Egg's so it should belong to the EggService.










      share|improve this question















      This is applicable to any language, but I'm using Java for this so...



      public class Egg {
      ...
      }

      public class BirdNest {

      private List<Egg> eggs;

      ...
      }


      I also have database entities which match these classes. Basically, BirdNest has 1:M relationship with Egg.



      To perform persistence/retrieval actions on these classes I also have a BirdNestService and an EggService.



      Say I want to retrieve a list of eggs from a given bird nest.



      I could have a method like List<Egg> getEggs (int birdNestId);



      My question is, which service should methods like this belong to?



      It's performing operations based on a particular bird nest so you could argue it should be part of the BirdNestService.



      Then again you could argue the item it is retrieving are Egg's so it should belong to the EggService.







      java design-patterns language-agnostic






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 at 14:41









      Ali Soltani

      6,3422829




      6,3422829










      asked Nov 9 at 3:56









      noobcoder

      1,3792923




      1,3792923
























          6 Answers
          6






          active

          oldest

          votes

















          up vote
          9
          down vote



          accepted
          +50










          If you put it into EggService then this service knows about a BirdNest concept (birdNestId) which is not something you want.



          So the answer is BirdNestService.






          share|improve this answer




























            up vote
            0
            down vote













            I think it is the part of BirdNestService because Egg is just an entity and it does't depend upon Nest. But Nest contains Eggs otherwise Nest is useless without the Eggs. Nest depend upon Eggs but eggs does't depend upon Nest. In my opinion Nest must have information for which eggs he have and it already contain the list of eggs as you described in the class and eggs doesn't need to remember their Nest Id unless you have a very complex scenario in which you want to find nest Id by their Eggs or some specific requirement.






            share|improve this answer




























              up vote
              0
              down vote













              BirdNestService shouldn't need a method like List<Egg> getEggs (int birdNestId);.

              If you want to retrieve eggs from a given birdnest, then it should be simple like that: List<Egg> myEggs = myBirdNest.getEggs()
              BirdNestService should have operations (methods) you want to use on birdnests, e.g. BirdNest buildBirdNest(int amountStems, Bird byBird).






              share|improve this answer




























                up vote
                0
                down vote













                I would say it should be the EggService.



                The reason is currently your query only needs Eggs by Birds Nest but that is probably not going to be the case always. Tomorrow you may need to find eggs based on certain characteristics for example size, color etc...



                Hence I would go for EggService because after all you are trying to identify Eggs with a certain characteristic - in this case belonging to a certain birdnest.






                share|improve this answer




























                  up vote
                  0
                  down vote













                  List getEggs (int birdNestId) method is asking for eggs contained in a particular nest. The way i think about it :




                  • Conceptually a bird nest stores handles(ids) for eggs. From the database perspective, foreign keys to egg table. Actual eggs are stored in egg table.

                  • Hence it makes more sense to ask BirdNestService for eggs of a nest which can do some validations like whether it's a valid nest id or not and then retrieve the handles of eggs.

                  • It can then consult EggService to give a list of eggs matching a list of ids.


                  Hence, i would prefer to have List getEggs (int birdNestId) method method in BirdNestService and will add a method in EggService like this :
                  List getEggs(List eggIds).



                  But again, there is no right or wrong answer. It will depend on what makes your design clean, coherent and maintainable.






                  share|improve this answer




























                    up vote
                    0
                    down vote













                    You retrieve attribute of the entity, so it's part of the nest. No brainer.



                    Now, if you had attribute birdnestid and you filtered your egg database by that attribute it would be part of egg entity, hence you would put it into egg service.



                    Rule of thumb: it's okay as long as it's an attribute.



                    In that way you can have both methods in two different services.



                    This also applies to multilayered attributes: it depends on the entity you use as base



                    Which should you use? As a rule of thumb, use top to bottom approach: always go for parent when you need the child






                    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',
                      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%2f53219661%2fwhich-service-class-do-methods-retrieving-lists-of-nested-objects-belong-to%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      6 Answers
                      6






                      active

                      oldest

                      votes








                      6 Answers
                      6






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      9
                      down vote



                      accepted
                      +50










                      If you put it into EggService then this service knows about a BirdNest concept (birdNestId) which is not something you want.



                      So the answer is BirdNestService.






                      share|improve this answer

























                        up vote
                        9
                        down vote



                        accepted
                        +50










                        If you put it into EggService then this service knows about a BirdNest concept (birdNestId) which is not something you want.



                        So the answer is BirdNestService.






                        share|improve this answer























                          up vote
                          9
                          down vote



                          accepted
                          +50







                          up vote
                          9
                          down vote



                          accepted
                          +50




                          +50




                          If you put it into EggService then this service knows about a BirdNest concept (birdNestId) which is not something you want.



                          So the answer is BirdNestService.






                          share|improve this answer












                          If you put it into EggService then this service knows about a BirdNest concept (birdNestId) which is not something you want.



                          So the answer is BirdNestService.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 9 at 5:10









                          Nghia Bui

                          1,443812




                          1,443812
























                              up vote
                              0
                              down vote













                              I think it is the part of BirdNestService because Egg is just an entity and it does't depend upon Nest. But Nest contains Eggs otherwise Nest is useless without the Eggs. Nest depend upon Eggs but eggs does't depend upon Nest. In my opinion Nest must have information for which eggs he have and it already contain the list of eggs as you described in the class and eggs doesn't need to remember their Nest Id unless you have a very complex scenario in which you want to find nest Id by their Eggs or some specific requirement.






                              share|improve this answer

























                                up vote
                                0
                                down vote













                                I think it is the part of BirdNestService because Egg is just an entity and it does't depend upon Nest. But Nest contains Eggs otherwise Nest is useless without the Eggs. Nest depend upon Eggs but eggs does't depend upon Nest. In my opinion Nest must have information for which eggs he have and it already contain the list of eggs as you described in the class and eggs doesn't need to remember their Nest Id unless you have a very complex scenario in which you want to find nest Id by their Eggs or some specific requirement.






                                share|improve this answer























                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  I think it is the part of BirdNestService because Egg is just an entity and it does't depend upon Nest. But Nest contains Eggs otherwise Nest is useless without the Eggs. Nest depend upon Eggs but eggs does't depend upon Nest. In my opinion Nest must have information for which eggs he have and it already contain the list of eggs as you described in the class and eggs doesn't need to remember their Nest Id unless you have a very complex scenario in which you want to find nest Id by their Eggs or some specific requirement.






                                  share|improve this answer












                                  I think it is the part of BirdNestService because Egg is just an entity and it does't depend upon Nest. But Nest contains Eggs otherwise Nest is useless without the Eggs. Nest depend upon Eggs but eggs does't depend upon Nest. In my opinion Nest must have information for which eggs he have and it already contain the list of eggs as you described in the class and eggs doesn't need to remember their Nest Id unless you have a very complex scenario in which you want to find nest Id by their Eggs or some specific requirement.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 9 at 4:09









                                  Khalid Shah

                                  1,127620




                                  1,127620






















                                      up vote
                                      0
                                      down vote













                                      BirdNestService shouldn't need a method like List<Egg> getEggs (int birdNestId);.

                                      If you want to retrieve eggs from a given birdnest, then it should be simple like that: List<Egg> myEggs = myBirdNest.getEggs()
                                      BirdNestService should have operations (methods) you want to use on birdnests, e.g. BirdNest buildBirdNest(int amountStems, Bird byBird).






                                      share|improve this answer

























                                        up vote
                                        0
                                        down vote













                                        BirdNestService shouldn't need a method like List<Egg> getEggs (int birdNestId);.

                                        If you want to retrieve eggs from a given birdnest, then it should be simple like that: List<Egg> myEggs = myBirdNest.getEggs()
                                        BirdNestService should have operations (methods) you want to use on birdnests, e.g. BirdNest buildBirdNest(int amountStems, Bird byBird).






                                        share|improve this answer























                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          BirdNestService shouldn't need a method like List<Egg> getEggs (int birdNestId);.

                                          If you want to retrieve eggs from a given birdnest, then it should be simple like that: List<Egg> myEggs = myBirdNest.getEggs()
                                          BirdNestService should have operations (methods) you want to use on birdnests, e.g. BirdNest buildBirdNest(int amountStems, Bird byBird).






                                          share|improve this answer












                                          BirdNestService shouldn't need a method like List<Egg> getEggs (int birdNestId);.

                                          If you want to retrieve eggs from a given birdnest, then it should be simple like that: List<Egg> myEggs = myBirdNest.getEggs()
                                          BirdNestService should have operations (methods) you want to use on birdnests, e.g. BirdNest buildBirdNest(int amountStems, Bird byBird).







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 16 at 17:24









                                          ndueck

                                          100111




                                          100111






















                                              up vote
                                              0
                                              down vote













                                              I would say it should be the EggService.



                                              The reason is currently your query only needs Eggs by Birds Nest but that is probably not going to be the case always. Tomorrow you may need to find eggs based on certain characteristics for example size, color etc...



                                              Hence I would go for EggService because after all you are trying to identify Eggs with a certain characteristic - in this case belonging to a certain birdnest.






                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote













                                                I would say it should be the EggService.



                                                The reason is currently your query only needs Eggs by Birds Nest but that is probably not going to be the case always. Tomorrow you may need to find eggs based on certain characteristics for example size, color etc...



                                                Hence I would go for EggService because after all you are trying to identify Eggs with a certain characteristic - in this case belonging to a certain birdnest.






                                                share|improve this answer























                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  I would say it should be the EggService.



                                                  The reason is currently your query only needs Eggs by Birds Nest but that is probably not going to be the case always. Tomorrow you may need to find eggs based on certain characteristics for example size, color etc...



                                                  Hence I would go for EggService because after all you are trying to identify Eggs with a certain characteristic - in this case belonging to a certain birdnest.






                                                  share|improve this answer












                                                  I would say it should be the EggService.



                                                  The reason is currently your query only needs Eggs by Birds Nest but that is probably not going to be the case always. Tomorrow you may need to find eggs based on certain characteristics for example size, color etc...



                                                  Hence I would go for EggService because after all you are trying to identify Eggs with a certain characteristic - in this case belonging to a certain birdnest.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 17 at 12:40









                                                  Sid Malani

                                                  1,73211011




                                                  1,73211011






















                                                      up vote
                                                      0
                                                      down vote













                                                      List getEggs (int birdNestId) method is asking for eggs contained in a particular nest. The way i think about it :




                                                      • Conceptually a bird nest stores handles(ids) for eggs. From the database perspective, foreign keys to egg table. Actual eggs are stored in egg table.

                                                      • Hence it makes more sense to ask BirdNestService for eggs of a nest which can do some validations like whether it's a valid nest id or not and then retrieve the handles of eggs.

                                                      • It can then consult EggService to give a list of eggs matching a list of ids.


                                                      Hence, i would prefer to have List getEggs (int birdNestId) method method in BirdNestService and will add a method in EggService like this :
                                                      List getEggs(List eggIds).



                                                      But again, there is no right or wrong answer. It will depend on what makes your design clean, coherent and maintainable.






                                                      share|improve this answer

























                                                        up vote
                                                        0
                                                        down vote













                                                        List getEggs (int birdNestId) method is asking for eggs contained in a particular nest. The way i think about it :




                                                        • Conceptually a bird nest stores handles(ids) for eggs. From the database perspective, foreign keys to egg table. Actual eggs are stored in egg table.

                                                        • Hence it makes more sense to ask BirdNestService for eggs of a nest which can do some validations like whether it's a valid nest id or not and then retrieve the handles of eggs.

                                                        • It can then consult EggService to give a list of eggs matching a list of ids.


                                                        Hence, i would prefer to have List getEggs (int birdNestId) method method in BirdNestService and will add a method in EggService like this :
                                                        List getEggs(List eggIds).



                                                        But again, there is no right or wrong answer. It will depend on what makes your design clean, coherent and maintainable.






                                                        share|improve this answer























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          List getEggs (int birdNestId) method is asking for eggs contained in a particular nest. The way i think about it :




                                                          • Conceptually a bird nest stores handles(ids) for eggs. From the database perspective, foreign keys to egg table. Actual eggs are stored in egg table.

                                                          • Hence it makes more sense to ask BirdNestService for eggs of a nest which can do some validations like whether it's a valid nest id or not and then retrieve the handles of eggs.

                                                          • It can then consult EggService to give a list of eggs matching a list of ids.


                                                          Hence, i would prefer to have List getEggs (int birdNestId) method method in BirdNestService and will add a method in EggService like this :
                                                          List getEggs(List eggIds).



                                                          But again, there is no right or wrong answer. It will depend on what makes your design clean, coherent and maintainable.






                                                          share|improve this answer












                                                          List getEggs (int birdNestId) method is asking for eggs contained in a particular nest. The way i think about it :




                                                          • Conceptually a bird nest stores handles(ids) for eggs. From the database perspective, foreign keys to egg table. Actual eggs are stored in egg table.

                                                          • Hence it makes more sense to ask BirdNestService for eggs of a nest which can do some validations like whether it's a valid nest id or not and then retrieve the handles of eggs.

                                                          • It can then consult EggService to give a list of eggs matching a list of ids.


                                                          Hence, i would prefer to have List getEggs (int birdNestId) method method in BirdNestService and will add a method in EggService like this :
                                                          List getEggs(List eggIds).



                                                          But again, there is no right or wrong answer. It will depend on what makes your design clean, coherent and maintainable.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 18 at 4:10









                                                          K. M. Fazle Azim Babu

                                                          1264




                                                          1264






















                                                              up vote
                                                              0
                                                              down vote













                                                              You retrieve attribute of the entity, so it's part of the nest. No brainer.



                                                              Now, if you had attribute birdnestid and you filtered your egg database by that attribute it would be part of egg entity, hence you would put it into egg service.



                                                              Rule of thumb: it's okay as long as it's an attribute.



                                                              In that way you can have both methods in two different services.



                                                              This also applies to multilayered attributes: it depends on the entity you use as base



                                                              Which should you use? As a rule of thumb, use top to bottom approach: always go for parent when you need the child






                                                              share|improve this answer

























                                                                up vote
                                                                0
                                                                down vote













                                                                You retrieve attribute of the entity, so it's part of the nest. No brainer.



                                                                Now, if you had attribute birdnestid and you filtered your egg database by that attribute it would be part of egg entity, hence you would put it into egg service.



                                                                Rule of thumb: it's okay as long as it's an attribute.



                                                                In that way you can have both methods in two different services.



                                                                This also applies to multilayered attributes: it depends on the entity you use as base



                                                                Which should you use? As a rule of thumb, use top to bottom approach: always go for parent when you need the child






                                                                share|improve this answer























                                                                  up vote
                                                                  0
                                                                  down vote










                                                                  up vote
                                                                  0
                                                                  down vote









                                                                  You retrieve attribute of the entity, so it's part of the nest. No brainer.



                                                                  Now, if you had attribute birdnestid and you filtered your egg database by that attribute it would be part of egg entity, hence you would put it into egg service.



                                                                  Rule of thumb: it's okay as long as it's an attribute.



                                                                  In that way you can have both methods in two different services.



                                                                  This also applies to multilayered attributes: it depends on the entity you use as base



                                                                  Which should you use? As a rule of thumb, use top to bottom approach: always go for parent when you need the child






                                                                  share|improve this answer












                                                                  You retrieve attribute of the entity, so it's part of the nest. No brainer.



                                                                  Now, if you had attribute birdnestid and you filtered your egg database by that attribute it would be part of egg entity, hence you would put it into egg service.



                                                                  Rule of thumb: it's okay as long as it's an attribute.



                                                                  In that way you can have both methods in two different services.



                                                                  This also applies to multilayered attributes: it depends on the entity you use as base



                                                                  Which should you use? As a rule of thumb, use top to bottom approach: always go for parent when you need the child







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Nov 18 at 11:47









                                                                  Sarief

                                                                  385317




                                                                  385317






























                                                                      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.





                                                                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                      Please pay close attention to the following guidance:


                                                                      • 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%2f53219661%2fwhich-service-class-do-methods-retrieving-lists-of-nested-objects-belong-to%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

                                                                      Guess what letter conforming each word

                                                                      Port of Spain

                                                                      Run scheduled task as local user group (not BUILTIN)