Return dictionary to SaveChanges ()
I am overriding a method SaveChanges()
to use ChangeTracker
to get changed properties of an object. I need to return the Dictionary<string, string>
changed properties so that in my controller I can call Audit Service
. So far, my SaveChanges () methods look like this:
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 dictionary modifiedProperties
in my controller so that I can pass this to my service?
Controller:
if (validator.IsValid())
{
_workRequestRepo.Save(workRequest);
_auditService.Log(UserId, modelId, "Work Order", "Edit", modifiedProperties);
}
source to share
Using IOC, I would assume that you have something like:
(This assumes an Audit, not an audit)
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 level
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;
}
}
source to share
You do not need to return changed properties, you can decide your audit procedures inside the method SaveChanges
. 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();
}
source to share