开发者

How to do refactoring and redesign implementations of class structure without switch counstruct?

开发者 https://www.devze.com 2023-03-15 12:35 出处:网络
I work with a code that has been written before a开发者_开发技巧nd I want to made refactoring . The problem is the follow:

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

  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!


Look in to the SiteMap and the Roles Provider

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号