Wednesday, 23 November 2016

DDD - Events

The act of something happening is one of the most crucial aspects of implementing Domain Driven Design (DDD). I missed the importance of domain events when first exploring DDD.

Why

Most requirements come in the form when something happens, do this. Something in this case would be an action, and this would be the result taken afterwards. Most domain events can be discovered when requirements use this sort of language.

Another important consideration is that most requirements are evolutionary. They are often added as the feature is developed. What may start off as a single piece of behaviour, may evolve into something much more complex. Events allow this evolution in a decoupled manner.

Example

When a blog post is published, update the authors statistics. In code this may have a signature similar to:

The publish method is responsible for the publishing of the post. This entity holds responsibility for the pre-conditions and post conditions of such action. Also the method takes a domain service that will update the authors statistics as this is not the responsibility of the Post entity itself.

A new requirement may be to automatically send out a tweet with the post title and description. Without events this could be added in a similar manner.

Again the service will do the right thing once invoked, in this case send a tweet out. As you can see we could repeat this sort of enhancement over and over. While this does indeed complete the functionality that the business requires, the solution is far from elegant. A much better solution is to rely upon domain events.

Solution

The difference here is the publish method does nothing other than its internal logic. However it does publish (raise) an event to indicate a post has been published. Subscribers (listeners) to this event can then perform their corresponding actions.

Using the previous example two subscribers would be configured to send tweets and update author statistics. Each of these subscribers (handlers) would run in process by default, so their internal implementation should be as simple as possible. In other words record the request, and process this in the background. The code to raise the event is relatively simple, and can simply forward to any registered subscribers based upon a type. Any failure should not cause the publish to fail. Alternatively external subscribers could also handle this event, though this implementation would require the use of resilient and durable storage such as message queues or databases.

Ultimately domain events allow for extremely loosely coupled code, that is open for extension. Each handler can be developed and tested in isolation. The use of composition means that new features should become easy additions, with low risk.

One aspect that may stand out is that the use of this pattern uses a static class to publish events. While in most cases this would be poor for testing, this is not the case here. For tests prior to each step executing you can simply clear any registered handlers and configure what is required. If no handlers are configured, then nothing occurs. Also test handlers that simply report that fact a message has been raised are more than adequate.

Downsides

While this refactored example is loosely coupled, and open for extension, the intent of what happens after a publish is somewhat lost. Before it was clearer to see what the Publish method would do. This is a trade off, though the pros outweigh the cons here. Most IDE's have a way of showing you the use of all types, so we could easily see any handlers that consume the PostPublishedEvent.

Even with IDE/editor support, the loosely coupled nature of Domain Events can be tricky to debug at runtime. For example I once accidentally configured a game engine to handle events triggered from player movement. This meant that each frame of the game executed the collision detection algorithm twice, instead of once. Without a clear audit of what handlers are being executed upon what events, the use of domain events can be tricky to debug.

Lessons

  • Domain Events are a key area of DDD.
  • Use events to write loosely coupled code.
  • Ensure you have a method of auditing with handlers respond to which events.

Tuesday, 8 November 2016

POODR Highlights Part 2

Two other stand out topics from POODR were the use of tests and inheritance. The first set of higlights covered dependencies and arguments.

Tests

A conclusion that I agree with is that in general most programmers write too many tests.. A great quote in the book sees tests (as) the canary in the coal mine; when the design is bad, testing is hard. Sadly too many poor tests are often written. Examples such as property or construction tests, framework tests or tests that are coupled to the implementation are all common problems. Instead we should aim to get better and more value out of our tests by writing fewer of them, but of higher quality. In short test everything once and only in the proper place. A first step is to simply focus on the ROI that tests give, and focus on the high risk areas.

The test categories are broken down into two core types of tests.

  • Incoming Public Messages (public API)
  • Outgoing Public Messages (To public API of another object)

State based tests should be used for incoming public messages. While verification based tests should be used for outgoing public messages as the state is tested on the receiver, elsewhere. The distinction between commands and queries is also highlighted. In summary incoming messages should be tested for the state they return. Outgoing commands should be tested to ensure they get sent. Outgoing query messages should not be tested, merely stubbed.

These testing rules are nothing new, but the summary and importance of following these guidelines is nicely summarized within the chapter covering testing principles.

Inheritance

Inheritance is widely abused and misunderstood. Either inheritance is the solution for all problems, or you're advised to never use inheritance. POODR takes a more pragmatic approach. Inheritance is a tool that can sometimes provide an excellent solution, however you are better off duplicating code and defer such decisions until you know more.

The wrong abstraction is harder to work with than duplicated code as duplication can easily be removed. A bad abstraction that is used in many places is much harder however. The application of the Rule of Three can help here.

Lessons

  • Tests are hard - write less but focus on the quality.
  • Minimize the number of tests you write by using boundaries via incoming/outgoing messages.
  • Inheritance is not all bad.
  • Defer or hold back using inheritance until you understand the problem.