0
votes

I'm using Breeze with my Web API and I want to add some auditoring on my Entities. So I want to use the BeforeSaveEntityDelegate but when I implement it, it gets fired after the save function... Here is my Breezecontroller:

[BreezeController]
public class EssController : ApiController
{
    private readonly ESSContextProvider _contextProvider;

    public EssController(ESSContextProvider contextProvider)
    {
        _contextProvider = contextProvider;
    }
    protected  bool BeforeSaveEntity(EntityInfo entityInfo)
    {
        // create audit record and add to your instance of your context
        // this.Context.YourAuditEntity.Add(...)
        if (entityInfo.EntityState == EntityState.Modified)
        {
            var auditable = (Entity)entityInfo.Entity;
            auditable.UpdatedBy = "jja";
            auditable.UpdatedDate = DateTime.Now;
        }
        return true;
    }

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle)
    {
        _contextProvider.BeforeSaveEntityDelegate = BeforeSaveEntity;
        return _contextProvider.SaveChanges(saveBundle);
    }

    [HttpGet]
    public string Metadata()
    {
        return _contextProvider.Metadata();
    }
}

So first he execute SaveChanges and then enters BeforeSaveEntity...

1

1 Answers

0
votes

I think there is no error in the sequence of calls. When you call _contextProvider.SaveChanges(saveBundle); the ContextProvider's SaveChanges method will call the BeforeSaveEntityDelegate if any, then it will persist the changes and call the AfterSaveEntities delegate.

Take a look a the source code for ContextProvider here https://github.com/IdeaBlade/Breeze/blob/master/Breeze.ContextProvider/ContextProvider.cs . Look for the "OpenAndSave" method inside the class.

Don't confuse your ApiController SaveChanges with ContextProvider's SaveChanges.