Is FxCop's CollectionPropertiesShouldBeReadOnly rule incompatible with the spring framework?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 14:07:44

If the collection property only has a getter exposed, we will assume the FxCop recommended patterns you list is used and add to the collection. The first pattern is also supported.

For generic collections this is only working if the exposed property is of the type IList. We have a JIRA issue for the next release to fix this. BTW, this is a very common pattern in the base class libraries (as you probably know...) which is where we first encountered the need to support this style in .NET 1.1 (which doesn't suffer from the limitation listed above).

Cheers, Mark

Is the problem as bad as you think? My understanding is that FxCop will complain if you have a read/write property like this:

public List<Foo> Items { get; set; }

... because users of your class would then be able to do this:

myInstance.Items = new List<Foo>();

Obviously you don't want users of your class to completely reassign the list. FxCop therefore recommends this pattern:

private List<Foo> _items = new List<Foo>();
public List<Foo> Items { get { return _items; } }

So now users of your class can only Add and Remove items from your list, rather than overwriting it with a new instance of List.

How does Spring.NET implement its collection properties? Are they really read/write like my first example? If so, it'd be interesting to see their use-cases for such a pattern, because it doesn't seem right.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!