Posts tagged DDD

Events are not an implementation detail

When listening to many discussion about Domain-Driven Design and Event Sourcing, subjects which I am recently very into, sometimes I have a feeling that events are considered merely a way of persisting aggregates. People tend to discuss benefits events have with regards to storing historical data, having possibility to generate any read model out of events and so on. Reducing event sourcing to such ‘housekeeping’ duties is castrating your architecture.

This point of view is expressed in a way people model their event-based solutions: thinking first about the aggregates and then, within each aggregate, about events they generate. Sometimes the process is even more event-UNcentric when people first implement a command and then start thinking about what event types should be created.

Given the fact that most Event Sourcing practitioners have a background in DDD, we tend to bring our experience from working in data-centric environment to the event-centric world. There is, however, a fundamental difference between these two.

When building a behaviour-oriented DDD solution on top of a data-centric infrastructure (a database) you do your best to hide the database and its schema from the business layer. Over the years we learnt that it is better treated as implementation detail because it does not provide concepts useful to model behaviour. There concepts must be built on top of it. The underlying data layer must not be visible to the outside world because of it’s volatility — it has to change whenever the behaviour it supports needs to change.

On the other hand, when building a similar solution on top of event-centric infrastructure you can (and should) leverage events to model the behaviour. They (not aggregates or anything built on top of them) are the central concept here. Whenever business changes, new event types appear and some old are no longer generated but no event is ever lost. Whatever happened, happened.

Returning to my original thought, in an event-centric solution aggregates should not be treated as first-class citizens. Don’t get me wrong, they are very useful. In a healthy design probably a vast majority of events would be generated via aggregates, but some would not. Aggregates are priceless when it comes to modelling concurrency and to encapsulating business logic.

You may now want to ask a question when should you use aggregates and when not? The answer seems to be simple. Whenever your system needs to respond to some external activity, you just directly inject an event representing what have happened into your system. The idea is, again, whatever happened, happened. You can’t throw concurrency exception at The World saying it is wrong to raise this event right now. You have to humbly acknowledge the fact and record the information. On the other hand, when it is your system who conducted the activity to be communicated by an event, you should use an aggregate to have full control over how and when the event is published.

Have fun building event-centric solutions!

VN:F [1.9.13_1145]
Rating: 4.8/5 (4 votes cast)

Classic DDD example, renewed

I’ve spent some time recently renewing my old DDDSample.NET project. I understand that it is the projection of my subjective and uninformed view of Domain-Driven Design, but still I see a value in having some implementation compared to having nothing — it provokes discussion. That’s why I will continue to invest my time in it. If you happen to read this and want to take a look at the code, please remember that is is a sample (as the name suggests), not a guidance. The difference is following: a sample shows how I am doing things while a guidance would show how you are supposed to do things.

Journey through the stack trace

Classic version of DDDSample is a web application with simple UI created with ASP.NET MVC. This is where all starts. I would like to take you on a journey through the stack trace to explain various architectural decisions. Let’s register a new handling event.

NHibernateAmbientSessionManager

The very first class that gets a chance to process the request is NHibernateAmbientSessionManager. It is responsible for creating an ambient NHibernate session that will be bound to the request. After processing the request the session is released. It can be accessed anywhere in the code statically through CurrentSessionContext class. This is a built-in feature of NHibernate.

public void CreateAndBind()
{
    CurrentSessionContext.Bind(_sessionFactory.OpenSession());
}
public void UnbindAndDispose()
{
    var session = CurrentSessionContext.Unbind(_sessionFactory);
    if (session != null)
    {
        session.Dispose();
    }
}

HandlingController

Next is the HandlingController and it’s RegisterHandlingEvent method/action

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult RegisterHandlingEvent(string trackingId, DateTime? completionTime, string location, HandlingEventType type)
{
    if (!completionTime.HasValue)
    {
        ViewData.ModelState.AddModelError("completionTime", @"Event completion date is required and must be a valid date.");
    }
    if (!ViewData.ModelState.IsValid)
    {
        AddHandlingLocations();
        AddHandlingEventTypes();
        return View();
    }
    _handlingEventFacade.RegisterHandlingEvent(completionTime.Value, trackingId, location, type);
    return RedirectToAction("Track", "Tracking", new { trackingId });
}

There’s nothing fancy here, but is shows in a nice way that in this design, the responsibility of a controller is data validation and parsing (mostly done by MVC internals). After ensuring that data conforms to the required format, it is passed to another class which is…

HandlingFacade

This class hides the design of internals of the application from the UI. As far as controllers are concerned, business logic is implemented in façade’s methods. The truth is slightly different, however.

