FxCop 讨厌我使用 MVVM

发布于 2024-08-26 23:46:30 字数 606 浏览 2 评论 0原文

我刚刚开始与 FxCop 合作,看看我的代码在其完整规则集上的表现有多差。我从“破坏”规则开始,我遇到的第一个规则是 CA2227,它基本上表示您应该将集合属性的 setter 设置为只读,这样您就不会意外更改集合数据。

由于我使用 MVVM,我发现使用具有 get/set 属性的 ObservableCollection 非常方便,因为它使我的 GUI 更新在代码隐藏中变得简单而简洁。不过,我也能看出FxCop在抱怨什么。

我刚刚遇到的另一种情况是使用 WF,我需要在创建工作流时设置参数,并且我不想为我正在使用的集合编写一个包装器类,以避免出现此特定的错误消息。

例如,下面是我将属性设置为只读时收到的示例运行时错误消息:

The activity 'MyWorkflow' has no public writable property named 'MyCollectionOfStuff'

您对此有何看法?我可以忽略这个特定的错误,但这可能不好,因为我可能会在不适用 MVVM 的代码中的其他地方违反此规则(例如,仅模型代码)。我认为我还可以将其从属性更改为具有操作底层集合的方法的类,然后从 setter 方法发出必要的通知。我有点困惑......有人能解释一下吗?

I've just started to work with FxCop to see how poorly my code does against its full set of rules. I'm starting off with the "Breaking" rules, and the first one I came across was CA2227, which basically says that you should make a collection property's setter readonly, so that you can't accidentally change the collection data.

Since I'm using MVVM, I've found it very convenient to use an ObservableCollection with get/set properties because it makes my GUI updates easy and concise in the code-behind. However, I can also see what FxCop is complaining about.

Another situation that I just ran into is with WF, where I need to set the parameters when creating the workflow, and I'd hate to have to write a wrapper class around the collection I'm using just to avoid this particular error message.

For example, here's a sample runtime error message that I get when I make properties readonly:

The activity 'MyWorkflow' has no public writable property named 'MyCollectionOfStuff'

What are you opinions on this? I could either ignore this particular error, but that's probably not good because I could conceivably violate this rule elsewhere in the code where MVVM doesn't apply (model only code, for example). I think I could also change it from a property to a class with methods to manipulate the underlying collection, and then raise the necessary notification from the setter method. I'm a little confused... can anyone shed some light on this?

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

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

发布评论

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

评论(1

維他命╮ 2024-09-02 23:46:30

这个特定规则告诉我们,集合属性应该设置为只读,因为您不需要将整个集合分配给属性。

例如,想象一个这样的类:

public class Foo
{
   public ObservableCollection<int> Bar { get; set; }
}

如果在代码中的某个位置有以下行,会发生什么:

var f = new Foo();
f.Bar = new ObservableCollection<int>();
f.Bar.AddRange(new int[] { 1, 2, 3, 4 });
// ...
// Attaches and handlers to the collection events
// ...
f.Bar = new ObservableCollection<int>();
f.Bar.AddRange(new int[] { 5, 6, 7, 8 });

当执行最后两行代码时,附加的事件处理程序不会被触发,因为 Bar 属性有一个完全不同的对象。

另一方面,如果该属性是只读的,则事件将被触发,并且一切都会按预期运行。

What this specific rule tells is that a collection property should be made read-only because you don't need to assign a whole collection to a property.

For instance, imagine a class like this:

public class Foo
{
   public ObservableCollection<int> Bar { get; set; }
}

What would happen if somewhere in the code I have the following line:

var f = new Foo();
f.Bar = new ObservableCollection<int>();
f.Bar.AddRange(new int[] { 1, 2, 3, 4 });
// ...
// Attaches and handlers to the collection events
// ...
f.Bar = new ObservableCollection<int>();
f.Bar.AddRange(new int[] { 5, 6, 7, 8 });

When the last two lines of code are executed the attached event handlers would not be fired, because the Bar property has a complete different object.

On the other hand, if the property were read-only the events would be fired and everything would behave as expected.

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