设置属性时设置属性... [英] Setting properties when setting a property...

查看:78
本文介绍了设置属性时设置属性...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个(可能)很常见的问题。我是谷歌,但我不确定谷歌会做些什么。

我遇到的问题如下:



我有具有一些属性的类,比如PropertyA,PropertyB和PropertyC。

当我设置PropertyA时,我希望PropertyB和PropertyC获取某些值(可能取决于PropertyA的新值)。我知道在属性设置器中做很多工作并不是一个好习惯,但我不知道怎么做(否则我正在使用WinForms绑定)。

所以无论如何,代码看起来有点像这样:

Hello all,
I'm having a (probably) very common problem. I'd Google, but I'm not sure what to Google for.
The problem I have is as follows:

I have a class with a few properties, say PropertyA, PropertyB and PropertyC.
When I set PropertyA I want PropertyB and PropertyC to get certain values (probably dependent on the new value of PropertyA). I know it's not a good practice to do a lot of work in a property setter, but I wouldn't know how to do it otherwise (I'm using WinForms binding).
So anyway, the code looks a little like this:

public string PropertyA
{
   get
   {
      return _PropertyA;
   }
   set
   {
      _PropertyA = value;
      if (value != null)
      {
         this.PropertyB = this.GetPropertyBValue();
         this.PropertyC = this.GetPropertyCValue();
      }
      else
      {
         this.PropertyB = null;
         this.PropertyC = null;
      }
}

到目前为止一直都很好,但随着我获得更多属性,我往往会遇到更多错误。我忘了重置值,或者PropertyC的setter需要设置PropertyD的值和PropertyD的setter设置PropertyB(我们已经在PropertyA中设置)等等。过了一会你甚至可能得到StackOverflowExceptions(例如我有一个ProductionDate,一个PreservableDate和一个DaysPreservable属性,每个属性相互设置。)

我想你可以猜到结果。代码变得难以理解,你永远不能确定哪个setter是从哪个setter触发的...

我看到这个方法的唯一优点是它很容易调试(当然这很糟糕)我需要调试它这么多!)。



有没有解决这个问题的方法,也许是一种设计模式?

谢谢。

So far so good, but as I get more properties I tend to get more bugs. I forget to reset values, or the setter of PropertyC needs to set the value of PropertyD and the setter of PropertyD sets PropertyB (which we have already set in PropertyA), etc. After a while you might even get StackOverflowExceptions (for example I have a ProductionDate, a PreservableDate and a DaysPreservable property that each set each other).
I think you can guess the result. The code becomes unreadable and you're never quite sure which setters are triggered from which setters...
The only plus I see to this method is that it's quite easy to debug (of course it's to bad I need to debug it so much!).

Is there any method, perhaps a design pattern, that addresses this specific issue?
Thanks.

推荐答案

您的代码没有任何问题。您的问题是代码的一般设计问题。你几乎找不到任何奇迹技术或设计模式,因为基本的想法是非常基本的,你的交叉依赖属性的问题太过具体到你个人设计你的类的方式;它与任何众所周知的问题无关。



让我们假设我们进行预OOP编程。你会尝试谷歌的问题我有越来越多的功能,他们都有不同数量的参数,许多功能使用其他几个功能;因为我添加了更多的功能,代码变得不可读;相互依赖的功能,我甚至有堆栈溢出异常......?现在想一想:这种情况怎么会与你的不同?不多。毕竟,所有这些属性都只是函数(有一个setter的输入参数和一个getter的返回值,加上一个隐含的this参数),只隐藏在属性读取和属性赋值语法之后。



修复你的错误。彻底规划您的课程及其成员。确定哪些约束非常重要。找出最重要的功能并专注于它们。预见你的课程的使用。等等。



-SA
There is nothing wrong with your code. Your problem is the matter of general design of your code. You hardly can find any miracle technique or design pattern, because the basic idea is very elementary, and your problem with cross-dependent properties is way too specific to the way you personally design your classes; it is not related to any commonly known problem.

Let's imagine we do pre-OOP programming. Would you try to Google for the problems "I have more and more functions, they all have different number of parameters, and many of the functions use several other functions; as I added more functions, the code becomes unreadable; for mutually depending functions, I even have stack overflow exceptions…"? Now think about this: how such situation can be different from yours? Not much. After all, all those properties are nothing but functions (with one input argument for setters and one return values for getters, plus one implicit "this" argument for both), only hidden behind the property reading and property assignment syntax.

Fix your bugs. Thoroughly plan your classes and their members. Decide what constraints are really important. Find out most important features and focus on them. Foresee the use of your classes. And so on.

—SA


克服这种循环的正确方法是定义每个属性的变量设置这些变量而不是属性本身...

The proper way to overcome such a loop is define a variable for every property an set those variables instead of the property itself...
private string _propA;
public string propA
{
  get {}
  set {
    _propB = value;
    _propC = value;
  }
}

这条线上的东西..



我还建议你写一个方法来做所有更新并从所有属性中调用它,而不是在每个setter中设置它们...





我在复杂场景中使用过的第二个选项是仅在请求时为核心属性和计算依赖属性设置脏标志...

Something along this line..

I also advise you to write a single method to do all the updates and call it from all the properties, instead of setting them in each and every setter...


A second options I used once in a complex scenario is to set a dirty flag for the core property and compute dependent properties only upon request...

private string _propA;
private bool _propAIsDiry;
public string propA
{
  get {}
  set {
    _propAIsDirty = true;
  }
}
public string propB
{
  get {
    if( _propAIsDiry) ComputePropB();

    return( _propB );
  }
  set {}
}


请,阅读我的评论解决方案1 ​​ - > PIEBALDconsult评论。



这是一个想法:

如何:实施财产变更通知 [ ^ ]

如何:在班级中实施活动 [ ^ ]
Please, read my comment to the solution1->PIEBALDconsult comment.

Here is an idea:
How to: Implement Property Change Notification[^]
How to: Implement Events in Your Class[^]


这篇关于设置属性时设置属性...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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