last day (14 days later) » 

2:21 PM
1
Q: How should I materialize DDD entities with private setters from the persistence layer?

jnm2I'm just starting to figure out DDD. Anemic domain models (that I learn were actually no more than persistence models) and pushing logic into the view models just wasn't cutting it. So I've modeled one of the more complex contexts (a feed nutrition/price optimizer). I'm totally on board with the ...

 
The first 3 points are really more feelings than precisely defined obstacles. 4) is admittedly better in NHibernate than it is in EF. The amount of effort needed to alleviate those problems (see answer below) is so big though that I personally don't bother and accept the slight toll EF takes on my entities. It is anyway infinitely better now with Code First + Fluent mapping than it was in the early versions.
"I also tried reconstructing and replaying the method calls which would cause the entity to be in an equivalent state" - this looks a lot like Event Sourcing, another elegant way of (not having to) solve the O/R mismatch problem. Maybe you should look into that.
 
How would I overcome obstacle 3 with EF, where the database is less normalized than the model? I like the idea of event sourcing but I am not ready to revamp the database.
 
Can you please detail "the database is less normalized than the model" (normalization isn't a very relevant concept in the OO world) and give a concrete example of how this hampers you ?
 
For example, the database has a Formulas table with a State field (proposed, approved, transmitted). The domain model has FormulaEntry with two fields, State and Formula. FormulaEntry has timeline-state-specific methods on it which are not a concern of Formula. Formula has its own set of complicated behaviors and supports unsaved what-if scenarios. To map from the database, I need to unflatten each row from the Formulas table into a FormulaEntry. Does that make sense?
 
Not really. Are you saying that FormulaEntry is at higher aggregation level than Formula ? Is this for read-only reporting purposes or can you actually modify a Formula through its FormulaEntry and if so how (functionally speaking) ?
 
2:21 PM
Yes, FormulaEntry is higher. No, it's only for behavior purposes. Formulas can be modified while belonging to a FormulaEntry but also on their own. Did I model it wrong? Comments are getting long, should we chat?
FeedTimeline is the aggregate root. It has a collection of FormulaEntries. FeedTimeline has methods AddProposedFormula, ApproveLastFormula, and UnapproveLastFormula. FormulaEntry holds the state (approved, unapproved, transmitted) and has a Transmit method.
Formula itself has SetTraitMin, SetTraitMax, SetTraitIdeal, SetIngredientMin, SetIngredientMax, CalculateOptimal.
FeedTimeline has many other things like notes and performance graphs.
 
2:41 PM
Maybe I'm mixing two separate contexts here- the timeline process, and the formula creation itself.
 
2:57 PM
I see
if FeedTimeLine is the AR, only it should have a Repository
Now the Repository will have a reference to the EF DbContext as usual
And it can fetch the Formula's from there and attach them to the (non persistent if I get you well) FormulaEntry's and ultimately FeedTimeline
if you modify Formula objects in any way, EF will know about it
does that make sense ?
The "Formulas can be modified while belonging to a FormulaEntry but also on their own" is strange though.
All changes are supposed to go through the aggregate the entity is located in
If you feel like you need to change one without affecting the other, they might be 2 separate AR's
 

last day (14 days later) »