public void RegisterHandlingEvent(DateTime completionTime, string trackingId, string location, HandlingEventType type)
{
    var command = new RegisterHandlingEventCommand
                        {
                            CompletionTime = completionTime,
                            TrackingId = trackingId,
                            OccuranceLocation = location,
                            Type = type
                        };
    _pipelineFactory.Process(command);
}

All the façade is doing is packing the arguments into a command object and sending this command to the pipeline where it is processed. Why create a separate class instead of putting this method on the controller? I like my controllers to be concerned only by the UI and have only one reason to change.

NHibernateTransactionCommandFilter

Before the command is actually executed, a number of filters are invoked. I use a very simple command pattern implementation I’ve written a few weeks ago for the sole purpose of using in DDDSample. It allows me to define these filters in a nice (again, subjective view) way

public void OnHandling(object command)
{
    _ambientSession = CurrentSessionContext.Unbind(_sessionFactory);
    _session = _sessionFactory.OpenSession();
    _transaction = _session.BeginTransaction();
    CurrentSessionContext.Bind(_session);
}
public void OnHandled(object command, object result)
{
    CurrentSessionContext.Unbind(_sessionFactory);
    _transaction.Commit();
    _session.Close();
    CurrentSessionContext.Bind(_ambientSession);
}

Before handling a command an aforementioned ambient NHibernate session is unbound from the current context. We are not going to use it while processing the command because we need a clear workplace. We open a new session and a a new transaction. Whatever happens in the command handler, stays in the command handler. After a command succeeds we unbind the session it used and commit the transaction. Then we restore the ambient session so that UI can use it.

Why don’t we just use a transaction on this ambient session? There is a couple of reasons. First, there can be a transaction started before processing the command and we don’t want to take part in it. Second, we want to be absolutely sure we don’t have any entities in the session-level cache. Why? Because we might use some hints to fetch them more efficiently in context of command processing (like for example force eager load on some relations). And last but not least, if command fails we don’t want to break the session UI was using because it may need it for whatever reason after the command processing attempt failed.

The corollary of this is, we don’t want to put any persistent (tracked by NHibernate) into the command. If we allowed this, we could attach it by mistake to this other session and very strange things could happen. The side effect is we can safely serialize commands which allows us, if the need comes, to put our domain model on a separate tier then its client (the web application).

RegisterHandlingEventCommandHandler

When we have the new session and transaction in place it’s time to do the actual work. Here’s how a typical command handler look like

public object Handle(RegisterHandlingEventCommand command)
{
    var trackingId = new TrackingId(command.TrackingId);
    var cargo = _cargoRepository.Find(trackingId);
    var occuranceLocationUnLocode = new UnLocode(command.OccuranceLocation);
    var occuranceLocation = _locationRepository.Find(occuranceLocationUnLocode);
    var evnt = new HandlingEvent(command.Type, occuranceLocation, DateTime.Now, command.CompletionTime, cargo);
    _handlingEventRepository.Store(evnt);
    return null;
}

The idea here is to have only one method or constructor invocation on an Aggregate Root in one command. As you can see, command handler’s responsibility is to convert the raw data from the command to the proper Value Objects. The validation of data is performed when constructing VOs. In this case we create a new AR so we first instantiate a new object and then call repository to store it.

HandlingEventRepository

What happens in the repository?

public void Store(HandlingEvent handlingEvent)
{
    Session.Save(handlingEvent);
    _eventPublisher.Raise(handlingEvent);
}

Two things. First, we save the newly created AR to the command’s session. Then we use an Event Aggregator to publish this AR as an event. I based this approach on Eric Evans’ What I’ve learned about DDD since the book presentation where he explains that in many domains you encounter Aggregate Roots that are immutable. He calls them Events.

CargoWasHandlerEventHandler

The Event Aggregator publishes the event to all interested parties. In our case there is only one

public void Handle(HandlingEvent handlingEvent)
{
    handlingEvent.Cargo.DeriveDeliveryProgress(handlingEvent);
}

It calls a method on a Cargo Aggregate Root to notify it about the event. One thing worth notice here is we are doing it synchronously in the sample but our contract does not guarantee synchronicity. We write our event handlers in such a way that they can be executed asynchronously to the transaction that published the event. It is good to have such possibilities open in case we need to scale.

Cargo

This is how we reach the Cargo AR

public virtual void DeriveDeliveryProgress(HandlingEvent lastHandlingEvent)
{
    Delivery = Delivery.DerivedFrom(RouteSpecification, Itinerary, lastHandlingEvent);
}

