gautema / CQRSlite

A lightweight framework to help creating CQRS and Eventsourcing applications in C#
Other
1.1k stars 266 forks source link

Untyped Save<T> when calling from Session #111

Open meichhorn-conet opened 2 years ago

meichhorn-conet commented 2 years ago

The following line calls IRepository.Save untyped (T is always AggregateRoot) https://github.com/gautema/CQRSlite/blob/ce8cc0f1032f88c7ae76dff8c97679d4f1b1c019/Framework/CQRSlite/Domain/Session.cs#L75

I would suggest to use dynamic

dynamic aggregate = descriptor.Aggregate;
await _repository.Save(aggregate, descriptor.Version, cancellationToken).ConfigureAwait(false);

This calls Save with the concrete Type of the aggregate.

gautema commented 2 years ago

Hi. Sorry for the late reply.

Can you please tell me what this would solve? Is there a bug happening the way it is now? Parsing to dynamic is very slow, so I'd like to have a good use case for it.

meichhorn-conet commented 2 years ago

I think it's a bug. When you implement your own IRepository.Save<T>, typeof(T) is always AggregateRoot and not your specific AggregateRoot-Type.

Pseudo code:


var aggregate = new MyAggregate();
session.Add(aggregate);
await session.Commit();

public class MyRepository : IRepository
{
  public Task Save<T>(T aggregate, int? expectedVersion = null, CancellationToken cancellationToken = default) where T : AggregateRoot 
  {
    Console.WriteLine(typeof(T).Name); // -> AggregateRoot
    Console.WriteLine(aggregate.GetType().Name); // -> MyAggregate
    // ...
  }
}