Explicit state modeling

695 views
Skip to first unread message

Alexandre Potvin Latreille

unread,
Aug 11, 2015, 3:58:16 PM8/11/15
to DDD/CQRS
I've read Patterns, Principles and Practices of Domain-Driven Design and came across a pretty interesting idea on how to model AR state transitions, but I'm not really convinced of the approach yet and wanted to gather more opinions on the subject.

Basically, the author suggests to model a single concrete class per state which allows to expose a different contract for every state, rather than using the state pattern or any type of state machine.

Nils Kilden-Pedersen

unread,
Aug 11, 2015, 7:10:14 PM8/11/15
to ddd...@googlegroups.com
This is a good general pattern, for things that resemble state machines. However, your typical AR is not a state machine.

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexandre Potvin Latreille

unread,
Aug 31, 2015, 3:29:55 PM8/31/15
to DDD/CQRS
Any other feedback? I'd like to hear more about the challenges faced when using this approach (e.g. using an ORM like Hibernate). Thanks!

Greg Young

unread,
Aug 31, 2015, 3:45:48 PM8/31/15
to ddd...@googlegroups.com
http://codebetter.com/gregyoung/2010/03/09/state-pattern-misuse/
--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Studying for the Turing test

@yreynhout

unread,
Aug 31, 2015, 4:34:38 PM8/31/15
to DDD/CQRS
Euh, I beg to differ - lifecycles are most often representable as hierarchic and concurrent statemachines.

Alexandre Potvin Latreille

unread,
Aug 31, 2015, 5:23:50 PM8/31/15
to DDD/CQRS
@Greg What puzzles me a bit is how to effectively handle persistence, especially when using an ORM such as Hibernate which already manages dirty checking and has it's own built-in identity map. Event with event sourcing, how would you deal with persistence and rehydration? You couldn't re-apply all the state transition events to the same AR instance since there's no way for an object to change it's runtime type in most languages. Would you have multiple event streams?

@yreynhout Yeah, state machines is what I've always seen (with or without the state pattern), but explicit state modeling still looks appealing. I'm just not sure if it really is truly practical just yet...


On Monday, August 31, 2015 at 3:45:48 PM UTC-4, Greg Young wrote:
http://codebetter.com/gregyoung/2010/03/09/state-pattern-misuse/

On Tuesday, August 11, 2015, Alexandre Potvin Latreille <alexandre.pot...@gmail.com> wrote:
I've read Patterns, Principles and Practices of Domain-Driven Design and came across a pretty interesting idea on how to model AR state transitions, but I'm not really convinced of the approach yet and wanted to gather more opinions on the subject.

Basically, the author suggests to model a single concrete class per state which allows to expose a different contract for every state, rather than using the state pattern or any type of state machine.

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

@yreynhout

unread,
Sep 4, 2015, 6:08:25 PM9/4/15
to DDD/CQRS
You could do the rehydration behind the repository facade and select the appropriate "class" to replay in based on the stream - only if you can make it streaming with minimal buffering would this make sense.

Šarūnas Valaškevičius

unread,
Sep 8, 2015, 1:16:45 PM9/8/15
to ddd...@googlegroups.com
I think modelling state and its usage lead to more complicated code than it should be :)

I've tried to tackle the problem in a different way - http://www.hyperlambda.com/posts/eventflow-cqrs-es-in-scala/ 

It's still using state machines (each event flow being one), however, the state in all cases is local and explicit.

Please let me know what you think?

Regards,
Sarunas

To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Studying for the Turing test

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.

Alexandre Potvin Latreille

unread,
Nov 16, 2015, 8:49:04 AM11/16/15
to DDD/CQRS
*BUMP* for Greg... regarding your article, could you expand a bit more on how this would work regarding persistence (using an ORM and using an Event Store)? Will you use different repositories? Also, how do you handle state transitions at runtime considering an object cannot change it's type? Do you do something like arInNewState = areInOldState.transitionToNewState(); ?

Greg Young

unread,
Nov 16, 2015, 8:52:20 AM11/16/15
to ddd...@googlegroups.com
Why would you be using an ORM and an EventStore?

That article is independent of any storage mechanism.

"Also, how do you handle state transitions at runtime considering an
object cannot change it's type?"

Return a new type from the operation:

public class InProcessApplication {

CompletedApplication SubmitForApproval(ILoanProcessingService
processor) { … }

}

On Mon, Nov 16, 2015 at 3:49 PM, Alexandre Potvin Latreille
>>>> an email to dddcqrs+u...@googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>>>
>>> --
>>> Studying for the Turing test
>>>
> --
> You received this message because you are subscribed to the Google Groups
> "DDD/CQRS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to dddcqrs+u...@googlegroups.com.

Alexandre Potvin Latreille

unread,
Nov 16, 2015, 10:07:41 AM11/16/15
to DDD/CQRS
Sorry I meant ORM OR an Event Store. I understand the pattern is independent of the storage mechanism, but I believe the implementation will be very different. For instance how would you deal with ORMs that uses an internal identity map to perform dirty checking (Hibernate)? Would you have one repository per concrete state representation?

E.g.

inProcessApplication = inProcessApplicationRepository.findById(someId);
completedApplication = inProcessApplication.submitForApproval(...); //does not mutate inProcessApplication
completedApplicationRepository.add(completedApplication);

If not, how would you model a single repository that handles multiple concrete classes?

Kijana Woodard

unread,
Nov 16, 2015, 10:25:08 AM11/16/15
to ddd...@googlegroups.com
By "repository", do you mean the "repository pattern"?

If so, I'd say that demonstrates a weakness in layering patterns. The ORM is already providing relevant patterns for persistence. Putting a repository between the ORM and your domain is causing strife with no upside.

Model operations that happen to implemented using an ORM.

Alexandre Potvin Latreille

unread,
Nov 25, 2016, 2:33:28 PM11/25/16
to DDD/CQRS
@Greg Young Do you have code examples of explicit state modeling using event sourcing? Seeing an example would probably answer many questions I have.

The most important ones are:

1. How are we supposed to rehydrate the aggregate in the correct state class from the repository given that in order to know in which state the aggregate is you first have to go through the list of events (unless you keep a snapshot of the status somewhere -- which adds complexity).

2. How do you prevent transitioning to the same state multiple times considering that the new state is only returned and the initial AR instance is not mutated (unless I got that wrong)?

E.g.
      publishedArticle = draftArticle.publish();
      articleRepository.save(publishedArticle);
      publishedArcticle = draftArticle.publish();
      articleRepository.save(publishedArticle);

Alexandre Potvin Latreille

unread,
Apr 11, 2017, 6:47:42 PM4/11/17
to DDD/CQRS
*BUMP* ;)

Greg Young

unread,
Apr 11, 2017, 6:50:16 PM4/11/17
to ddd...@googlegroups.com
"@Greg Young Do you have code examples of explicit state modeling
using event sourcing? Seeing an example would probably answer many
questions I have."

There are already many out there including videos etc. Not sure what you want?

On Fri, Nov 25, 2016 at 7:33 PM, Alexandre Potvin Latreille
> Visit this group at https://groups.google.com/group/dddcqrs.

Alexandre Potvin Latreille

unread,
Apr 11, 2017, 7:10:33 PM4/11/17
to DDD/CQRS
Somehow I can't really find any of that content, perhaps just a few links to some videos and articles you are referring to?
Reply all
Reply to author
Forward
0 new messages