如何在没有 switch 结构的情况下重构和重新设计类结构的实现?
我使用以前编写的代码,并且想要进行重构。问题如下: 在具有多个用户角色的 ASP.NET MVC 应用程序中,应显示每个用户角色的个人详细信息页面,其中包含一些可应用于特定用户角色的操作的菜单,对于不同的用户角色,菜单中的操作是不同的,例如: 用户以管理员身份登录并希望查看学习者的个人详细信息页面(使用角色),学习者菜单项将显示
- 更改密码
- 简历
- 分配资格
如果用户使用其他角色登录,例如评估员(用户角色) 菜单将显示其他项目
- 更改密码
- 进度
- 分配资格
- 分配单位
,如果用户想查看自己的个人详细信息页面,菜单将显示其他项目。
主要思想是,特定用户角色的菜单项有所不同,用户角色数量约为 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
- Change password
- CV
- Assign qualifications
If user logged in with other role for example Assessor (user role)
the menu will display other items
- Change password
- Progress
- Assign qualifications
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看站点地图和角色提供程序
Look in to the SiteMap and the Roles Provider