Seeing everything that is persisted
No edit summary
No edit summary
Line 7: Line 7:


Maybe you have a common base class for all classes in your model, and maybe this class implements an Interface ITrackUpdate, then it would be a good thing to catch all updates and set the datetime…
Maybe you have a common base class for all classes in your model, and maybe this class implements an Interface ITrackUpdate, then it would be a good thing to catch all updates and set the datetime…
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; overflow: auto;">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">UpdateHandler</span> : <span style="color: #2b91af;">ChainedPersistenceHandlerBase</span></li>
<li style="background: #f3f3f3;">{</li>
<li>  <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">readonly</span> <span style="color: #2b91af;">IObjectRepresentationProvider</span> orp;</li>
<li style="background: #f3f3f3;">  <span style="color: #0000ff;">public</span> UpdateHandler(<span style="color: #2b91af;">IObjectRepresentationProvider</span> orp, <span style="color: #2b91af;">IEcoTypeSystem</span> typeSystem)</li>
<li>    : <span style="color: #0000ff;">base</span>(typeSystem)</li>
<li style="background: #f3f3f3;">  {</li>
<li>    <span style="color: #0000ff;">this</span>.orp = orp;</li>
<li style="background: #f3f3f3;">  }</li>
<li>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> UpdateDatabaseWithList(<span style="color: #2b91af;">ICollection</span>&lt;<span style="color: #2b91af;">Locator</span>&gt; locators)</li>
<li style="background: #f3f3f3;">  {</li>
<li>    <span style="color: #2b91af;">DateTime</span> timestamp = <span style="color: #2b91af;">DateTime</span>.Now;</li>
<li style="background: #f3f3f3;">    <span style="color: #0000ff;">foreach</span> (<span style="color: #2b91af;">Locator</span> loc <span style="color: #0000ff;">in</span> locators)</li>
<li>    {</li>
<li style="background: #f3f3f3;">      <span style="color: #2b91af;">IObject</span> obj = orp.IObjectForLocator(loc);</li>
<li>      <span style="color: #0000ff;">if</span> (obj.AsObject <span style="color: #0000ff;">is</span> <span style="color: #2b91af;">ITrackUpdate</span>)</li>
<li style="background: #f3f3f3;">      {</li>
<li>        (obj.AsObject <span style="color: #0000ff;">as</span> <span style="color: #2b91af;">ITrackUpdate</span>).PreUpdate(timestamp);</li>
<li style="background: #f3f3f3;">      }</li>
<li>    }</li>
<li style="background: #f3f3f3;">    <span style="color: #0000ff;">base</span>.UpdateDatabaseWithList(locators);</li>
<li>  }</li>
<li style="background: #f3f3f3;">}</li>
</ol>
</div>
</div>
</div>


We need to install our ChainedPersistenceHandlerBase baseclass. We do this by mixing it in before the current persistencehandler , we do this in the ecospace – before we go active:
We need to install our ChainedPersistenceHandlerBase baseclass. We do this by mixing it in before the current persistencehandler , we do this in the ecospace – before we go active:
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9259e046-ad23-4f69-b77e-ec15cd0d3597" class="wlWriterEditableSmartContent" style="margin: 0px; display: inline; float: none; padding: 0px;">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; overflow: auto;">
<ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;">
<li><span style="color: #008000;">// Install the update handler which intercepts all updates and updated the "Modified" attribute</span></li>
<li style="background: #f3f3f3;"><span style="color: #2b91af;">UpdateHandler</span> uh = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">UpdateHandler</span>(FrontsidePolicy.ObjectRepresentationProvider, TypeSystem);</li>
<li><span style="color: #2b91af;">PersistenceServiceImpl</span> ps = Persistence <span style="color: #0000ff;">as</span> <span style="color: #2b91af;">PersistenceServiceImpl</span>;</li>
<li style="background: #f3f3f3;">uh.NextPersistenceHandler = ps.PersistenceHandler;</li>
<li>ps.PersistenceHandler = uh;</li>
<li style="background: #f3f3f3;">FrontsidePolicy.PersistenceHandler = uh;</li>
</ol>
</div>
</div>
</div>

