Coder Social home page Coder Social logo

Comments (3)

ardalis avatar ardalis commented on June 14, 2024

Yeah, this behavior should be supported. I thought I'd verified it was working. Can you provide some sample code that isn't working?

from cleanarchitecture.

SystematicChaos012 avatar SystematicChaos012 commented on June 14, 2024

Yeah, this behavior should be supported. I thought I'd verified it was working. Can you provide some sample code that isn't working?

@ardalis Yes,

In Core

public class Category : EntityBase<uint>, IAggregateRoot
{
    public uint ParentId { get; private set; }

    public string Name { get; private set; } = default!;

    protected Category() { }

    public Category(uint parentId, string name)
    {
        ParentId = parentId;
        Name = Guard.Against.NullOrWhiteSpace(name, nameof(name));

        RegisterDomainEvent(new CategoryCreatedEvent(parentId, name));
    }
}

In UseCases

public record CreateCategoryCommand(uint ParentId, string Name) : ICommand<Result<uint>>;

public class CreateCategoryHandler(IRepository<Category> repository) : ICommandHandler<CreateCategoryCommand, Result<uint>>
{
    public async Task<Result<uint>> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
    {
        var result = await repository.AddAsync(new Category(request.ParentId, request.Name), cancellationToken);
        return result.Id;
    }
}

In Infrastructure

public class AppDbContext(DbContextOptions<AppDbContext> options, IDomainEventDispatcher? dispatcher) : DbContext(options)
{
  public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
  {
    if (dispatcher == null) return await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

    // bug here
    // ChangeTracker.Entries<EntityBase>() cannot return instances of 'EntityBase<TId>'
    // because there is no inheritance relationship between EntityBase<TId> and EntityBase
    var entitiesWithEvents = ChangeTracker.Entries<EntityBase>()
        .Select(e => e.Entity)
        .Where(e => e.DomainEvents.Any())
        .ToArray();

    // Task DispatchAndClearEvents(IEnumerable<EntityBase> entitiesWithEvents);
    // DispatchAndClearEvents method only accepts IEnumerable<EntityBase>
    await dispatcher.DispatchAndClearEvents(entitiesWithEvents);

    return await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false);;
  }

  public override int SaveChanges()
  {
    return SaveChangesAsync().GetAwaiter().GetResult();
  }
}

Maybe make DispatchAndClearEvents accept IEnumerable<HasDomainEventsBase>

from cleanarchitecture.

rameshdabhi avatar rameshdabhi commented on June 14, 2024

@ardalis just wanted to check in if you're planning to knock this off. This one is bugging me as well. Thanks...

from cleanarchitecture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.