Wednesday, April 11, 2012

Accessing Session in Domain Object

We use event sourcing in our app and also have a strict need to track the user who initiated changes to many of our objects. Currently we have code like this



class Order { 
setNameBy(newname, User user) {
applyChange(new OrderRenamed(user.id, newname));
}
:
}


Since most of our methods are like this and all of them are called like this



setNameBy("a new name", SessionContext.currentUser)


we where contemplating accessing the SessionContext inside the domain object. i.e:



setNameBy(newname, User user) {
applyChange(new OrderRenamed(user.id, newname));
}


becomes



setName(newname) {
applyChange(new OrderRenamed(SessionContext.currenUser.id, newname));
}


I personally prefer the later method signature as it seams more natural on the other hand it feels a bit messy to access the SessionContext inside the Domain object.



So how do you best handle Session data like this in DDD/CQRS apps ?. Is it OK to access the SessionContext in the Domain objects or should I use other methods like event enrichment to add this information to the events emitted from the domain ?.





No comments:

Post a Comment