代码之家  ›  专栏  ›  技术社区  ›  stej

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

  •  0
  • stej  · 技术社区  · 16 年前

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

    我们有一个班 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 回复  |  直到 16 年前
        1
  •  0
  •   Vinko Vrsalovic    16 年前

    我同意格雷格的观点,但会使其更简单:

     u.GroupMembership();
    

    我认为在get这个动词后面加上get是没用的。 返回类型(组列表)

        2
  •  3
  •   J c    16 年前

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

    例如:

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

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

        3
  •  1
  •   Greg Hewgill    16 年前

    我想我会选择:

    u.GetGroupMembership()
    
        4
  •  1
  •   Owen Ryan Doherty    16 年前
    u.GetGroups()
    

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

        5
  •  1
  •   Panos    16 年前

    如果没有参数,我建议使用属性,例如

    u.Groups;
    

    u.UserGroups; // if Groups is ambiguous
    
        6
  •  1
  •   Yarik    16 年前
    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
                }
        }
    }
    

    这有什么意义吗?;-)

        7
  •  0
  •   dkl    16 年前

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

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

        8
  •  0
  •   Community CDub    8 年前

    根据你们所处的环境,你们可能能够利用现有的框架来完成这类工作,而不是滚动自己的框架。如果您使用的是.NET 2.0或更高版本,我建议您利用 System.Web.Security.RoleProvider 班级。我以前的回答对此有更多的想法 here .

        9
  •  0
  •   dkl    16 年前

    角色是扁平的,我们需要更强大的东西。我们也不想绑定到Web环境。