Using a Command Execution Style Architecture with a Domain Model

Kyle Baley has an interesting article about how he’s using commands to break up the logic of his application into small manageable pieces. I’m using the same command processor pattern for a project I’ve working on as well.  It’s a great way for breaking up those "service" classes that really just end up as great big lint collectors for various methods.

Kyle mentions that in his application, he still has a very anemic domain model and most of the work happens when a command is processed. However, it’s entirely possible to use a full domain model with the commands, and really make things easier in the long run.  The commands do the same job the service methods did: collect information necessary for the domain models to do their job.  Once that’s done, the info should be passed to your domain model to determine what to do with it.

There are two reasons (that I can think of) why I think anemic domain models will cause problems in command based architectures.  First, just as with service layer architecture, if you’re service layer is making decisions about what state your domain objects should be in, then the only way you can change the state of your domain object is by processing a command.  Another problem is that, while using commands, makes it easier to understand what each command is doing, it also hides the process somewhat.  When a command executor is responsible for finding the correct command to execute, it can be more difficult to manually fire off the correct command to execute.  If there is important logic hidden in the command, it is more likely to be duplicated rather than referred.

Regardless of whether you use a Service Layer or a Command Processor architecture, the important part is to keep all of the logic that keeps your domain objects in a valid state within the domain object.  This will promote the reuse of logic, increase the cohesion of you domain objects, and reduce the coupling to other components to maintain proper state of an object through it’s lifetime.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

This entry was posted in Command Processor Pattern, DDD. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Blair

    How does a command return data. If I have a SaveCustomerCommand, how do I return the auto generated identifier from it? Code Sample would be helpfull

  • http://www.teamdatalogic.com Steve Powell

    Generally you pass the ID into the command. This is easy if you are using Guids as ids. A bit harder if you are using ints.

  • http://datalogicsolutions.blogspot.com/ Steve Powell

    We are using the command pattern in conjunction with domain events for communicating between aggregates. This has had a number of nice side effects (see http://datalogicsolutions.blogspot.com/). A good place for sample code is Udi Dahan’s blog

  • Blair

    Any sample code?

  • http://www.teamdatalogic.com Steve Powell

    There is code here for raising events and handling them. I will be putting more details of our implementation on our blog soon.
    http://www.udidahan.com/2009/06/14/domain-events-salvation/

  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    @Blair

    Commands don’t return data. In command-query separation, (Bertrand-Meyer style), you have methods that DO things, methods that RETURN things, but not both. You either perform an operation or answer a question.

    What we often do is create a correlation identifier for commands, so that I can request the status of a command to know if it’s finished processing/status etc.

  • Blair

    If I have a MVC controller which is doing a save for example how do I know what the Id of the customer for example is. To have a Command Processor Does not need a MessageBus as I believe.

    Do you use Assigned Identify in NHibernate then?

  • Blair

    Here is My Usage scenario.
    1. Web Page with field to create a new customer
    2. MVC Controller action for Save
    3. Create Command with details from for
    4. Get Handler for Command
    5. Execute Command
    6. Redirect to display new details on Say a Update page ie Controller Action Update in Get Mode.

    If I do not know the Id of the newly saved customer how do I perform step 6?

    Thats my point.

  • Blair

    What I would like to See is some code to get a feel for the comment I mentioned above.

    Blair

  • http://www.lostechies.com/members/jcteague/default.aspx jcteague

    @Blair, I will post some sample code as soon as I can. I

    On this application, we are not strictly following the CQS (though I with they looked more like what Jimmy is describing). I’ll try to work up an example of both.

    They run synchronously and return success or failure, any error messages and whatever data needs to be generated.

  • Blair

    All I really want to know is how you handle create as mentioned earlier?

  • http://www.lostechies.com/members/jcteague/default.aspx jcteague

    As Stated earlier, the easiest way is to use a GUID for your id and pass that to you command. At that point you just pass the guid onto the update command.

    If you’re using integers for your ids, than using a hi/lo implementation would be my next suggestion.

    I would not recommend using an auto incrementing identity column in your database for your id (which I’m guessing you’re doing) At that point, you’re application no longer has control over the id generation. Then you’ll need to use a correlation identifier as Jimmy suggested.

  • Blair

    I thought as such. So to recap.

    1. Web Page with field to create a new customer

    2. MVC Controller action for Save

    3. Create Command with details from form – Including a new GUID created in the controller and passed to the command object like the form arguements.

    4. Get Handler for Command

    5. Execute Command

    6. Redirect to display new details on Say a Update page ie Controller Action Update in Get Mode.

    I assume with NHibernate your using GUIDs for you Identity and no GENERATOR just GENERATOR ASSIGNED type in NHibernate.

    Is this correct?

    Thanks.

  • Blair

    Thought?

  • Blair

    Anyone got any feedback on the discussion so far?

  • http://datalogicsolutions.blogspot.com/ Steve Powell

    @Blair

    We have an EntityBase base class which all our entities inherit from. We use fluent nhibernate and map the ID column of the base class as follows (you also need a version column if you are assigning your own id value):
    Id(e => e.ID).GeneratedBy.Assigned();
    Version(x => x.Version).UnsavedValue(“0″);
    Then in the constructor of the entity base class we set the ID using a sequential GUID (quicker to access when querying db) using:

    protected EntityBase()
    {
    ID = SequentialGuid();
    }

    [DllImport("rpcrt4.dll", SetLastError = true)]
    static extern int UuidCreateSequential(out Guid guid);

    public static Guid SequentialGuid()
    {
    const int RPC_S_OK = 0;
    Guid g;
    if (UuidCreateSequential(out g) != RPC_S_OK)
    return Guid.NewGuid();
    else
    return g;
    }

    The ID can be overwritten if required (as in the above case where you are asking a command to create an entity with a specific id)

    As I say I will post more readable code on our blog once I get a chance…