Created
October 16, 2012 18:47
-
-
Save corkupine/3901182 to your computer and use it in GitHub Desktop.
AuditingPersistence pseudocode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ObjRepository : IObjRepository | |
{ | |
public Obj Add(Obj newObj) | |
{ | |
Audited<Obj> newAuditedObj = new AuditPersist.Audited<Obj>(Obj newObj); | |
Audited<Obj> persistedAuditedObj = CI.Add<Audited<Obj>>("objbucket", newAuditedObj); | |
return newAuditedObj.CurrentState(); | |
} | |
private Audited<Obj> GetAudited(Guid objId) | |
{ | |
Audited<Obj> existingAuditedObj = CI.Get<Audited<Obj>>("objbucket", objId); | |
if (existingAuditedObj.HasSiblings) | |
{ | |
Obj[] siblings = GetSiblings(existingAuditedObj.Siblings); | |
Audited<Obj> mergedAuditedObj = AuditPersist.Merge<Audited<Obj>>(siblings); | |
existingAuditedObj = CI.Update<Audited<Obj>>(mergedAuditedObj); | |
} | |
return existingAuditedObj; | |
} | |
public Obj Get(Guid objId) | |
{ | |
return GetAudited(objId).CurrentState(); | |
} | |
public AuditLog GetAuditLog(Guid objId) | |
{ | |
return GetAudited(objId).AuditLog(); | |
} | |
public Obj Update(Obj obj) | |
{ | |
Audited<Obj> existingAuditedObj = GetAudited(obj.Id); | |
Audited<Obj> updatedAuditedObj = existingAuditedObj.Delta(obj); | |
return CI.Update<Audited<Obj>>(updatedAuditedObj).CurrentState(); | |
} | |
public void Delete(Guid objId) | |
{ | |
CI.Delete("objbucket",objId); | |
} | |
} | |
namespace AuditPersist | |
{ | |
public Audited<T> | |
{ | |
public Audited(T obj) | |
{ | |
this.Original = obj; | |
this.AuditLog = new List<AuditEntry>(); | |
} | |
public T Original { set; } | |
public Audited<T> Delta(T obj) | |
{ | |
Audited<T> deltaAuditedObj = new Audited<T>(this.Original); | |
deltaAuditedObj.AuditLog = this.AuditLog; | |
AuditEntry diffs = DiffingMagic(this.CurrentState(),obj); | |
deltaAuditedObj.AuditLog.Add(diffs); | |
return deltaAuditedObject; | |
} | |
public T CurrentState() | |
{ | |
return SummingMagic(this); | |
} | |
public List<AuditEntry> AuditLog {get; set;} | |
private AuditEntry DiffingMagic(T then, T now) | |
{ | |
//Do magic | |
return auditEntry; | |
} | |
private T SummingMagic(Audited<T> auditedObj) | |
{ | |
//Do magic | |
return obj; | |
} | |
} | |
public AuditEntry | |
{ | |
List<string> Deletes {get; set;} | |
Dictionary<string, string> Updates {get; set;} | |
DateTime TimeStamp {get; set;} | |
Guid UserId {get; set;} | |
String UserName {get; set;} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment