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

How to get exact index of selected item from filtered adapter?
how to share dependecy in programs
Can't detect existing phone number
How to convert file content to a string? [duplicate]
Delete RandomAccessFile from directory
Why does JUnitCore only return the first results?
Java Parsing Issue- How to Receive data from Server?
Java Finding String regex
Regex to check ColdFusion code: Is tag name lowercase? [closed]
Using java to get the google search results [duplicate]
My index parameter is not working [duplicate]
Does an OutputStreamWriter without buffering exist?
how to use single fragment for ViewPager Swipe Tabs in Java Android
String initialization with + operator java
authenticate member through database with hashed/salted password
Check if exists exactly one attribute of the node and return value of this attribute in XPAth

Categories

HOME
compiler-construction
openmp
plone
grep
jsrender
ebean
vault
fancybox-3
izpack
dax
etl
visual-studio-cordova
dynamics-crm-online
vaadin7
worldwind
after-effects
saxon
transformation
oxyplot
samoa
java-3d
fish
facebook-access-token
fgetcsv
buck
bpel
semantic-versioning
data-manipulation
grails-3.1
repo
android-ble
lto
swift3.0.2
core-plot
google-closure
nand2tetris
nomethoderror
turbogears
gpx
rainbowtable
return-value
nstextview
kbuild
feeds
typed-lambda-calculus
np-complete
dwscript
sonarlint-vs
objective-c-swift-bridge
instant
adxstudio-portals
qpid
react-native-listview
wapiti
freelancer.com-api
srand
wyam
riak-cs
nsight
geonetwork
block-device
winddk
asp.net-web-api-odata
wordpress-theme-customize
socketexception
googlemock
code-access-security
contenttype
mysql-error-1062
message-driven-bean
oam
ocunit
wsdl-2.0
labwindows
dotnethighcharts
nsdatecomponents
invite
appender
iphone-web-app
w3wp.exe
fluent-interface
ncqrs
iweb
javap
anti-piracy
port-number

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App