2013-03-19 182 views
5

这是一个基本的命名约定问题,但我没有找到具体处理这个问题的任何地方。服务方法的命名约定

我有一个名为Foo的类和一个名为Bar的类。

我有一个方法在服务中检索Foo的所有酒吧。我是否应该将其命名为:

GetFooBars(int fooId) 

GetBarsForFoo(int fooId) 

扩大,你可以有其他类,如酒吧

GetMooBars(int mooId) 

GetBarsForMoo(int mooId) 

回答

5

我建议

GetBarsByFooId(int fooId) 

GetBarsByMooId(int mooId) 

或者 ...调整您的API来支持这样的

[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); 
+0

谢谢,我喜欢你指定的“...... FooId”。我唯一需要注意的是,当我看到“By”而不是“For”时,它让我觉得我得到了很多(全部)酒吧,这些酒吧被FooId(或MooId)细分或排序。或者,当我看到“For”时,它让我觉得我只是为了一个特定的FooId(或MooId)而获得所有酒吧的子集。 – 2013-03-19 17:50:41

+0

当我看到一个像GetBarsByFooId这样的名字时,它让我觉得“过滤”或“搜索”。我从来没有想到,这可能意味着“分组”或“排序”。但那只是我......我想你传递'fooId'作为参数的事实应该澄清这个意图。 – 2013-03-19 18:17:39

2

我会打电话宁可使用一个o f以下:

GetBarsByFoo(int fooID) { } 

GetBarsByMoo(int mooID) { } 

GetBarsByFooId(int fooID){ } 

GetBarsByMooId(int mooID){ } 
+0

重复'GetBarsByMooId'。我想你的意思是'GetBarsByMoo'。 – 2013-03-19 16:31:36

+0

你是完全正确的,感谢您的评论。 :) – Abbas 2013-03-19 18:08:33