如何在没有 switch 结构的情况下重构和重新设计类结构的实现?

发布于 2024-11-17 06:20:54 字数 1820 浏览 1 评论 0原文

我使用以前编写的代码,并且想要进行重构。问题如下: 在具有多个用户角色的 ASP.NET MVC 应用程序中,应显示每个用户角色的个人详细信息页面,其中包含一些可应用于特定用户角色的操作的菜单,对于不同的用户角色,菜单中的操作是不同的,例如: 用户以管理员身份登录并希望查看学习者的个人详细信息页面(使用角色),学习者菜单项将显示

  1. 更改密码
  2. 简历
  3. 分配资格

如果用户使用其他角色登录,例如评估员(用户角色) 菜单将显示其他项目

  1. 更改密码
  2. 进度
  3. 分配资格
  4. 分配单位

,如果用户想查看自己的个人详细信息页面,菜单将显示其他项目。

主要思想是,特定用户角色的菜单项有所不同,用户角色数量约为 7 个。并且用户查看其他用户的个人详细信息页面时存在不同的组合。

目前代码看起来像

    public class LearnerMenuBuilder 
{

    public LearnerMenuBuilder(UserRole userRole, UserRole loggedUserRole)
        : base(userRole, loggedUserRole)
    {

    }

    public UserItemMenu Build()
    {
        var ret = new UserItemMenu();

        switch (UserTypeHelper.GetTypeNameOf(currentUserRole.GetType()))
        {
            case Const.LearnerRole:
                ret.Items = GetLearnerMenu();
                return ret;

            case Const.AssessorRole:
                ret.Items = GetMenuForAssessor();
                return ret;
            case Const.InternalVerifierRole:
            case Const.QualityAdviserRole:
            case Const.ManagerRole:
            case Const.NonEdiQualityAdviserRole:
                ret.Items = GetMenuForInternalVerifier();
                return ret;

            case Const.CentreAdministratorRole:
                ret.Items = GetMenuForCentreAdmin();
                return ret;
            case Const.SystemAdministratorRole:
                ret.Items = GetMenuItemsForCentreSupport();
                return ret;
            default:
                return ret;
        }
    }

  //private methods for add items to menu

}

}

从这个类中,您可以看到对于每个用户角色都有 switch case 语句,我想重构它并删除 switch 构造。

如何高效地重构和重新设计代码?

请帮我提供一些建议或解决方案。

提前致谢!

I work with a code that has been written before and I want to made refactoring . The problem is the follow:
In a ASP.NET MVC Application with multiple user roles for each user role should be displayed the personal details page with the menu with some actions that can be apply for specific user role, for different user role the actions from menu is different for example:
User logged in as Admin and wants to look at the personal details page for Learner (use role) the Learner menu items will show

  1. Change password
  2. CV
  3. Assign qualifications

If user logged in with other role for example Assessor (user role)
the menu will display other items

  1. Change password
  2. Progress
  3. Assign qualifications
  4. Assign units

and if user want to look own personal details page the menu will show with other items.

The main idea that the menu items is different for specific user roles the number user roles about 7. and there are different combinations which user over look personal details page of other user.

At the moment the code look like

    public class LearnerMenuBuilder 
{

    public LearnerMenuBuilder(UserRole userRole, UserRole loggedUserRole)
        : base(userRole, loggedUserRole)
    {

    }

    public UserItemMenu Build()
    {
        var ret = new UserItemMenu();

        switch (UserTypeHelper.GetTypeNameOf(currentUserRole.GetType()))
        {
            case Const.LearnerRole:
                ret.Items = GetLearnerMenu();
                return ret;

            case Const.AssessorRole:
                ret.Items = GetMenuForAssessor();
                return ret;
            case Const.InternalVerifierRole:
            case Const.QualityAdviserRole:
            case Const.ManagerRole:
            case Const.NonEdiQualityAdviserRole:
                ret.Items = GetMenuForInternalVerifier();
                return ret;

            case Const.CentreAdministratorRole:
                ret.Items = GetMenuForCentreAdmin();
                return ret;
            case Const.SystemAdministratorRole:
                ret.Items = GetMenuItemsForCentreSupport();
                return ret;
            default:
                return ret;
        }
    }

  //private methods for add items to menu

}

}

From this class you can that for each user role there is switch case statement, and I want to refactor this and remove the switch construction.

How can do refactoring and redesign of the code in efficient way ?

Help me with some suggestions or solution, please.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-11-24 06:20:54

查看站点地图和角色提供程序

Look in to the SiteMap and the Roles Provider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文