6.0.0 How to access the 'before' copy of content during save?
Hi,
I need to access IContent object 'before' (and 'after') a saving change - i.e. during one of the ContentService.Save events. Reason for this is to detect a change in a property value (date) in order to trigger a potentially very expensive operation (e.g. folder sorting). IContent is being updated from back-office so I don't have access to object before the update to persist object state and there doesn't seem to be any event hooks for that.
Here is what I have tried so far:
ContentService.Saving event does not provide a copy of the old IContent object, but rather the proposed changes about to be persisted.
Attempt to read currently persisted value from database (i.e. old date) within the ContentService.Saving event handler by using ContentService.GetById() does not work because current architecture - specifically RepositoryBase of ContentRepository - hits the cache first and returns the same, already (modified) object rather than going to database.
...Quickly heading into hack territory now...: attempt to bypass caching and construct ContentRepository to inject mock null cache fails because ContentRepository is flagged as in internal class.
Workaround by instantiating PetaPocoUnitOfWorkProvider, getting UnitOfWork, instantiating RepositoryFactory() then finally creating an instance of ContentRepository also does not appear to have obvious way of overriding / injecting a different cache.
So how does one do it? How to access currently persisted copy of content, and be able to compare 'before' and 'after' copies of content?
The only things I could come up with are:
Add additional property into to content type and store the 'last saved state' then use that to compare (and update) during Saving/Saved event. This is not an option in my case / when developing a public package.
SQL the database directly and incur the wrath of tight-coupling to underlying Umbraco structure du jour.
Override (hack or replace) default editors (controls) of the back office to introduce additional event hooks.
Umbraco - being a versioned CMS - may also hold content history to access old versions of page (content) - option I haven't looked at as yet.
Can anyone see error in my ways and perhaps offer a more elegant approach? Thanks.
6.0.0 How to access the 'before' copy of content during save?
Hi,
I need to access IContent object 'before' (and 'after') a saving change - i.e. during one of the ContentService.Save events. Reason for this is to detect a change in a property value (date) in order to trigger a potentially very expensive operation (e.g. folder sorting). IContent is being updated from back-office so I don't have access to object before the update to persist object state and there doesn't seem to be any event hooks for that.
Here is what I have tried so far:
So how does one do it? How to access currently persisted copy of content, and be able to compare 'before' and 'after' copies of content?
The only things I could come up with are:
Can anyone see error in my ways and perhaps offer a more elegant approach? Thanks.
Cheers!
VM
is working on a reply...