Implementing Audit Trail for Objects in C#?

点点圈 提交于 2019-11-30 05:34:32
Satya

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..

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; }
}

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.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!