GitHub - gautema/CQRSlite: A lightweight framework to help creating CQRS and Eventsourcing applications in C# (original) (raw)

Build Status NuGet

A lightweight CQRS and Event Sourcing framework for .NET

Overview

CQRSlite is a small, focused CQRS (Command Query Responsibility Segregation) and Event Sourcing framework for .NET. It provides the essential building blocks for implementing CQRS/ES patterns while maintaining flexibility and pluggability.

Key Characteristics:

CQRSlite originated as a CQRS sample project by Greg Young and Gaute Magnussen in 2010. Original code: http://github.com/gregoryyoung/m-r

Features

Quick Start

Installation

dotnet add package CQRSlite

Basic Usage

  1. Define your messages:

// Command public class CreateProduct : ICommand { public Guid Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }

// Event public class ProductCreated : IEvent { public Guid Id { get; set; } public int Version { get; set; } public DateTimeOffset TimeStamp { get; set; } public string Name { get; set; } public decimal Price { get; set; } }

  1. Create your aggregate:

public class Product : AggregateRoot { private string _name; private decimal _price;

public Product(Guid id, string name, decimal price)
{
    Id = id;
    ApplyChange(new ProductCreated(id, name, price));
}

private Product() { } // Required for rehydration

private void Apply(ProductCreated e)
{
    _name = e.Name;
    _price = e.Price;
}

}

  1. Implement handlers:

public class ProductCommandHandler : ICommandHandler { private readonly ISession _session;

public async Task Handle(CreateProduct message)
{
    var product = new Product(message.Id, message.Name, message.Price);
    await _session.Add(product);
    await _session.Commit();
}

}

  1. Configure services:

// Register Router var router = new Router(); services.AddSingleton(router); services.AddSingleton(router); services.AddSingleton(router); services.AddSingleton(router);

// Register core services services.AddSingleton<IEventStore, YourEventStore>(); // You must implement this services.AddScoped(sp => new Repository(sp.GetService())); services.AddScoped<ISession, Session>();

// Auto-register handlers var registrar = new RouteRegistrar(serviceProvider); registrar.Register(typeof(ProductCommandHandler).Assembly);

Documentation

External Resources

Great introductions to CQRS and CQRSlite:

Requirements

You must implement your own IEventStore for persistence. CQRSlite provides the framework but intentionally does not include a default event store implementation, as storage requirements vary greatly between applications.

Example event stores:

See DEVELOPER.md for implementation guidance.

Architecture

CQRSlite follows clean CQRS/ES principles:

Application Layer
    ↓
Commands → CommandHandlers → Aggregates → Events → EventStore
    ↓                                          ↓
Queries → QueryHandlers → ReadModels ← EventHandlers

Write Side (Commands):

Read Side (Queries):

Contributing

Contributions are welcome! Please see DEVELOPER.md for guidelines.

Version Compatibility

License

Copyright 2020 Gaute Magnussen

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.