简体   繁体   English

派生类中属性变化的基类意识

[英]Base Class Awareness of Property Changes in Derived Classes

I was wondering if anyone had a possible solution to the following problem... I have a base class that a series of derived classes will inherit from. 我想知道是否有人可能解决以下问题...我有一个基类,一系列派生类将继承。 The base class cares about whether properties on the derived class have changed. 基类关心派生类的属性是否已更改。 This is to set up an "IsDirty" approach for a data transfer object. 这是为数据传输对象设置“IsDirty”方法。

The traditional approach would be to set an IsDirty boolean flag declared in the base class. 传统的方法是设置在基类中声明的IsDirty布尔标志。 I was hoping to avoid this somehow, and was wondering if there might be another approach? 我希望以某种方式避免这种情况,并想知道是否可能有另一种方法? I am concerned about a developer not remembering to set the flag in the equivalent set operation on a property in the derived class. 我担心开发人员不记得在派生类的属性上的等效集合操作中设置标志。 I had considered the idea of creating a generic "SetProperty" method that would take the property name and value, but thought that there might be something more elegant. 我曾考虑过创建一个通用的“SetProperty”方法,该方法将获取属性名称和值,但认为可能有更优雅的东西。

Just as a side note, ideas regarding using reflection are not a problem. 正如旁注,关于使用反射的想法不是问题。 Regardless of the expense of reflection, I would like to prove out a way to do it and work on refining it further on down the road. 不管反射费用如何,我想证明一种方法,并在未来的道路上进一步完善。

Can you force the derived classes to implement INotifyPropertyChanged? 你能强制派生类实现INotifyPropertyChanged吗? In that case I'd add a listener to the PropertyChanged event in the base class constructur and chill. 在这种情况下,我会在基类构造函数中添加一个监听器到PropertyChanged事件并放松。 The derived class now still needs to follow the pattern (which might be something you want to avoid?). 派生类现在仍然需要遵循模式(这可能是你想要避免的东西?)。

Nice read for a "no literal strings used" approach for raising the property changed events: http://monotorrent.blogspot.com/2009/12/yet-another-inotifypropertychanged-with_06.html 很好的阅读“没有文字字符串使用”的方法来提高属性改变事件: http//monotorrent.blogspot.com/2009/12/yet-another-inotifypropertychanged-with_06.html

An appropriate pattern for controlling access to a class (or a set of classes) is the Proxy pattern. 用于控制对类(或一组类)的访问的适当模式是代理模式。 This pattern would allow you to implement the correct isDirty behavior once at the proxy level and delegate any read-only behavior to the real instance unchanged. 此模式允许您在代理级别实现一次正确的isDirty行为,并将任何只读行为委派给未更改的实例。

This pattern is most effective when used with a Factory pattern to control creation of the instances. 与Factory模式一起使用以控制实例的创建时,此模式最有效。

This solution will mask the run-time type of the instance, so if your design relies on run-time type information you will either need to solve this problem separately (eg with the State pattern) or find an alternative approach. 此解决方案将屏蔽实例的运行时类型,因此如果您的设计依赖于运行时类型信息,您将需要单独解决此问题(例如,使用State模式)或找到替代方法。

(Quick & Dirty) - What about something like this? (快与肮脏) - 这样的事情怎么样? (Your implementation of GetHashCode may vary - I used ReSharper to auto-generate this one) (你的GetHashCode实现可能会有所不同 - 我使用ReSharper自动生成这个)

    public abstract class Animal
    {
        private int _originalHash;

        public string Name { get; set; }
        public int Age { get; set; }

        public Animal(string name, int age)
        {
            this.Name = name;
            this.Age = age;

            this._originalHash = GetHashCode();
        }


        public override sealed int GetHashCode()
        {
            unchecked
            {
                return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ Age;
            }
        }

        public bool IsDirty
        {
            get
            {
                return this._originalHash != GetHashCode();
            }
        }
    }

    public class Cat : Animal
    {
        public Cat(string name, int age)
            : base(name, age)
        {
        }
    }

Used this to test... 用这来测试......

var cat = new Cat("BobTheCat", 12);
Console.WriteLine(cat.IsDirty);
cat.Age = 13;
Console.WriteLine(cat.IsDirty);

Results were: 结果是:

False True 假真

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM