public interface INotifyProperyChanged<T>
{
event PropertyChangedEventHandler<T> PropertyChanged;
}
public delegate void PropertyChangedEventHandler<T>(object sender,
PropertyChangedEventArgs<T> e);
public class PropertyChangedEventArgs<T> : EventArgs
{
private readonly string propertyName;
public PropertyChangedEventArgs(string propertyName)
{
this.propertyName = propertyName
}
public virtual string PropertyName { get { return propertyName; } }
public T OldValue { get; set; }
public T NewValue { get; set; }
}
You could implement something similiar to INotifyPropertyChanged with a slight difference. I've extracted most of INotifyPropertyChanged and changed it to be generic and store new and old values. You can then have some sort of a management class that can listen to this and onSaving and onDeleting you can deal with the changes.
public interface INotifyProperyChanged<T>
{
event PropertyChangedEventHandler<T> PropertyChanged;
}
public delegate void PropertyChangedEventHandler<T>(object sender,
PropertyChangedEventArgs<T> e);
public class PropertyChangedEventArgs<T> : EventArgs
{
private readonly string propertyName;
public PropertyChangedEventArgs(string propertyName)
{
this.propertyName = propertyName
}
public virtual string PropertyName { get { return propertyName; } }
public T OldValue { get; set; }
public T NewValue { get; set; }
}
如果您使用 SQL Server 2008,则可以使用另一种方法,您可以实现新的审核功能,该功能将允许您审核数据库记录的更改,您甚至可以跟踪(我认为)何时读取数据。
Have you considered using a simple notification pattern? You could have your service layer raise events such as NewObject, ChangedObject, DeletedObject that, will be listened to by a generic service layer which can then take the object and save the results.
If you want to save the state of the object you could leverage Xml Serialization.
Another approach is available if your using SQL Server 2008 you can implement the new Auditing features which will let you audit changes to database records you can even track (I think) when the data is read.
We've implemented a similar solution, using AOP (aspectJ implementation). Using this particular points can be captured and specific operations can be performed.
This can be plugged in and plugged off when we like.
However, our implementation was in J2EE..
If you really want to do it in the app layer, i would suggest this.
In addition to some of the things mentioned in the above thread the Command Pattern might be of help, if you wrap all the state changes on your object in a command then the command can be responsible for keeping the audit trail, while the object does not have to worry about auditing itself. Of course there is added overhead to creating and disposing commands.
You can wrap commands around any existing object structure, you just delegate your actions to the command layer as opposed to doing them on the objects directly.
发布评论
评论(5)
您可以实现类似于 INotifyPropertyChanged 的东西,但略有不同。 我已经提取了大部分 INotifyPropertyChanged 并将其更改为通用的并存储新值和旧值。 然后,您可以拥有某种可以侦听此操作的管理类,并且可以在 onSaving 和 onDeleting 上处理更改。
You could implement something similiar to INotifyPropertyChanged with a slight difference. I've extracted most of INotifyPropertyChanged and changed it to be generic and store new and old values. You can then have some sort of a management class that can listen to this and onSaving and onDeleting you can deal with the changes.
您是否考虑过使用简单的通知模式? 您可以让服务层引发 NewObject、ChangedObject、DeletedObject 等事件,通用服务层将侦听这些事件,然后通用服务层可以获取对象并保存结果。
如果您想保存对象的状态,您可以利用 Xml 序列化。
如果您使用 SQL Server 2008,则可以使用另一种方法,您可以实现新的审核功能,该功能将允许您审核数据库记录的更改,您甚至可以跟踪(我认为)何时读取数据。
Have you considered using a simple notification pattern? You could have your service layer raise events such as NewObject, ChangedObject, DeletedObject that, will be listened to by a generic service layer which can then take the object and save the results.
If you want to save the state of the object you could leverage Xml Serialization.
Another approach is available if your using SQL Server 2008 you can implement the new Auditing features which will let you audit changes to database records you can even track (I think) when the data is read.
问题非常类似于
如何为对象实施审计跟踪(编程) ?
我们已经使用 AOP(aspectJ 实现)实现了类似的解决方案。 利用这个特定点可以捕获并执行特定操作。
当我们需要时,可以插入和拔出。
但是,我们的实现是在 J2EE 中。
如果您确实想在应用程序层中执行此操作,我会建议这样做。
希望能帮助到你..
Question is pretty similar to
How do you implement audit trail for your objects (Programming)?
We've implemented a similar solution, using AOP (aspectJ implementation). Using this particular points can be captured and specific operations can be performed.
This can be plugged in and plugged off when we like.
However, our implementation was in J2EE..
If you really want to do it in the app layer, i would suggest this.
Hope it helps..
除了上面线程中提到的一些内容之外,命令模式可能会有所帮助,如果将对象上的所有状态更改包装在命令中,则该命令可以负责保留审核跟踪,而对象不必担心审核本身。 当然,创建和处理命令会增加开销。
您可以将命令包装在任何现有的对象结构中,只需将操作委托给命令层,而不是直接在对象上执行这些操作。
In addition to some of the things mentioned in the above thread the Command Pattern might be of help, if you wrap all the state changes on your object in a command then the command can be responsible for keeping the audit trail, while the object does not have to worry about auditing itself. Of course there is added overhead to creating and disposing commands.
You can wrap commands around any existing object structure, you just delegate your actions to the command layer as opposed to doing them on the objects directly.
这是一个很老的问题,但对于那些想要审核 C# 对象的人,我会推荐 Audit.NET图书馆。
它具有登录到不同存储系统(SQL、Azure、Mongo)和审核不同系统(WCF、EF、MVC)的扩展。
注:我是楼主
Pretty old question, but for those wanting to audit C# objects, I would recommend the Audit.NET library.
It has extensions to log to different storage systems (SQL, Azure, Mongo) and for auditing different systems (WCF, EF, MVC).
Note: I'm the owner