Hello La,
here is my example code:
using
System;
using
System.Collections.Generic;
using
Telerik.OpenAccess;
namespace
AuditTrail
{
class
Program
{
static
void
Main(
string
[] args)
{
var db = Database.Get(
"DatabaseConnection1"
);
var scope = db.GetObjectScope();
scope.Tracking.Changed += Tracking_Changed;
scope.Tracking.Changing += Tracking_Changing;
A a =
new
A();
a.Name =
"orig"
;
a.B =
new
B();
a.Cs =
new
List<C>();
a.Cs.Add(
new
C());
a.Cs.Add(
new
C());
scope.Transaction.Begin();
scope.Add(a);
scope.Transaction.Commit();
scope.Transaction.Begin();
a.Name =
"changed"
;
a.B =
new
B();
a.Cs.RemoveAt(0);
a.Cs.Add(
new
C());
scope.Transaction.Commit();
}
static
void
Tracking_Changing(
object
sender, ChangeEventArgs e)
{
var scope = Database.GetContext(e.PersistentObject)
as
IObjectScope;
if
(e.PersistentObject
is
A)
{
// make a clone of the old object and store it in a second table
if
(e.WasDirty ==
false
)
scope.Add(
new
AClone((A)e.PersistentObject));
// store old list content
if
(e.FieldName ==
"cs"
)
e.Tag =
new
List<C>(((A)e.PersistentObject).Cs);
}
}
static
void
Tracking_Changed(
object
sender, ChangeEventArgs e)
{
var scope = Database.GetContext(e.PersistentObject)
as
IObjectScope;
if
(e.PersistentObject
is
A)
{
if
(e.FieldName ==
"cs"
)
{
var oldValue = e.Tag
as
List<C>;
var newValue =
new
List<C>(((A)e.PersistentObject).Cs);
// what do you want to do now with these lists?
}
else
if
(e.FieldName ==
"b"
)
{
var oldValue1 = e.OldValue
as
B ;
var newValue1 = e.NewValue
as
B;
// what do you want to do now with these references?
}
//...
}
}
}
[Persistent]
class
A
{
private
string
name;
private
B b;
private
IList<C> cs;
public
string
Name
{
get
{
return
name;
}
set
{
name = value;
}
}
public
B B
{
get
{
return
b;
}
set
{
b = value;
}
}
public
IList<C> Cs
{
get
{
return
cs;
}
set
{
cs = value;
}
}
}
[Persistent]
class
AClone
{
private
string
name;
private
B b;
private
IList<C> cs;
private
DateTime changed;
public
string
Name
{
get
{
return
name;
}
set
{
name = value;
}
}
public
B B
{
get
{
return
b;
}
set
{
b = value;
}
}
public
IList<C> Cs
{
get
{
return
cs;
}
set
{
cs = value;
}
}
public
DateTime Changed
{
get
{
return
changed;
}
set
{
changed = value;
}
}
public
AClone()
{
}
public
AClone(A other)
{
Name = other.Name;
B = other.B;
Cs =
new
List<C>(other.Cs);
Changed = DateTime.Now;
}
}
[Persistent]
class
B
{
}
[Persistent]
class
C
{
}
}
Sincerely yours,
Jan Blessenohl
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the
Telerik Public Issue Tracking system and vote to affect the priority of the items