CQRSlite icon indicating copy to clipboard operation
CQRSlite copied to clipboard

Untyped Save<T> when calling from Session

Open meichhorn-conet opened this issue 2 years ago • 2 comments

The following line calls IRepository.Save<T> 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<T> with the concrete Type of the aggregate.

meichhorn-conet avatar Jun 17 '22 06:06 meichhorn-conet

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.

gautema avatar Aug 08 '22 13:08 gautema

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
    // ...
  }
}

meichhorn-conet avatar Aug 08 '22 13:08 meichhorn-conet