The method looks very simple and it is not a coincidence. In classic DDD we have limited testing options since we can’t set our Entities to proper state easily (as easy in if we use Event Sourcing). That’s why we don’t want to test Entities. We want to test Value Objects because they are immutable and truly persistence ignorant. All we do in an Entity is pass the arguments to a VO and replace the current value with a new one. The probability that we make a bug in this one-liner is small, at least I hope so.

Delivery

Finally we arrived at our destination, the Value Object. Here I won’t include any code as it is quite complex and lengthy. I’ve already said probably the most important thing about VOs in this approach: they are immutable. This property allows us to test them easily. The compromise we are willing to accept in regards to Value Objects is, we expose their state via properties. Because we don’t have Command and Query Responsibility Separation, we use same objects (but remember, fetched via different session) to read and to write.

Summary

I hope you enjoyed this journey through the layers of the onion. Remember that this is just a sample. Most of its features were tested in the field but some combinations of them were not. I am eager to hear what you think about this approach and also to hear about your approach to DDD when size and/or complexity does not justify CQRS.

VN:F [1.9.13_1145]
Rating: 5.0/5 (2 votes cast)

Renewing DDDSample.Net, iteration 0

As I promised in one of the previous posts, I am now in process of renewing the DDDSample.Net source code. It’s been quite a while since I published the first version of it. Many technologies have changed and my take on Domain-Driven Design and software architecture also has changed slightly.

First of all, the repo was moved to github. Hurray! In the process of transforming TFS repository into a git one I lost the branch structure. Originally all versions were branched one from another which made updating all samples a little bit easier since (ideally) I had to change only one and then merge this change. With git I am thinking of a slightly different structure. I am pretty sure I can freeze some portions of the solution (like routing engine and most of the UI) and move them to separate repos. Then I can import them as submodules to each of the samples.

At first I just wanted to add new features to the existing samples but when I opened the vanilla version (a port of Java DDDSample) I found out that it is so outdated that it urgently needs to be renewed. The changes I made so far include:

  • Upgrading solution to VS2010 format
  • Removing the web setup project (I guess nobody used it, ever)
  • Switching to NuGet for resolving most of the packages (using don’t commit packages workflow described here by David Ebbo)
  • Upgrading some dependencies (like NHibernate) to the newest versions
  • Switching from using façade-like classes in application layer to commands and command handlers implemented using brand new tiny LeanCommandUnframework (a separate micro project hosted here on github)
  • Switching from Unity 1.2 to Autofac
  • Removing dependency on NServiceBus
  • Cleaning up the global.asax file

