FxCop - 在适当的情况下使用属性
我在服务层中有多个以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须为每个方法添加属性。
You will have to add the attribute for each method.
我明白这里需要使用方法。 虽然这些方法确实可能不会更改状态,但使用方法暗示着冗长/向外的操作,这可能是通过服务类方法的情况。
您不能将方法重命名为 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.
不幸的是,您必须将其应用于每种方法。
另外,我在这里没有看到任何理由使用您的 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.