Which service class do methods retrieving lists of nested objects belong to?
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
java design-patterns language-agnostic
edited Nov 14 at 14:41
Ali Soltani
6,3422829
6,3422829
asked Nov 9 at 3:56
noobcoder
1,3792923
1,3792923
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
9
down vote
accepted
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
.
add a comment |
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.
add a comment |
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)
.
add a comment |
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.
add a comment |
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.
add a comment |
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
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
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
.
add a comment |
up vote
9
down vote
accepted
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
.
add a comment |
up vote
9
down vote
accepted
up vote
9
down vote
accepted
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
.
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
.
answered Nov 9 at 5:10
Nghia Bui
1,443812
1,443812
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 9 at 4:09
Khalid Shah
1,127620
1,127620
add a comment |
add a comment |
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)
.
add a comment |
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)
.
add a comment |
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)
.
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)
.
answered Nov 16 at 17:24
ndueck
100111
100111
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 17 at 12:40
Sid Malani
1,73211011
1,73211011
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 18 at 4:10
K. M. Fazle Azim Babu
1264
1264
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 18 at 11:47
Sarief
385317
385317
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown