Naming convention for a service method

This is the main naming question, but I haven’t found anywhere that specifically addressed this.

I have a class called Foo and a Bar class.

I have a method in the service to retrieve all the bars for Foo. Should I call him:

GetFooBars(int fooId)

or

GetBarsForFoo(int fooId)

To expand, you can have Bars for other classes, for example.

GetMooBars(int mooId)

or

GetBarsForMoo(int mooId)
+5
source share
2 answers

I would suggest

GetBarsByFooId(int fooId)

GetBarsByMooId(int mooId)

Or ... setting up your API to support a call like this

[DataContract]
[KnownType(typeof(GetBarsByFooIdRequest))]
[KnownType(typeof(GetBarsByMooIdRequest))]
abstract class GetBarsRequest
{
   ..
}

[DataContract]
sealed class GetBarsByFooIdRequest : GetBarsRequest
{
   [DataMember]
   public int FooID { get; set; }
}

sealed class GetBarsByMooIdRequest : GetBarsRequest
{
   [DataMember]
   public int MooID { get; set; }
}

GetBarsResponse GetBars(GetBarsRequest);
+5
source

I would prefer to use one of the following:

GetBarsByFoo(int fooID) { }

GetBarsByMoo(int mooID) { }

GetBarsByFooId(int fooID){ }

GetBarsByMooId(int mooID){ }
+2
source

All Articles