Base solution for your next web application
Open Closed

entity change set in DomainService #6007


User avatar
0
vladsd created

I am looking for the way to get the current entity change set in my domain service before it gets submitted to the database.

I can see I can do this: var changeSet = _entityHistoryHelper.CreateEntityChangeSet(ChangeTracker.Entries().ToList());

BUT ChangeTracker is part of DbContext. How do I get current dbcontext in domain service?

Thanks for insight, as I need list of changes before the submitted to the database.


7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    using Abp.EntityFrameworkCore.Repositories;

    var changeTracker = yourRepository.GetDbContext().ChangeTracker;

  • User Avatar
    0
    aaron created
    Support Team

    Can you describe your use case?

  • User Avatar
    0
    vladsd created

    @mailming, thanks, but domain service in core package which does not have relationship with yourRepository, as that one includes core package.

    @aaron - here is a use case I have a domain service, which does bunch of works on various financial accounts part of unit of work. That works creates a number of affects, which I need to capture and validate.

    If I wait till SaveChanges, then its too late. I need to get all those changes myself before SaveChanges is called. Hope it helps.

  • User Avatar
    0
    aaron created
    Support Team

    thanks, but domain service in core package which does not have relationship with yourRepository, as that one includes core package.

    Then you shouldn't be doing it in domain service.

    If I wait till SaveChanges, then its too late. I need to get all those changes myself before SaveChanges is called.

    Too late for what? Show code.

  • User Avatar
    0
    BobIngham created

    Hi Vlad, I think the key here is to override EntityHistoryStore. Paste a copy (I seem to tremember it's in Boilerplate) and inject in ProjectNameCoreModule:

    Configuration.ReplaceService<IEntityHistoryStore, YourEntityHistoryStore>(DependencyLifeStyle.Transient);
    

    Then modify the SaveAsync method accordingly. Personally I choose to save entity history in Mongodb because of the size of data (I also use Mongodb for audit log entries) so my personal override uses a Mongodb interface. Let me know if you want the code and I will post accordingly but, judging by your product, your guys seem to be proficient enough to take this forward.

  • User Avatar
    0
    vladsd created

    @aaron, domain service is where the logic lies. do you have suggestion on how to get the changes of the context of the objects without relying on yourRepository.GetDbContext().ChangeTracker

  • User Avatar
    0
    aaron created
    Support Team

    domain service is where the logic lies.

    It's where the logic should not lie, if you have a dependency on ChangeTracker.

    You cannot get the EntityChangeSet of the context without ChangeTracker, as it's only created (and exists) in SaveChanges.

    Seems like an XY problem. Show code, so we can solve the actual problem X.