Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi
I've been reading article about OOPs, and then I came across with Polymorphism which eliminate the if condition.
I would like to give it a try to implemente Polymorphism on my Login page.

2 users
1. ADMIN
2. Oridnary Users

If the role of the user is ADMIN it should redirect to Admin Page

How can I accomplish this kind of scenario without using if else condition ?

Hope someone Enlighten me with Polymorphism :)

Sorry for my bad English not too good with english :(


EDIT

Forgot my Login Condition
VB
If Login.isAuthorized() Then 'Says that Account is Registered           
            If Login.isAdminUser Then  'Redirect to Admin.aspx if Account role is Admin              
                Response.Redirect("~/Admin.aspx", False)
            Else
                If Login.isOrdinaryUser Then
                    Response.Redirect("~/OrdinaryUser.aspx", False)
                Else
                    lblError.Text = "Access Denied!"
                End If
            End If
        Else
            lblError.Text = "Access Denied!"
        End If
Posted
Updated 25-Nov-13 23:07pm
v2
Comments
VishwaKL 26-Nov-13 5:07am    
you can use web.config file to do this, here no polymorphism is required if you use below code it works
<configuration>
<system.web>
<authorization>
<deny users="?">


iMaker.ph 26-Nov-13 5:14am    
Hmm I prefer using Creating a class rather than using the web.config, maybe for Practices :) thanks anyway

Polymorphism doesn't work on roles: it works on classes.
So if you have a User class, and Admin class, then you can create a "GoHome" method that exhibitys polymorphism:
C#
public void GoHome(User u) { GoPage("userhome.aspx?u=" + u.Name); }
public void GoHome(Admin a) { GoPage("adminhome.aspx?u=" + a.Name); }

But you cannot use polymorphism based on the value of a variable:
C#
public void GoHome(userType==User) { GoPage("userhome.aspx?u=" + u.Name); }
public void GoHome(userType==Admin) { GoPage("adminhome.aspx?u=" + a.Name); }
It won't compile, or work.
 
Share this answer
 
Comments
iMaker.ph 26-Nov-13 5:11am    
What if their are 3 kinds of Admin, and each admin have different page assign ?
Admin level 1
Admin level 2
Admin level 3

BTW Thanks for fast reply

EDIT

I've already create a Class for the OrdinaryUser,Admin1,Admin2,Admin3 but I haven't figure it out how can I eliminate the If condition.. :(
OriginalGriff 26-Nov-13 5:22am    
Then they would need to be different classes, and if you are determined to use polymorphism here (and I wouldn't) then you need to provide a "GoHome" method that accepts each type of class, and does the job.

But I wouldn't do that.
I would create an abstract base class "SiteUser" which declares an abstract GoGome method.
The other classes would derive from SiteUser and would have to implement the GoHome method.
Then all you would have to do is:
SiteUser user = GetUser();
user.GoHome();
GetUser returns the appropriate class (User, Admin1, Admin2, etc.) and the appropriate GoHome method will be called automatically.
iMaker.ph 26-Nov-13 6:17am    
Hmm Seems that creating new object per Role would be the best solution.
I get the point of abstract Class and derive to its child class.

But am confuse with the GetUser whats its implementation o.O ?

Thank :)
OriginalGriff 26-Nov-13 6:45am    
It's just a placeholder for your code to return the appropriate class instance for the user when he logged in. I have no idea how you do all that, so I stuck in a dummy method so you could see where it goes.
iMaker.ph 26-Nov-13 7:00am    
Hmm Thanks for the Help, You've help a lot :) back to research and reading articles again :D
Quote:
Polymorphism doesn't work on roles: it works on classes.


Absolutely rite but we can do the same using lose coupling or interface

C#
public interface IUser
{
string GoHome();
}
public enum Level
{
Lvl_1,
Lvl_2,
Lvl_3
} 
public class Admin: IUser
{
public Level Level { get;set;} 
public string GoHome()
{
string URL=string.Empty;
      switch(Level)
      {
        case Lvl_1:
         URL="Your Preferd URL";
         break;
         ...
      }
 return URL;

}
}

// Calling 

void RedirectToHome(IUser User)
{
 Response.Redirect(User.GoHome());
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900