java


DDD - Repository and many-to-many relationship


I am new to DDD and I am stuck in here:
I have a many-to-many relationship between two entities: User and Group. THe relationship is not an Aggregate because a User can exists without a Group and a Group can exists without a User.
THis is my code for User class:
public class User {
private List<Group> groups = new ArrayList<Group>();
private UserRepository userRepository;
public void create() throws Exception{
userRepository.create(this);
// I have to update the groups with the user.
for (Group group : groups) {
group.update();
}
}
public void addGroup(Group group){
if (!groups.contains(group)) {
groups.add(group);
group.addUser(this);
}
}
}
The problem is that I dont know where to associate those classes when I create a User which has groups in (I cannot use ORM). I made it in the create method of User, and also manage transactions there through Spring. Is this correct? Or should I put that code in the userRepository or in a Service?
Thanks!
I made it in the create method of User, and also manage transactions there through Spring. Is this correct?
Correctness of this logic would depend on how the repository is written and it's contract. If the repository will not persist any relationships, then it could be a sign of a poorly designed repository. I would expect a repository to implement the necessary logic to store entity associations as well, and not just the simple properties of the entity.
A repository based on an ORM solution would have made this easier by through the cascading of persistence events, but in your case, it might lead to duplication of code in both your repositories (I am assuming that you have a GroupRepository as well). It might be better if you decide on which entity owns the relationship, and then have the association persisted by the corresponding repository. Any changes in the association should therefore be done only by the entity that owns the association (it appears to be User in your case, and by inference the UserRepository should manage the association).
Lucas, I would:
1) Create a User factory that can take in the Groups that should be associated with the new User. The factory can use the addGroup method to make that association.
2) Remove your reference to the UserRepository from the User class.
According Eric Evans' book, many to many associations make your model too complex. Try to replace them with
2 many-to-one associations. One part of many-to-many association always has more
business value than other.
In your case I would stay with 2 aggregates - User and Group, where User aggregate is responsible for holding bunch of GroupReferences.

Related Links

Use of the String(String) constructor in Java [duplicate]
Best XML format for log events in terms of tool support for data mining and visualization?
ParameterizedTest with a name in Eclipse Testrunner
Java Client to connect to Server with Openssl and Client Auth
Java: In what order are the methods called in an Applet?
Hibernate 3.4 with slf4j and log4j
Class method and variable with same name, compile error in C++ not in Java?
Streaming files over the network with random access - java
Pure lightweight application in java
jsp utf encoding
How can I see if an element in an int array is empty?
Chosing a suitable table size for a Hash
How to tune Tomcat 5.5 JVM Memory settings without using the configuration program
How to avoid, that URL.equals needs access to the internet in Java?
Java - get the newest file in a directory?
Am I missing something, or do varargs break Arrays.asList?

Categories

HOME
image
deployment
cookies
minimum-spanning-tree
comparison
relayjs
vmware
alfresco
kalman-filter
gis
ravendb
in-app-purchase
multiple-records
kibana-4
izpack
handsontable
modx-revolution
modelica
orchardcms
immutable.js
swiftlint
zapier
icloud-api
dxf
apache-commons-io
django-storage
saas
large-file-upload
c++-amp
procdump
sql-server-2012-express
javascriptcore
preg-match-all
openoffice.org
xmlreader
accelerate-framework
cloud-code
ios5
simplexml
multilingual
leading-zero
sas-visual-analytics
eclipse-scout
android-mediaprojection
sas-jmp
user-accounts
atomicity
rails-routing
ionicons
yii2-extension
jxcore
nodebb
spring-mongodb
dynamics-sl
django-scheduler
cubes
parallel-data-warehouse
fputcsv
nessus
associative-array
eclipse-clp
computer-algebra-systems
dereference
gui-test-framework
rebol3
algebraic-data-types
industrial
runtime.exec
buster.js
flashvars
specification-pattern
isnullorempty
broken-links
armcc
jspinclude
ticoredatasync
wse3.0
work-stealing
data-loss
cinema-4d
xfbml
procedural-music

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App