逻辑的属性获取一部分。好的做法呢? [英] Logic in get part of property. Good practice?

查看:132
本文介绍了逻辑的属性获取一部分。好的做法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据绑定我的XAML一些数据我经常使用的一个属性的得到的部分做一些逻辑。 。就像给总结列表或如果事情是肯定的支票总计

When databinding my xaml to some data I often use the "get" part of a property to do some logic. Like giving to sum of totals of a list or a check if something is positive.

例如:

public List<SomeClass> ListOfSomeClass{get;set;}

public double SumOfSomeClass
{
  get
  {
    return ListOfSomeClass.Sum(s => s.Totals);
  }
}

public bool SumPositive
{
  get
  {
    if(SumOfSomeClass >= 0)
      return true;
    else
      return false;
  }
}



这样我可以绑定到SumPositive和SumOfSomeClass。这被认为是好的做法呢?即使它得到比这更复杂?或者,它会更好调用一个方法并返回结果?关于调用另一个类,甚至数据库是什么?

This way I can bind to SumPositive and SumOfSomeClass. Is this considered good practice? Even if it gets more complex than this? Or would it be better call a method and return the outcome? What about calls to another class or even a database?

推荐答案

属性获取预期要快的和幂等(即无破坏性操作应那里执行)。虽然这是完全正常的迭代内存中的集合对象,我不会做电子书籍任何繁重的工作在任何的获得的或的设置的部分。和迭代的来讲,我还是会缓存结果,以节省几毫秒。

Property getters are expected to be fast and idempotent (i.e. no destructive actions should be performed there). Though it's perfectly fine to iterate over an in-memory collection of objects, I wouldn't recomment doing any kind of heavy lifting in either get or set parts. And speaking of iterating, I'd still cache the result to save a few milliseconds.

这篇关于逻辑的属性获取一部分。好的做法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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