Skip to content
kvarv edited this page Dec 19, 2014 · 7 revisions

Documentation

###Documentation for 3.0 is coming soon...

##Define a command

public class CreateCustomerCommand : ICommand
{
    public string Name { get; set; }
}

##Send a command

public class CustomerService : ICustomerService
{
    private readonly IBus _bus;

    public CustomerService(IBus bus)
    {
        _bus = bus;
    }

    public async Task SendCommand(CreateCustomerCommand command)
    {
        await _bus.SendAsync(command);
    }
}

##Handle a command

public class CreateCustomerHandler : IHandleMessages<CreateCustomerCommand>
{
    public async Task HandleAsync(CreateCustomerCommand command)
    {
        await Task.Delay(100);
        Console.WriteLine("Creating customer {0}.", command.Name);
    }
}

##Define an event

public class CustomerCreatedEvent : IEvent
{
    public Guid CustomerId { get; set; }
}

##Publish an event

public class CreateCustomerHandler : IHandleMessages<CreateCustomerCommand>
{
    private readonly IBus _bus;

    public CreateCustomerHandler(IBus bus)
    {
        _bus = bus;
    }

    public async Task HandleAsync(CreateCustomerCommand command)
    {
        Console.WriteLine("Creating customer {0}.", command.Name);
        await _bus.PublishAsync(new CustomerCreatedEvent { CustomerId = Guid.NewGuid() });
    }
}

##Handle an event

public class CustomerCreatedHandler : IHandleMessages<CustomerCreatedEvent>
{
    public async Task HandleAsync(CustomerCreatedEvent command)
    {
        await Task.Delay(50);
        Console.WriteLine("Customer with id {0} created.", command.CustomerId);
    }
}

##Define a query

public class GetAllCustomersQuery : IQuery<GetAllCustomersResponse>
{
}

##Define a query response

public class GetAllCustomersResponse
{
    public List<Customer> Cutomers { get; set; }
}

##Send a query

public async Task<GetAllCustomersResponse> SendQuery(GetAllCustomersQuery query)
{
    return await _bus.SendAsync(query);
}

##Handle a query

public class GetAllCustomersHandler : IHandleQueries<GetAllCustomersQuery, GetAllCustomersResponse>
{
    public async Task<GetAllCustomersResponse> HandleAsync(GetAllCustomersQuery query)
    {
        await Task.Delay(50);
        return new GetAllCustomersResponse
        {
            Cutomers = Db.Customers
        };
    }
}

##Set up an IoC container ###LightInject setup

var serviceContainer = new ServiceContainer();
//Register all types that implements IHandleMessages and IHandleQueries
serviceContainer.RegisterAssembly(Assembly.GetExecutingAssembly(), (serviceType, implementingType) => serviceType.IsGenericType && (serviceType.GetGenericTypeDefinition() == typeof(IHandleMessages<>) || serviceType.GetGenericTypeDefinition() == typeof(IHandleQueries<,>)));

//Register the Bus
serviceContainer.Register<IBus>(sf => new Bus(sf.GetAllInstances), new PerContainerLifetime());

Polymorphic dispatching

Say you publish an event like the CustomerCreatedEvent as described below. Since CustomerCreatedEvent implements IMessage you can create a handler for IMessage that also will be invoked when you publish. The same goes for commands.

public class LoggingHandler : IHandleMessages<IMessage>
{
    public async Task HandleAsync(IMessage message)
    {
        await Task.Delay(20);
        Console.WriteLine("{0} was handled", message.GetType().Name);
    }
}

##Examples See the LightBus.Examples repository

Clone this wiki locally