Skip to content
geoffreysmith edited this page Jun 2, 2011 · 11 revisions

Recipe #003 - Using Commands

Overview

Inspiration: CQRS a la Greg Young

Lets first consider what would normally happen after receiving a DTO; the user would make changes to the data and save this >back on the DTO. This DTO then gets shipped back to the back-end, converted into an entity and the ORM will make sure that >the changes are persisted into the database.

This would result in loosing some very valuable information; the Why did this change happen? You completely loose the >intent the user had when he changed the data, and this is one of the things Greg’s implementation of CQRS is solving.

Commands, CommandHandlers and their Result live in the Tasks Layer. A Command is sent to a Handler, and returns a result.

Example Implementation

Command

public class ChangeCustomerAddressCommand : CommandBase
{
    public ChangeCustomerAddressCommand(
                                        int id, 
                                        string addressLine1, 
                                        string addressLine2, 
                                        string city,
                                        string stateProvince,
                                        string postalCode,
                                        string countryRegion)
    {
        this.Id = id;
        this.AddressLine1 = addressLine1;
        this.AddressLine2 = addressLine2;
        this.City = city;
        this.StateProvince = stateProvince;
        this.PostalCode = postalCode;
        this.ModifiedDate = DateTime.Now;
        this.CountryRegion = countryRegion;
    }

    [Required]
    public int Id { get; set; }

    [Required]
    public string AddressLine1 { get; set; }

    public string AddressLine2 { get; set; }

    [Required]
    public string City { get; set; }

    [Required]
    public string StateProvince { get; set; }

    [Required]
    public string PostalCode { get; set; }

    [Required]
    public DateTime ModifiedDate { get; set; }

    [Required]
    public string CountryRegion { get; set; }

}

Handler

public class ChangeCustomerAddressHandler : ICommandHandler<ChangeCustomerAddressCommand>
{
    private readonly INHibernateRepository<Address> addressRepository;

    public ChangeCustomerAddressHandler(INHibernateRepository<Address> addressRepository)
    {
        this.addressRepository = addressRepository;
    }

    public ICommandResult Handle(ChangeCustomerAddressCommand command)
    {
        var address = this.addressRepository.Get(command.Id);
        var newAddress = new Address
                             {
                                 AddressLine1 = command.AddressLine1,
                                 AddressLine2 = command.AddressLine2,
                                 City = command.City,
                                 StateProvince = command.StateProvince,
                                 PostalCode = command.PostalCode,
                                 ModifiedDate = command.ModifiedDate,
                                 CountryRegion = command.CountryRegion
                             };

        address = newAddress;
        this.addressRepository.SaveOrUpdate(address);

        return new ChangeCustomerAddressResult(true);
    }
}

Result

public class ChangeCustomerAddressResult : CommandResult
{
    public ChangeCustomerAddressResult(bool success) : base(success)
    {
    }
    
    public string Message { get; set; }
}

Tasks

ToDo

Clone this wiki locally