VERY new to C#, using Visual Studio 2010. In my old PHP, to build this menu, what I would do is have 2 arrays of hyper-links. Level1 array would be displayed if a ‘requester’ logs in, Level1 AND Level2 array would be merged and sorted and displayed if an ‘administrator’ logs in.
In C#, on my default page (which has a Site.Master page), I find myself doing this:
case NewCourseRequestView.Administrator:
if (access.Administrator)
{
UserTypeLabel.Text = "Administrator Details:";
AdministratorMenuList.Items.Add("View Un-Approved Requests");
adminContent.Visible = true;
}
break;
case NewCourseRequestView.Requestor:
if (access.Requestor)
{
UserTypeLabel.Text = "Requester Details:";
RequestorMenuList.Items.Add("Request A New Course");
RequestorMenuList.Items.Add("View My New Course Requests");
requestContent.Visible = true;
}
break;
How can I
- have these list items act as hyper-links AND
- have these 2 lists in 2 separate arrays (collections?) and merge them if I have access.Administrator?
I need a fairly specific explanation, thanks!
I suggest you to start from the design first, no matter what language you’re using.
Imagine you will need to add one more User Type (for example, “Guest”) which has different
set of menu items.
What needs to be done then? Adding one more “if” statement and, basically copy-pasting.
This is not good at all. And now imagine what happens when you have 10 user types.
Your switch-case statement will become giant monster and everyone in your team (even you) will be afraid to add a new Item there or change an existing one.
Ok, that was sad, let’s have some fun now 🙂
Ideally, your “client” code, which I suppose you’re going to place into the Page_Load method will have only one line:
How cool it is to have only one line in your method and have everything “magically” sorted out.
Let’s say that we have some kind of a policy which regulates available items for our user types.
The basic interface is pretty simple:
At the moment, we have two user types, so we will have exactly two implementations.
The Requestor:
And an Administrator (pay attention where the “merge” is)
As you can see, we retrieve Requestor’s items and then adding one more item there.
If this needs to be changed in future, your client/calling code will know nothing about
the implementation details because you depend on abstract entity. That means you’ll need no changes in your client/calling code the rules will be changed; or if available links for a particular user type will need to be changed in future.
To simplify the example, I have used enum for User Types:
And now, let’s have a look on the implementation.
I have placed a simple and standard asp.net menu control onto the page:
And Here’s the “codebehind”:
I hope, the example above answers your questions.
The example, of course, is not ideal. However, it helps to keep your code relatively clean and maintainable.
P.S. I do recommend you to go through the article:
http://objectmentor.com/resources/articles/Principles_and_Patterns.pdf
refer to the OCP example – you’ll see, how close it is to your situation.
Thanks for reading 🙂