Revision as of 16:28, 28 November 2018

There is something called a ChainedPersistenceHandlerBase that you can inherit from and intercept everything that goes to and from the database.

You can use override as you see fit:

  1. namespace Eco.Persistence
  2. {
  3.   // Summary:
  4.   //     Base class for persistence handler.
  5.   //
  6.   // Remarks:
  7.   //     Use this class to implement your own IPersistenceHandler override any of
  8.   //     the virtual methods to inject your own behaviour and copy the Install-procedure
  9.   //     (from the source of this class) for a convenient way to install the new class
  10.   //     in the EcoSpace.
  11.   public class ChainedPersistenceHandlerBase : IPersistenceHandler
  12.   {
  13.     public ChainedPersistenceHandlerBase(IEcoTypeSystem typeSystem);
  14.  
  15.     public bool IsPersistent { get; }
  16.     public virtual int MaxSavedVersion { get; }
  17.     public IPersistenceHandler NextPersistenceHandler { get; set; }
  18.     public bool SupportsSync { get; }
  19.     protected IEcoTypeSystem TypeSystem { get; }
  20.  
  21.     public virtual event LocatorArrayEventHandler ObjectsUpdated;
  22.  
  23.     public virtual void Fetch(ICollection<Locator> locators, int[] members, FetchStrategy FetchStrategy);
  24.     public virtual ICollection<Locator> FetchLinksWithObjects(ICollection<Locator> objects, IAssociationEnd assocEnd);
  25.     public virtual ICollection<Locator> GetAllWithCondition(AbstractCondition condition, int maxAnswers, int offset);
  26.     public virtual Datablock GetValueWithCondition(AbstractCondition condition, int maxAnswers, int offset);
  27.     public virtual void RetrieveChanges(out DBChangeCollection ignoredChanges);
  28.     public virtual DateTime TimeForVersion(int version);
  29.     public virtual void UpdateDatabaseWithList(ICollection<Locator> locators);
  30.     public virtual int VersionAtTime(DateTime time);
  31.   }
  32. }


Maybe you have a common base class for all classes in your model, and maybe this class implements an Interface ITrackUpdate, then it would be a good thing to catch all updates and set the datetime…

  1. public class UpdateHandler : ChainedPersistenceHandlerBase
  2. {
  3.   private readonly IObjectRepresentationProvider orp;
  4.   public UpdateHandler(IObjectRepresentationProvider orp, IEcoTypeSystem typeSystem)
  5.     : base(typeSystem)
  6.   {
  7.     this.orp = orp;
  8.   }
  9.   public override void UpdateDatabaseWithList(ICollection<Locator> locators)
  10.   {
  11.     DateTime timestamp = DateTime.Now;
  12.     foreach (Locator loc in locators)
  13.     {
  14.       IObject obj = orp.IObjectForLocator(loc);
  15.       if (obj.AsObject is ITrackUpdate)
  16.       {
  17.         (obj.AsObject as ITrackUpdate).PreUpdate(timestamp);
  18.       }
  19.     }
  20.     base.UpdateDatabaseWithList(locators);
  21.   }
  22. }


We need to install our ChainedPersistenceHandlerBase baseclass. We do this by mixing it in before the current persistencehandler , we do this in the ecospace – before we go active:

  1. // Install the update handler which intercepts all updates and updated the "Modified" attribute
  2. UpdateHandler uh = new UpdateHandler(FrontsidePolicy.ObjectRepresentationProvider, TypeSystem);
  3. PersistenceServiceImpl ps = Persistence as PersistenceServiceImpl;
  4. uh.NextPersistenceHandler = ps.PersistenceHandler;
  5. ps.PersistenceHandler = uh;
  6. FrontsidePolicy.PersistenceHandler = uh;
This page was edited 77 days ago on 02/10/2024. What links here