检查对象是否被更改的最佳实践是什么? [英] What is the best practice to check if an object is changed?

查看:37
本文介绍了检查对象是否被更改的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何检查对象是否已更改.基本上,我需要一个名为TrackChanges的属性,将它设置一次为true,并且如果更改"了该对象中的任何数据,则同一对象(IsObjectChanged)上的方法可以返回true.

I need to know how do you check if an object is changed. Basically I need something like a property that is named TrackChanges, when I set it true once and if any data within this object is "changed", a method on the same object (IsObjectChanged) can return true.

您是否曾经需要过这样的东西,您是如何解决的?如果已经有针对这种情况的最佳实践,我是否不想发明轮子?

Did you ever needed such a thing and how did you solve it? I do not want to invent the wheel if there is already a best-practice for such scenario?

我曾想在我的设置器中调用TrackChange = true之前克隆该对象.当我调用IsObjectChanged()时,通过使用反射,我会将其所有公共字段值与克隆副本进行比较.我不确定这是否是个好方法.

I was thinking to clone the object before I call TrackChange=true, in its setter. And when I call IsObjectChanged() By using reflection I will compare all the public field values of it with the cloned copy. I am not sure if it is a good way.

有什么建议吗?

谢谢,burak ozdogan

thanks, burak ozdogan

推荐答案

当我需要跟踪对象的属性更改以进行测试时,我将事件处理程序挂接到对象的PropertyChanged事件上.这样对您有帮助吗?然后,您的测试可以根据更改执行所需的任何操作.通常,我会统计更改的数量,并将更改添加到字典等中.

When I need to track changes to properties on my objects for testing I hook an event handler on the objects PropertyChanged event. Will that help you? Then your tests can do whatever action they want based on the change. Normally I count the number of changes, and add the changes to dictionaries, etc.

要实现此目的,您的课程必须实现 INotifyPropertyChanged 界面.然后任何人都可以附加并收听更改后的属性:

To achieve this your class must implement the INotifyPropertyChanged interface. Then anyone can attach and listen to changed properties:

public class MyClass : INotifyPropertyChanged { ... }

[TestFixture]
public class MyTestClass
{
    private readonly Dictionary<string, int> _propertiesChanged = new Dictionary<string, int>();
    private int _eventCounter; 

    [Test]
    public void SomeTest()
    {
        // First attach to the object
        var myObj = new MyClass(); 
        myObj.PropertyChanged += SomeCustomEventHandler;
        myObj.DoSomething(); 
        // And here you can check whether the object updated properties - and which - 
        // dependent on what you do in SomeCustomEventHandler. 

        // E.g. that there are 2 changes - properties Id and Name changed once each: 
        Assert.AreEqual(2, _eventCounter); 
        Assert.AreEqual(1, _propertiesChanged["Id"]);
        Assert.AreEqual(1, _propertiesChanged["Name"]);
    }

    // In this example - counting total number of changes - and count pr property. 
    // Do whatever suits you. 
    private void SomeCustomEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        var property = e.PropertyName;
        if (_propertiesChanged.ContainsKey(property))
            _propertiesChanged[property]++;
        else
            _propertiesChanged[property] = 1;

        _eventCounter++;
    }
}

这篇关于检查对象是否被更改的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