I am now quite happy with the solution. Everything seems to have its place in the architecture. There are some things yet to be clean up such as changing the way command handlers are created but these should be minor fixes. If you have some spare time, please clone the repo and check if you like what I did. And please note that only Vanilla version was updated.

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)

    .NET architecture samples

    A Domain-Driven Design sample I created some time ago, DDDSample.Net, is now a little bit rusty. I though it is a good idea to spend some time and make it up to date. It will involve, for sure, moving the project to github because you all know what I thing about TFS version control.

    Currently there are 4 major versions of DDDSample solution

    • Vanilla (‘by the book’)
    • CQRS with two relational databases
    • CQRS with event sourcing
    • Layered model

    I am thinking about dropping the second one as it was more like an experiment, not a final solution. The layered model is an advanced version of vanilla solution witch demonstrates how to structure larger domain models. Another thing I might do is migrate the event sourcing sample to NCQRS. Or maybe I should use EventStore directly? What do you think?

    Now the important part. I have some ideas what to implement next and I need your feedback. Help me rate them

    • Event driven SOA a’la Udi Dahan. This would be probably an extended version of layered model sample but with slightly different structuring approach (autonomous services instead of layers).
    • Vanilla with RavenDB as a data store
    • Native CQRS with event sourcing on RavenDB according to this description by Ayende.

    If you have some other ideas, post them in the comments. I am also thinking about getting rid of the installer projects. It does not make sense to just install the application in IIS and don’t look at the code. And if you open it, you can run it by hitting F5.

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)

    Domain-Driven Design by the book

    Starting a new project is fun. You (assuming you, reader, are The Architect) can make all the decision and do it right. You can use all the newest technologies you can find. And what do you do when it comes to designing business logic? Hopefully you base you decision on the level of complexity of the problem you are solving. If the problem is relatively easy, you implement a CRUD solution, right? On the other hand, if it looks really complex, you can enjoy having a good reason for CQRS architecture. But what’s between these two extremes? Enter (again) classic Domain-Driven Design by the book. I understand that in this CQRS noise you could forgot about this option. Let me remind you about architectural properties of such design.

    10.000 feet view

    The best way to describe such system is to use Jeffrey Palermo’s Onion Architecture approach. Domain model is the most inner layer of the onion. It contains your business logic and does not depend on anything significant (like WCF or NHibernate). The next layer of the onion would be something I usually call Application Layer. Notice that I skipped Domain Services Layer. It was intentionally. I consider domain services (if you really, really need to have them) part of the domain model. Application Layer is where your commands and command handlers live. For each business action there is a pair of a command and it’s handler. Trust me, making business operations explicit by implementing them as classes (as opposed to methods on a class/interface) pays off in the long run. If the application has a user interface, it is the outermost layer of the onion. If not, the Application Layer becomes the outermost one with it’s commands exposed via some remote interface (like WCF or NServiceBus).

    Repositories and data access

    You probably know I am a fan of repositories. I like the explicitness of their interface. In the architecture we are talking about you would probably use some kind of ORM (like my favourite NHibernate) so repositories would end up being a thin wrapper around the almighty mapper library. Using an ORM also implies that you have a Unit of Work in your solution. Most (probably all) of the major mappers have it implemented so it does not make any sense to not use it or write your own implementation.

    Aggregate boundaries

    The corollary of having a unit of work is the possibility of violating the aggregate boundaries. It needs discipline (and you, as The Architect, are the one who is responsible for maintaining it) to avoid transactions spanning multiple aggregates. Why is it so important? Because Aggregate Roots are usually used as concurrency units. Involving two concurrency units in a transactions can lead to all sorts of nasty things, including race conditions and deadlocks. There is one exception to this One Transaction-One Aggregate rule, namely creating new aggregates. Because newly created aggregates, for obvious reasons, can’t be involved in two transactions (thus causing concurrency problems), you can create them as part of a transaction initiated for another, already existing, aggregate.

    Of course, you can read as much as you like, also from other aggregates.

    Testability

    There are several things you can test in such solution, each of which requires a different strategy. First of all you should test your domain model as it is your most valuable asset. You can read more about it in one of the previous posts. For testability reasons you should strive to squeeze as much business logic in Value Objects as you can. The remaining logic will be placed on the entities.

    Another thing worth testing is persistence. You have to prove that you can persist and load objects in any valid state. Because it would be not practical to test for all the valid states, I would recommend testing for the most complex states. Odds are that simpler states will just work.

    Application layer tests are also worth the effort. They come in two flavours: with mocked repositories and with real repositories. It depends on particular case which one is more useful. Testing with real repositories requires of course some database. The easiest ones to use in test environment are SQLite and SQL Server CE.

    Last but not least, there is usually some UI code (like controllers) to be tested. Don’t forget about it.

    Events

    Yes, the events have their place also in this architecture. We need them because they are the ultimate integration mechanism. You can do all sorts of cool things if your system publishes the changes to its internal state as a serious of events. If you don’t believe me, read or listen to Udi Dahan when he talks about SOA done right.

    You can implement events in a classic DDD solution in two ways. Either make them a separate concept like in this article of Udi or make them part of your domain model (as proposed by Eric Evans in one of his great presentations). I consider the latter approach cleaner but more difficult to implement properly. The choice is yours.

    User interface

    Some systems really need a user interface. Really. From time to time it is good to display something to the user and let him make some decisions. It makes users feel important etc. So how do you get the data out of the store so that use can see it? As you may have read before on this blog, I am a big opponent of using repositories for getting the data to the views. This is a separate responsibility which I like to implement in Finder classes. Another issue is, how you implement these finders. As usual, you have more than one option. One is to have a set of classes parallel to your domain model that are mapped to the same tables in the database. Of course you would also need another set of ORM mappings. You can read more about this approach here. Another, also valid, approach would be to map domain model classes to DTOs using a tool such as AutoMapper. I hear you saying that mapping domain objects to DTOs is considered an anti pattern. I guess I have said it a few times. But what I am proposing here is a bit different. You should encapsulate the way you get your DTOs in the finders so that it’s not an architectural issue any more. If it’s not an architectural issue, it can’t be an anti-pattern, right? You may have some finders that issue a plain old SQL, some that map from domain objects and some that use database-mapped DTOs. Use whatever method works best to solve the problem at hand and don’t forget to encapsulate the solution.

    Summary

    I would like to hear from you what do you think about this architecture. For sure it’s not the ideal one, the one you dream about. But I’ve implemented it a few times and it worked reasonably well. Do you have any ideas how can I improve it without introducing additional complexity?

    VN:F [1.9.13_1145]
    Rating: 5.0/5 (2 votes cast)