How to do refactoring and redesign implementations of class structure without switch counstruct?
I work with a code that has been written before a开发者_开发技巧nd 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!
Look in to the SiteMap and the Roles Provider
精彩评论