FxCop hates my use of MVVM

I just started working with FxCop to see how poorly my code works against its complete set of rules. I start with the Break rules, and the first thing I came across was CA2227, which basically says that you should make the collection properties set read-only so that you cannot accidentally change the collection data.

Since I use MVVM, it was very convenient for me to use an ObservableCollection with get / set parameters, because it simplifies and simplifies my GUI updates in code. However, I also see what FxCop is complaining about.

Another situation I came across is WF, where I need to set parameters when creating a workflow, and it would be awkward to write a wrapper class around the collection that I use to avoid this error message.

For example, here appears an example runtime error message that I get when I do readonly properties:

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

What do you think about this? I could either ignore this particular error, but this is probably not very good, because I could apparently break this rule elsewhere in the code where MVVM is not applied (for example, only model code). I think that I could also change it from a property to a class using methods to manage the base collection, and then raise the necessary notification from the setter method. I'm a little confused ... can anyone shed some light on this?

+3
1

, , .

, :

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 .

, , , , .

+7

Source: https://habr.com/ru/post/1739006/


All Articles