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)
-
0
using Abp.EntityFrameworkCore.Repositories;
var changeTracker = yourRepository.GetDbContext().ChangeTracker;
-
0
Can you describe your use case?
-
0
@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.
-
0
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.
-
0
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.
-
0
@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
-
0
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 withoutChangeTracker
, as it's only created (and exists) inSaveChanges
.Seems like an XY problem. Show code, so we can solve the actual problem X.