列出当前用户所属的所有用户组的方法名称

时间:2008-10-14 07:44:40

标签: coding-style

我们对方法名称进行了热烈的讨论。

我们有一个班级User。用户上有一个名为“Groups”的属性。它包含直接包含用户的所有组。没关系。我们遇到的问题是,递归列出所有用户组及其“父”组的方法名称,并返回所有组的列表,用户可以将其视为成员。

User u = <get user>;
IList<UserGroup> groups = u.XYZ();

Console.WriteLine("User {0} is member of: ", u);
foreach(UserGroup g in groups) 
   Console.WriteLine("{0}", g);

我的同事带来了:

u.GetAllGroups();       // what groups?
u.GetMemberOfGroups();  // doesn't make sense
u.GroupsIAmMemberOf();  // long
u.MemberOf();           // short, but the description is wrong
u.GetRolesForUser();    // we don't work with roles, so GetGroupsForUser ?
u.GetOccupiedGroups();  // is the meaning correct?

你会提出什么名字?

9 个答案:

答案 0 :(得分:3)

为了high cohesion and low coupling的利益,我建议将该功能完全保留在User类之外。如果该功能属于不同的类,则为多个调用实现缓存也应该更容易。

例如:

User u = <get user>;
IList<UserGroup> groups = SecurityModel.Groups.getMembership(u);

然后,您可以选择在Groups对象中缓存组/用户成员身份,从而提​​高其他用户未来组成员身份请求的效率。

答案 1 :(得分:1)

我想我会选择:

u.GetGroupMembership()

答案 2 :(得分:1)

u.GetGroups()

除非你的应用程序中的组的含义有些含糊不清,我想。 (我喜欢尽可能少打字!)

答案 3 :(得分:1)

鉴于没有参数,我建议使用一个属性,例如

u.Groups;

u.UserGroups; // if Groups is ambiguous

答案 4 :(得分:1)

if (the signature of the property Groups cannot be changed)
{
    I think you are screwed
    and the best thing I can think of
    is another property named AllGroups
    // u.Groups and u.GetWhatever() look very inconsistently
}
else
{
    if (you are okay with using the term "group")
    {
        I would select one of these variants:
            {
                a pair of properties named ParentGroups and AncestorGroups
            }
            or
            { 
                a parameterized method or property Groups(Level)
                where Level can be either PARENTS (default) or ANCESTORS
            }
    }
    else
    {
        I would consider replacing "group" with "membership"
        and then I would select one of these variants:
            {
                a pair of properties named DirectMemberships and AllMemberships
            }
            or
            { 
                a parameterized method or property Memberships(Level)
                where Level can be either DIRECT_ONLY (default) or ALL
            }
    }
}

这有什么意义吗? ; - )

答案 5 :(得分:0)

我同意格雷格,但会更简单:

 u.GroupMembership();

我认为附加动词Get是有用的,给定  返回类型(组列表)

答案 6 :(得分:0)

我来自Stej的团队:-)用户已经有一个名为“Groups”的属性。它包含直接包含用户的所有组。没关系。

我们遇到的问题是,递归列出所有用户组及其“父”组的方法名称,并返回所有组的列表,用户可以将其视为成员。

答案 7 :(得分:0)

根据您的工作环境,您可以利用现有的框架来做这类事情而不是自己动手。如果您使用的是.NET 2.0或更高版本,我建议您使用System.Web.Security.RoleProvider类。我之前的回答对此here有更多的想法。

答案 8 :(得分:0)

角色是平的,我们需要更强大的东西。我们也不想受到网络环境的束缚。