Jozef Izso
Jozef Izso

Reputation: 1804

FxCop - Use properties where appropriate

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);
}

Upvotes: 9

Views: 6569

Answers (3)

Bertvan
Bertvan

Reputation: 5033

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.

Upvotes: 4

casperOne
casperOne

Reputation: 74550

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.

Upvotes: 1

Sesh
Sesh

Reputation: 6192

You will have to add the attribute for each method.

Upvotes: 4

Related Questions