CQRSlite
CQRSlite copied to clipboard
Untyped Save<T> when calling from Session
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.
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.
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
// ...
}
}