FxCop - 在适当的情况下使用属性

发布于 2024-07-13 23:01:53 字数 644 浏览 3 评论 0原文

我在服务层中有多个以 Get 开头的方法的接口,并且 FxCop 的 在适当的情况下使用属性 规则抱怨我应该考虑使用属性。

我尝试使用 SuppressMessageAttribute 但当它在接口上定义时,它对成员方法没有影响。 我是否需要将 SuppressMessageAttribute 放入每个方法中,或者是否有办法抑制整个类型的 CA1024

[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate"]
public interface IProjectService
{
    // Information and statistics about projects
    IList<ProjectInfo> GetProjects();
    ProjectsDashboard GetProjectsDashboard();

    // Project's settings
    ProjectSettings GetProjectSettings(Guid id);

    void SaveProjectSettings(ProjectSettings settings);
}

I have interface in service layer with several methods starting with Get and FxCop's Use properties where appropriate rule complains that I should consider using properties instead.

I tried to use SuppressMessageAttribute but when it's defined on interface it has no effect on member methods. Do I need to put SuppressMessageAttribute to every method or is there a way to suppress CA1024 for a whole type?

[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate"]
public interface IProjectService
{
    // Information and statistics about projects
    IList<ProjectInfo> GetProjects();
    ProjectsDashboard GetProjectsDashboard();

    // Project's settings
    ProjectSettings GetProjectSettings(Guid id);

    void SaveProjectSettings(ProjectSettings settings);
}

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

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

发布评论

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

评论(3

记忆里有你的影子 2024-07-20 23:01:54

您必须为每个方法添加属性。

You will have to add the attribute for each method.

野稚 2024-07-20 23:01:54

我明白这里需要使用方法。 虽然这些方法确实可能不会更改状态,但使用方法暗示着冗长/向外的操作,这可能是通过服务类方法的情况。

您不能将方法重命名为 LoadProjectSettings 吗?

否则,您确实必须向每个方法添加属性,或者禁用该规则。

I understand the need to use methods here. While it is true that these methods probably don't change the state, using a method hints at a lengthy/outward operation, which is probably the case through Service Class methods.

Can't you rename your methods to LoadProjectSettings ?

Otherwise you will indeed have to add the attribute to each method, or disable the rule.

七秒鱼° 2024-07-20 23:01:54

不幸的是,您必须将其应用于每种方法。

另外,我在这里没有看到任何理由使用您的 Get 方法。 为什么不只拥有只读属性,至少对于 ProjectsDashboard 和 IList 来说如此。 我认为这些实现不会修改实现的状态,并且无论如何都应该是属性。

ProjectSettings 还应该返回一个看起来也有索引的集合。

You will have to apply it to every method, unfortunately.

Also, I'm not seeing any reason here to have your Get methods. Why not just have read only properties, at least for the ProjectsDashboard and the IList<ProjectInfo>. Those don't strike me as implementations that would modify the state of the implementation, and should probably be properties anyways.

The ProjectSettings should also return an indexed collection it would seem as well.

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