FxCop hates my usage of MVVM

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2010-03-29T16:25:12Z Indexed on 2010/03/29 16:43 UTC
Read the original article Hit count: 596

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?

© Stack Overflow or respective owner

Related posts about fxcop

Related posts about mvvm