I am overriding the SaveChanges() method so that I can use the ChangeTracker to get the modified properties of an entity. I need to return a Dictionary<string, string> of the modified properties so that in my controller I can call the Audit Service. So far my SaveChanges() methods looks like:
public override int SaveChanges()
{
var changeInfo = ChangeTracker.Entries()
.Where(t => t.State == EntityState.Modified)
.Select(t => new {
Original = t.OriginalValues.PropertyNames.ToDictionary(pn => pn, pn => t.OriginalValues[pn]),
Current = t.CurrentValues.PropertyNames.ToDictionary(pn => pn, pn => t.CurrentValues[pn])
});
Dictionary<string, string> modifiedProperties = new Dictionary<string, string>();
foreach (var item in changeInfo)
{
foreach (var origValue in item.Original)
{
var currValue = item.Current[origValue.Key];
if ((origValue.Value != null && currValue != null) && !origValue.Value.Equals(currValue))
{
modifiedProperties.Add(origValue.Key, string.Format("Old Value: {0}, New Value: {1}", origValue.Value, currValue));
}
}
}
return base.SaveChanges();
}
Is there a way to access the modifiedProperties dictionary in my controller so I can pass that to my service?
Controller:
if (validator.IsValid())
{
_workRequestRepo.Save(workRequest);
_auditService.Log(UserId, modelId, "Work Order", "Edit", modifiedProperties);
}
You don't have to return the modified properties, you can tackle your audit procedures inside the SaveChanges method. This is an example:
public MyContainer(IUserProvider userProvider) {
_userProvider = userProvider;
}
public override int SaveChanges() {
var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));
if (entities.Any()) {
User currentUser = _userProvider.GetCurrent();
if (currentUser == null)
throw new Exception("Current user is undefined.");
DateTime time = DateTime.Now;
foreach (var entity in entities) {
BaseEntity baseEntity = (BaseEntity)entity.Entity;
if (entity.State == EntityState.Added) {
baseEntity.Created = time;
baseEntity.CreatedBy = currentUser;
}
baseEntity.Modified = time;
baseEntity.ModifiedBy = currentUser;
// get and store the changed properties of the entity here
// ....
var changeInfo = entities.Select(t => new { Original = t.OriginalValues.PropertyNames.ToDictionary(pn => pn, pn => originalValues[pn]), Current = t.CurrentValues.PropertyNames.ToDictionary(pn => pn, pn => t.CurrentValues[pn]);
}
}
return base.SaveChanges();
}
Using IOC I would imagine you have something like:
(This is assume Auditing and not Revisioning)
Presentation:
public PersonController
{
private IPersonBL _personBL;
public PersonController(IPersonBL personBL)
{
_personBL = personBL
}
public ActionResult SavePerson(PersonVM model)
{
// if ModelState etc etc
var person = Mapper.Map<Person>(model);
_personBL.Save(person)
}
}
Business Layer
public PersonBL : IPersonBL
{
private IAuditService _auditService;
private IPersonRepo _personRepo;
public PersonBL(IAuditService auditService,
IPersonRepo personRepo)
{
_auditService = auditService;
_personRepo = personRepo;
}
public void Save(Person person)
{
PersonDTO personDTO = Mapper.Map<PersonDTO>(person);
var result = _personRepo.Save(personDTO);
if (result.Count > 0)
{
_auditService.Audit(result);
}
}
}
Data Layer
public PersonDL : IPersonDL
{
private DbContext _context;
public PersonDL(DbContext dbContext)
{
_context = dbContext;
}
public IDictionary<string, string> Save(PersonDTO person)
{
var result = new Dictionary<string, string>()
_context.Persons.Add(person);
var saveCount = _context.SaveChanges();
if (saveCount > 0)
{
// Do Object Tracking
// Populate result;
}
return result;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With