FxCop的CollectionPropertiesShouldBeReadOnly规则与spring框架不兼容吗?

发布于 2024-07-06 23:45:57 字数 758 浏览 7 评论 0原文

FxCop 有 CollectionPropertiesShouldBeReadOnly 规则,该规则会在您的类具有某种集合属性时发出抱怨客户可以设置。 相反,它建议将该属性设置为只读,并提供 Clear() 方法和 Add() 或 AddRange() 方法来更改集合的内容。

我同意这会产生一个更干净、更受控制的界面,但我正在努力使该界面与 Spring 框架一起工作。 如果我想用协作者集合配置一个对象,我必须公开一些集合属性以将协作者注入其中。 我浏览了 Spring 文档,我可以没有看到任何方法告诉 Spring 调用 AddRange() 方法,我错过了什么吗?

现在,我将排除该警告,并指出它对于 Spring 配置是必需的。

更新:由于过去两个月我没有在这里得到任何啃咬,所以我在 FxCop 论坛

FxCop has the CollectionPropertiesShouldBeReadOnly rule that complains if your class has some kind of collection property that clients can set. Instead, it suggests making the property read-only and supplying a Clear() method and Add() or AddRange() methods for changing the contents of the collection.

I agree that makes for a cleaner and more controlled interface, but I'm struggling to make that interface work with the Spring framework. If I want to configure an object with a collection of collaborators, I have to expose some collection property to inject the collaborators into. I've looked through the Spring documentation, and I can't see any way to tell Spring to call the AddRange() method, am I missing something?

For now, I'm going to exclude the warning with a note that it's necessary for Spring configuration.

Update: since I didn't get any nibbles here in the last two months, I posted the same question on the FxCop forum.

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

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

发布评论

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

评论(2

飘落散花 2024-07-13 23:45:58

问题有你想象的那么严重吗? 我的理解是,如果您有这样的读/写属性,FxCop 会抱怨:

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

...因为您班级的用户将能够执行此操作:

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

显然您不希望您班级的用户完全重新分配列表。 因此,FxCop 推荐这种模式:

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

因此,现在您的班级的用户只能在列表中添加和删除项目,而不是用新的 List 实例覆盖它。

Spring.NET如何实现其集合属性? 它们真的像我的第一个例子一样读/写吗? 如果是这样,那么看看他们的这种模式的用例会很有趣,因为它看起来不对。

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.

烟凡古楼 2024-07-13 23:45:57

如果集合属性仅公开一个 getter,我们将假设使用您列出的 FxCop 推荐模式并将其添加到集合中。 还支持第一种模式。

对于泛型集合,仅当公开的属性属于 IList 类型时,此方法才有效。 我们有一个 JIRA 问题,需要在下一个版本中修复此问题。 顺便说一句,这是基类库中非常常见的模式(您可能知道...),这是我们第一次在 .NET 1.1 中遇到需要支持这种样式的地方(它不受上面列出的限制) 。

干杯,
标记

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

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