Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vojin M. 3 posts 23 karma points
    Feb 01, 2013 @ 09:51
    Vojin M.
    0

    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:

    1. 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.
    2. SQL the database directly and incur the wrath of tight-coupling to underlying Umbraco structure du jour.
    3. Override (hack or replace) default editors (controls) of the back office to introduce additional event hooks.
    4. 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.

    Cheers!

    VM 

     

Please Sign in or register to post replies

Write your reply to:

Draft