审计日志方法的提示

发布于 2024-07-25 04:55:03 字数 330 浏览 1 评论 0原文

我目前正在开发 ASP.NET 人力资源系统。 我正在使用 Web 客户端软件工厂的分层架构,它基于 MVP 模式。 ORM 是 NHibernate。 我需要实现一个审核日志模块。 我读过很多关于不同方法的文章。 他们中的大多数描述了如何跟踪日期、时间戳和进行更改的人的身份,但没有人能告诉我这一点:如何跟踪我的域层中任何属性的更改? 我不需要任何回滚功能,我只需要记录:谁、何时以及什么对象的属性被更改,该属性的旧值和新值。

我无法决定将这些更改的处理程序放在哪里。 Fowler 在属性的 setter 方法中指出了审核日志,但我仍然想让我的域类保持简单的 POCO。 也许还有其他方法?

I'm currently developing an ASP.NET Human Resources System. I'm using a layered architecture with Web Client Software Factory, which is based on MVP pattern. ORM is NHibernate. And I need to implement an Audit Log module. I've read a lot about different approaches. Most of them describe how to track date, timastamp and identity of person who made that change, but no one could tell me about that one: how to track changes of any property in my Domain layer? I don't need any rollback capability, I need only Log with: who, when and what property of what object was changed, old value and new value of that property.

I can't decide where to put the handler of this changes. Fowler pointed an Audit Log in setter method of properties, but I still want to keep my domain classes simple POCO`s. Maybe there is some other approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫多说 2024-08-01 04:55:03

几年前我也必须为人力资源系统这样做。 我通过让我的所有“字段”实现一个模板(通用)来完成它:

这是我精简的模板的示例:

class DataField<T>
{
    public T Current { get; set; }
    public T Original { get; set; }
    // stores the field name as a nice textual readable representation.
    // would default to property name if not defined.
    public string FieldName { get; set; }
    public bool Modified
    {
        get { return !(Current.Equals(Original));
    }

    public DataField(T value)
    {
        Original = Current = value;
    }

    public DataField(T value, T fieldName)
    {
        Original = Current = value;
        FieldName = fieldName;
    }
}

它使审核变得容易的有趣部分是每个对象都可以生成自己的审核日志。 我可以采用任何可能有 x 个这些“字段”的对象,并对其调用 GetAudit,它会返回一个审计对象,其中包含对类的所有更改,显示字段名称、旧值、新值等。 “DataField”将实现一个返回审核对象的方法。 对于字符串、双精度、整数等,它几乎是内置的,但如果您使用自定义对象,您可以为它们编写审计实现,只需返回一个审计对象。

因此,最后以一种典型的形式,我将所有数据存储在一个具有所有这些类型字段的对象中。 然后,我将进行更新并调用 GetAudit 方法,该方法也将写入审核表。

我可以轻松判断表单中是否有任何更改,即使它们必须浏览多个页面等。

撤消在逐个字段、逐个部分或整个对象级别上也非常容易。

由于我很长时间没有接触代码,所以对确切的细节有点模糊,但这就是它的要点。 希望有帮助。

I had to do this a few years back for an HR system as well. I accomplished it having all my 'fields' implement a template (generic):

Here is an example of the template I made trimmed down:

class DataField<T>
{
    public T Current { get; set; }
    public T Original { get; set; }
    // stores the field name as a nice textual readable representation.
    // would default to property name if not defined.
    public string FieldName { get; set; }
    public bool Modified
    {
        get { return !(Current.Equals(Original));
    }

    public DataField(T value)
    {
        Original = Current = value;
    }

    public DataField(T value, T fieldName)
    {
        Original = Current = value;
        FieldName = fieldName;
    }
}

The interesting part about it that made auditting easy was that each object could produce it's own audit log. I could take any object that could have x number of these 'fields' and call the GetAudit on it and it would return me an audit object with all the changes to the class showing the field name, old val, new val etc.. Each 'DataField' would implement a method to return an audit object. For strings, double, ints etc it was pretty much baked in but if you used custom objects you could write the audit implementation for them that just had to return a Audit object.

So in a typical form at the end I would have all the data stored in one object that had all these types of fields. I would then do an update and call the GetAudit method which would also be written to an audit table as well.

I could easily tell if anything had changed in the form even if they had to go through multiple pages etc.

Undo's were really easy on a field by field, section by section or the entire object level as well.

Little foggy on the exact details as I haven't touched the code in a long time but that was the gist of it. Hope that helps.

耀眼的星火 2024-08-01 04:55:03

也许您可以实现观察者模式,但是,由于 .net 隐式实现了此模式(通过事件),我认为不会有太多附加值。

也许您可以保存“原始”对象,并在适当的时候将它们与修改后的对象进行比较(也许使用反射),并找出哪些属性已更改以及它们的新值是什么。
但是,请注意,您无法对 .net 中的对象进行深层复制(除非您可以序列化对象),因此在此解决方案中请记住这一点

Perhaps you can implement the observer pattern, however, since .net implements this pattern implicitly (with events) i think there will not be much added value.

Perhaps you can save the "orginal" objetcs and compare them with the modified objects when time is right (perhaps using reflection) and find out what properties have changed and what their new value is.
However, note that you cannot make deepcopies of objects in .net (except if you can serialize the objects), so keep that in mind for this solution

苏辞 2024-08-01 04:55:03

您需要将原始数据保留在对象中,或者在更新之前从数据库中提取数据以进行记录。

我已经在数据层(存储过程或触发器)中看到过这种实现,但从未在域层中实现。

编辑:使用数据库触发器记录历史记录的两部分示例可在此处找到:https://web.archive.org/web/20210506085627/http://www.4guysfromrolla.com/webtech/041807-1.shtml 似乎有很多关于该技术的优点/缺点的良好讨论。 我希望这有帮助。

Either you need to keep the original data around in your object or pull it from the database for logging before the update.

I've seen this implemented in the data layer, either in stored procedures or triggers, but never in the domain layer.

Edit: A 2 part example of using database triggers to log history is found here: https://web.archive.org/web/20210506085627/http://www.4guysfromrolla.com/webtech/041807-1.shtml There seems to lots of good discussion on the pros/cons of this technique. I hope that helps.

蓝色星空 2024-08-01 04:55:03

您是否检查过 Microsoft 企业库的日志应用程序块

Have you checked out the Logging Application Block of the Microsoft Enterprise Library?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文