Best practice: how to implement many-to-many relations using Domain Models without framework
Hello stackoverflow members,
I'm using pure PHP to implement a Domain Model. I have two entities, entity dog
and entity human
which acts as the owner of the dog.
This is a many-to-many relationship. A owner
can have more than one dog
and a dog
is owned by many humans
(let's say the dog belongs to a family or a couple).
I have a database table for dog
and human
and also the n:n connection table. I have the two entities which are POPOs. And I also have two repositories, one for dog
and one for human
. These repositories have the crud operations and are responible for the database queries.
Which repository is responsible for managing the n:n table?
Example:
entity dog
has a property array with all connected human
's and vice versa. If i create a new human
like: $human = new Human('name');
and let him own an already existing dog
like: $human->addDog($dog);
Who is responsible for the n:n connection?
I can do $dogRepo->Update($human->getDogs()[0]);
to update the dog in DB or I can do $humanRepo->Insert($human)
. Should the DogRepo also insert the new human
(utilizing the HumanRepo)? Should the HumanRepo also update the dog
(utilizing the DogRepo). Or is the business logic responsible for this? (call insertHuman()
and updateDog()
seperately from the business logic).
On all of this approaches I don't know which repository is responible for the n:n table?
Sound like a common question but i couldn't find a suitable solution on the web.
Thanks in advance!
php database domain-driven-design
add a comment |
Hello stackoverflow members,
I'm using pure PHP to implement a Domain Model. I have two entities, entity dog
and entity human
which acts as the owner of the dog.
This is a many-to-many relationship. A owner
can have more than one dog
and a dog
is owned by many humans
(let's say the dog belongs to a family or a couple).
I have a database table for dog
and human
and also the n:n connection table. I have the two entities which are POPOs. And I also have two repositories, one for dog
and one for human
. These repositories have the crud operations and are responible for the database queries.
Which repository is responsible for managing the n:n table?
Example:
entity dog
has a property array with all connected human
's and vice versa. If i create a new human
like: $human = new Human('name');
and let him own an already existing dog
like: $human->addDog($dog);
Who is responsible for the n:n connection?
I can do $dogRepo->Update($human->getDogs()[0]);
to update the dog in DB or I can do $humanRepo->Insert($human)
. Should the DogRepo also insert the new human
(utilizing the HumanRepo)? Should the HumanRepo also update the dog
(utilizing the DogRepo). Or is the business logic responsible for this? (call insertHuman()
and updateDog()
seperately from the business logic).
On all of this approaches I don't know which repository is responible for the n:n table?
Sound like a common question but i couldn't find a suitable solution on the web.
Thanks in advance!
php database domain-driven-design
add a comment |
Hello stackoverflow members,
I'm using pure PHP to implement a Domain Model. I have two entities, entity dog
and entity human
which acts as the owner of the dog.
This is a many-to-many relationship. A owner
can have more than one dog
and a dog
is owned by many humans
(let's say the dog belongs to a family or a couple).
I have a database table for dog
and human
and also the n:n connection table. I have the two entities which are POPOs. And I also have two repositories, one for dog
and one for human
. These repositories have the crud operations and are responible for the database queries.
Which repository is responsible for managing the n:n table?
Example:
entity dog
has a property array with all connected human
's and vice versa. If i create a new human
like: $human = new Human('name');
and let him own an already existing dog
like: $human->addDog($dog);
Who is responsible for the n:n connection?
I can do $dogRepo->Update($human->getDogs()[0]);
to update the dog in DB or I can do $humanRepo->Insert($human)
. Should the DogRepo also insert the new human
(utilizing the HumanRepo)? Should the HumanRepo also update the dog
(utilizing the DogRepo). Or is the business logic responsible for this? (call insertHuman()
and updateDog()
seperately from the business logic).
On all of this approaches I don't know which repository is responible for the n:n table?
Sound like a common question but i couldn't find a suitable solution on the web.
Thanks in advance!
php database domain-driven-design
Hello stackoverflow members,
I'm using pure PHP to implement a Domain Model. I have two entities, entity dog
and entity human
which acts as the owner of the dog.
This is a many-to-many relationship. A owner
can have more than one dog
and a dog
is owned by many humans
(let's say the dog belongs to a family or a couple).
I have a database table for dog
and human
and also the n:n connection table. I have the two entities which are POPOs. And I also have two repositories, one for dog
and one for human
. These repositories have the crud operations and are responible for the database queries.
Which repository is responsible for managing the n:n table?
Example:
entity dog
has a property array with all connected human
's and vice versa. If i create a new human
like: $human = new Human('name');
and let him own an already existing dog
like: $human->addDog($dog);
Who is responsible for the n:n connection?
I can do $dogRepo->Update($human->getDogs()[0]);
to update the dog in DB or I can do $humanRepo->Insert($human)
. Should the DogRepo also insert the new human
(utilizing the HumanRepo)? Should the HumanRepo also update the dog
(utilizing the DogRepo). Or is the business logic responsible for this? (call insertHuman()
and updateDog()
seperately from the business logic).
On all of this approaches I don't know which repository is responible for the n:n table?
Sound like a common question but i couldn't find a suitable solution on the web.
Thanks in advance!
php database domain-driven-design
php database domain-driven-design
asked Nov 21 '18 at 9:20
user3796786user3796786
104311
104311
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In DDD we design our Aggregates based on the business rules. Each Aggregate protects its own invariants. There are no tables or relation tables in our head when we design them, just invariants and consistency requirements.
That being said, you are having difficulties because you haven't identified the invariants or there are none.
Does the human that own a dog behave in some particular way? Are some human commands rejected if the Human doesn't/does own a Dog ? Then you add to the Human
Aggregate a list of owned dog ID's (and not dog instances!).
Does the dog that is owned by a human behave differently from a dog that has no master? Then add to the Dog
Aggregate a list of owner IDs.
Or is this relation between them shown only in the UI? Then add it as a separate Aggregate, i.e. DogOwnershipByAHuman(id, dogId, humanId)
to not pollute the Dog
or Human
Aggregates with data that they don't need.
add a comment |
Both repositories should only change their corresponding tables, since this is their main functionality (following the Single responsibility principle
). These repositories would be wrapped by a Service
(a wrapper class which has utility functions for managing relations), where the service would be responsible for altering both databases correctly. So this service could have a function like assignDogToHuman($humanId, $dogId)
, where this function would call both repositories and change their data accordingly.
By doing this you make sure that the repositories have one responsibility (updating their own table) and the service has the responsibility of managing the relations between dogs
and humans
.
This also provides you with the added benefit that the repository layer is now replaceable. I.e. if you ever decide to change the database (for example from Mysql to MongoDB), only the repositories have to be swapped/updated since the implementation of the functions called by assignDogToHuman
should stay the same and for this service it does not matter what the underlying database is.
I hope this helps you, if you have any questions feel free to ask
add a comment |
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
});
}
});
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%2f53408757%2fbest-practice-how-to-implement-many-to-many-relations-using-domain-models-witho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In DDD we design our Aggregates based on the business rules. Each Aggregate protects its own invariants. There are no tables or relation tables in our head when we design them, just invariants and consistency requirements.
That being said, you are having difficulties because you haven't identified the invariants or there are none.
Does the human that own a dog behave in some particular way? Are some human commands rejected if the Human doesn't/does own a Dog ? Then you add to the Human
Aggregate a list of owned dog ID's (and not dog instances!).
Does the dog that is owned by a human behave differently from a dog that has no master? Then add to the Dog
Aggregate a list of owner IDs.
Or is this relation between them shown only in the UI? Then add it as a separate Aggregate, i.e. DogOwnershipByAHuman(id, dogId, humanId)
to not pollute the Dog
or Human
Aggregates with data that they don't need.
add a comment |
In DDD we design our Aggregates based on the business rules. Each Aggregate protects its own invariants. There are no tables or relation tables in our head when we design them, just invariants and consistency requirements.
That being said, you are having difficulties because you haven't identified the invariants or there are none.
Does the human that own a dog behave in some particular way? Are some human commands rejected if the Human doesn't/does own a Dog ? Then you add to the Human
Aggregate a list of owned dog ID's (and not dog instances!).
Does the dog that is owned by a human behave differently from a dog that has no master? Then add to the Dog
Aggregate a list of owner IDs.
Or is this relation between them shown only in the UI? Then add it as a separate Aggregate, i.e. DogOwnershipByAHuman(id, dogId, humanId)
to not pollute the Dog
or Human
Aggregates with data that they don't need.
add a comment |
In DDD we design our Aggregates based on the business rules. Each Aggregate protects its own invariants. There are no tables or relation tables in our head when we design them, just invariants and consistency requirements.
That being said, you are having difficulties because you haven't identified the invariants or there are none.
Does the human that own a dog behave in some particular way? Are some human commands rejected if the Human doesn't/does own a Dog ? Then you add to the Human
Aggregate a list of owned dog ID's (and not dog instances!).
Does the dog that is owned by a human behave differently from a dog that has no master? Then add to the Dog
Aggregate a list of owner IDs.
Or is this relation between them shown only in the UI? Then add it as a separate Aggregate, i.e. DogOwnershipByAHuman(id, dogId, humanId)
to not pollute the Dog
or Human
Aggregates with data that they don't need.
In DDD we design our Aggregates based on the business rules. Each Aggregate protects its own invariants. There are no tables or relation tables in our head when we design them, just invariants and consistency requirements.
That being said, you are having difficulties because you haven't identified the invariants or there are none.
Does the human that own a dog behave in some particular way? Are some human commands rejected if the Human doesn't/does own a Dog ? Then you add to the Human
Aggregate a list of owned dog ID's (and not dog instances!).
Does the dog that is owned by a human behave differently from a dog that has no master? Then add to the Dog
Aggregate a list of owner IDs.
Or is this relation between them shown only in the UI? Then add it as a separate Aggregate, i.e. DogOwnershipByAHuman(id, dogId, humanId)
to not pollute the Dog
or Human
Aggregates with data that they don't need.
answered Nov 21 '18 at 9:54
Constantin GalbenuConstantin Galbenu
11.3k21531
11.3k21531
add a comment |
add a comment |
Both repositories should only change their corresponding tables, since this is their main functionality (following the Single responsibility principle
). These repositories would be wrapped by a Service
(a wrapper class which has utility functions for managing relations), where the service would be responsible for altering both databases correctly. So this service could have a function like assignDogToHuman($humanId, $dogId)
, where this function would call both repositories and change their data accordingly.
By doing this you make sure that the repositories have one responsibility (updating their own table) and the service has the responsibility of managing the relations between dogs
and humans
.
This also provides you with the added benefit that the repository layer is now replaceable. I.e. if you ever decide to change the database (for example from Mysql to MongoDB), only the repositories have to be swapped/updated since the implementation of the functions called by assignDogToHuman
should stay the same and for this service it does not matter what the underlying database is.
I hope this helps you, if you have any questions feel free to ask
add a comment |
Both repositories should only change their corresponding tables, since this is their main functionality (following the Single responsibility principle
). These repositories would be wrapped by a Service
(a wrapper class which has utility functions for managing relations), where the service would be responsible for altering both databases correctly. So this service could have a function like assignDogToHuman($humanId, $dogId)
, where this function would call both repositories and change their data accordingly.
By doing this you make sure that the repositories have one responsibility (updating their own table) and the service has the responsibility of managing the relations between dogs
and humans
.
This also provides you with the added benefit that the repository layer is now replaceable. I.e. if you ever decide to change the database (for example from Mysql to MongoDB), only the repositories have to be swapped/updated since the implementation of the functions called by assignDogToHuman
should stay the same and for this service it does not matter what the underlying database is.
I hope this helps you, if you have any questions feel free to ask
add a comment |
Both repositories should only change their corresponding tables, since this is their main functionality (following the Single responsibility principle
). These repositories would be wrapped by a Service
(a wrapper class which has utility functions for managing relations), where the service would be responsible for altering both databases correctly. So this service could have a function like assignDogToHuman($humanId, $dogId)
, where this function would call both repositories and change their data accordingly.
By doing this you make sure that the repositories have one responsibility (updating their own table) and the service has the responsibility of managing the relations between dogs
and humans
.
This also provides you with the added benefit that the repository layer is now replaceable. I.e. if you ever decide to change the database (for example from Mysql to MongoDB), only the repositories have to be swapped/updated since the implementation of the functions called by assignDogToHuman
should stay the same and for this service it does not matter what the underlying database is.
I hope this helps you, if you have any questions feel free to ask
Both repositories should only change their corresponding tables, since this is their main functionality (following the Single responsibility principle
). These repositories would be wrapped by a Service
(a wrapper class which has utility functions for managing relations), where the service would be responsible for altering both databases correctly. So this service could have a function like assignDogToHuman($humanId, $dogId)
, where this function would call both repositories and change their data accordingly.
By doing this you make sure that the repositories have one responsibility (updating their own table) and the service has the responsibility of managing the relations between dogs
and humans
.
This also provides you with the added benefit that the repository layer is now replaceable. I.e. if you ever decide to change the database (for example from Mysql to MongoDB), only the repositories have to be swapped/updated since the implementation of the functions called by assignDogToHuman
should stay the same and for this service it does not matter what the underlying database is.
I hope this helps you, if you have any questions feel free to ask
answered Nov 21 '18 at 9:39
Sven HakvoortSven Hakvoort
2,1602621
2,1602621
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.
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%2f53408757%2fbest-practice-how-to-implement-many-to-many-relations-using-domain-models-witho%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