【问题标题】:Name of method that lists all user groups where the current user is member of列出当前用户所属的所有用户组的方法名称
【发布时间】:2010-09-17 01:12:38
【问题描述】:

我们就方法名称进行了激烈的讨论。

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

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?

你会建议什么名字?

【问题讨论】:

    标签: coding-style


    【解决方案1】:

    我想我会选择:

    u.GetGroupMembership()
    

    【讨论】:

      【解决方案2】:
      u.GetGroups()
      

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

      【讨论】:

      • 同意 (+1)。除非用户和多个组之间存在另一种关系(例如,他们可以是多个组的创建者,而不是这些组的成员吗?),“getGroups”是完全明确的。
      【解决方案3】:

      我同意 Greg 的观点,但会更简单:

       u.GroupMembership();
      

      我认为附加动词 Get 有点没用,因为 返回类型(组列表)

      【讨论】:

        【解决方案4】:

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

        u.Groups;
        

        u.UserGroups; // if Groups is ambiguous
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

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

            例如:

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

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                【解决方案8】:

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

                【讨论】:

                  【解决方案9】:
                  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
                              }
                      }
                  }
                  

                  这有什么意义吗? ;-)

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-02-21
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2023-03-21
                    相关资源
                    最近更新 更多