Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create source generators to minimize boilerplate code #1077

Open
alexeyfv opened this issue Jan 19, 2025 · 1 comment
Open

Create source generators to minimize boilerplate code #1077

alexeyfv opened this issue Jan 19, 2025 · 1 comment

Comments

@alexeyfv
Copy link
Contributor

Hi,

I recently came up with the idea of creating a source code generator for aggregates because I got tired of writing repetitive boilerplate code.

Let's say we have the following aggregate:

public class OrderAggregate(OrderAggregateId id) : 
    AggregateRoot<OrderAggregate, OrderAggregateId>(id)
{
    public Task DoSomething() 
    {
        // Some code
    }
}

public class OrderAggregateId(string value) :
    Identity<OrderAggregateId>(value);

To update the aggregate, we usually write this

await store.UpdateAsync<OrderAggregate, OrderAggregateId>(
    id,
    SourceId.New,
    (order, _) => order.DoSomething(),
    cancellationToken);

With source generators, we can omit the type declarations, skip the source ID, and avoid the cancellation token:

await store.UpdateAsync(
    id,
    order => order.DoSomething(),
    cancellationToken);

The source code generator creates the following extension method for IAggregateStore

public static class IAggregateStoreExtensions
{
    public static Task<IReadOnlyCollection<IDomainEvent>> UpdateAsync(
        this IAggregateStore aggregateStore,
        OrderAggregateId id,
        Func<OrderAggregate, Task> update,
        CancellationToken cancellationToken) =>
            aggregateStore.UpdateAsync<OrderAggregate, OrderAggregateId>(
                id,
                SourceId.New,
                (aggregate, _) => update(aggregate),
                cancellationToken);

    // other code
}

What do you think about adding something like this to EventFlow?

You can see the full example here.

@rasmus
Copy link
Member

rasmus commented Jan 23, 2025

Seems simple enough and I'm all for making it easier for developers. Only minor change is that the class should properly be named AggregateStoreExtensions to follow the existing code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants