<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Nicholas Blumhardt : DDD</title><link>http://blogs.msdn.com/nblumhardt/archive/tags/DDD/default.aspx</link><description>Tags: DDD</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>State Machines in Domain Models</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/04/16/state-machines-in-domain-models.aspx</link><pubDate>Thu, 16 Apr 2009 09:52:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9552345</guid><dc:creator>niblumha</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9552345.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9552345</wfw:commentRss><description>&lt;p&gt;I was reminded today of &lt;a href="http://stateless.googlecode.com"&gt;Stateless&lt;/a&gt;, a little project that I’m quite fond of.&lt;/p&gt;  &lt;p&gt;Stateless is a &lt;em&gt;hierarchical state machine framework&lt;/em&gt; based on &lt;a href="http://codeplex.com/simplestatemachine"&gt;Simple State Machine&lt;/a&gt; for &lt;a href="http://boo.codehaus.org"&gt;Boo&lt;/a&gt;, but configured using C# 3.0.&lt;/p&gt;  &lt;p&gt;When I &lt;a href="http://ubik.com.au/article/named/stateless"&gt;announced Stateless last year&lt;/a&gt;, I hardly even explained its purpose, let alone its tongue-in-cheek name. I think I owe it a better start in life!&lt;/p&gt;  &lt;p&gt;If you’re doing heavy duty state-machine based programming (writing parsers, radiotherapy machines or flight control systems) then Stateless is probably not what you’re looking for.&lt;/p&gt;  &lt;p&gt;If you’re doing domain-driven design, and anxious about the ugly nested ‘if’ and ‘switch’ statements building up around _state variables, Stateless can help.&lt;/p&gt;  &lt;p&gt;State-based behaviour is awkward because despite careful programming, it is difficult to see how the states relate to each other. Adding and removing states is error-prone, repetition creeps in, and refactoring gets tricky.&lt;/p&gt;  &lt;p&gt;There’s more than one way out. The &lt;a href="http://en.wikipedia.org/wiki/State_pattern"&gt;State&lt;/a&gt; &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2006/04/30/143733.aspx"&gt;pattern&lt;/a&gt; can be one very elegant solution. &lt;/p&gt;  &lt;p&gt;Another solution that works nicely is to create a declarative model of the states, transitions and associated actions, and have a state machine framework ‘run’ it for you. This is the approach enabled by Stateless.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_2.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="551" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb.png" width="337" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The Telephone Call state chart is broken down into the following states and triggers:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_4.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="351" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_1.png" width="238" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can use any types to represent states and triggers, but enumerations are convenient.&lt;/p&gt;  &lt;p&gt;After creating an instance of &lt;em&gt;StateMachine&lt;/em&gt;, each state is configured independently. Configuration for a state revolves around the triggers that the state can accept, the transitions that these triggers will cause (to new states) and the actions that will be performed when entering or leaving a state.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_6.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="385" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_2.png" width="546" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Once the state machine has been configured, the triggers can be ‘fired’. The side-effects from firing triggers drive the program.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_8.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="46" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_3.png" width="300" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You might wonder how this fits into a domain model. &lt;em&gt;StateMachine&lt;/em&gt; certainly doesn’t belong on the public surface area of your domain model classes. It’s an implementation detail – like, for example, &lt;em&gt;StringBuilder &lt;/em&gt;or &lt;em&gt;Regex.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_10.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="281" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_4.png" width="438" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Users of the class interact with the state machine indirectly, by calling public methods on the domain object.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_12.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="178" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_5.png" width="349" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The state machine is configured to call two methods when entering and leaving the Connected state (see the configuration above.)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_14.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="197" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_6.png" width="452" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Stateless does all of the heavy lifting. For example, the state machine understands that because &lt;em&gt;OnHold&lt;/em&gt; is a sub-state of &lt;em&gt;Connected&lt;/em&gt;, the &lt;em&gt;Connected &lt;/em&gt;entry/exit actions aren’t called when moving &lt;em&gt;between&lt;/em&gt; these two states.&lt;/p&gt;  &lt;p&gt;Now for a justification of the silly name…&lt;/p&gt;  &lt;p&gt;Notice the way that the &lt;em&gt;_state&lt;/em&gt; field belongs to the &lt;em&gt;PhoneCall &lt;/em&gt;object, rather than the state machine? This allows the field to be easily mapped to a database column by NHibernate or your ORM of choice. The state machine reads and writes the &lt;em&gt;_state&lt;/em&gt; value using the pair of lambdas provided to its constructor. In that sense you can &lt;em&gt;almost &lt;/em&gt;say that the state machine itself is ‘stateless’. Almost.&lt;/p&gt;  &lt;p&gt;There’s some more basic documentation and a source download &lt;a href="http://code.google.com/p/stateless/"&gt;at the site&lt;/a&gt;. I hope you’ll try Stateless and be pleasantly surprised by how expressive it can make your code. Search your domain model for “State” or “Status” and see what Stateless can do!&lt;/p&gt;  &lt;p&gt;&lt;em&gt;This post also comes with a challenge: Stateless is fairly simple and very hackable - if you’re a fluent-interface aficionado and think you can improve the configuration API, join the project!&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9552345" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/DDD/default.aspx">DDD</category><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Stateless/default.aspx">Stateless</category></item></channel></rss>