So I understand the importance of Polymorphism, including how vital it is. But something I don’t quite understand is what about the Constructor
and any inherited Class
the initial Base Class
may have.
For example, I have a class to test some criteria- Level of account, Windows Version, and some other checks.
// Base Class
public class CheckInstalled : CheckUserLevel
{
public CheckInstalled()
{
// Check verify the CheckUserLevel token is true, if it isn't throw an exception and close.
if (!(_level == true))
{
// The above implementation would technically go here.
}
}
public virtual void IsInstalled()
{
// Implementation of initial check of general software.
}
}
So that would in theory be the Base Class
which will do a Generic check; but there are other applications I need to test if they are installed. So now I would create a Class
for:
- Sql
- IIs
- Dnn
Or whatever you may need to ensure is checked. So for each of those Classes
I’d inherit the CheckInstalled Class
. In each one I’d have this:
public override void IsInstalled()
{
// Implementation to check for that specific Class Item is installed.
}
So I see how Polymorphism will really simplify and keep each one of these Classes
separate. But will provide that flexibility for Object Orientation.
The root of my question, the Base Class Constructor
contains that inherited token value. But do all other Classes
that inherit the value? Essentially that Base Class Constructor
consistently execute? Or does the _level
token have to be called in each?
Hopefully that makes sense. If not please let me know and I’ll try and rephrase it.
Update:
The _level is invoked here:
class CheckUserLevel
{
// Token Bool:
private bool _level = false;
#region Constructor:
protected CheckUserLevel()
{
// Invoke Method On Creation:
Elevate();
}
#endregion
public void Elevate()
{
// Get Identity:
WindowsIdentity user = WindowsIdentity.GetCurrent();
// Set Principal
WindowsPrincipal role = new WindowsPrincipal(user);
#region Test Operating System for UAC:
if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
{
// False:
_level = false;
// Todo: Exception/ Exception Log
}
#endregion
else
{
#region Test Identity Not Null:
if (user == null)
{
// False:
_level = false;
// Todo: "Exception Log / Exception"
}
#endregion
else
{
#region Ensure Security Role:
if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
{
// False:
_level = false;
// Todo: "Exception Log / Exception"
}
else
{
// True:
_level = true;
}
#endregion
} // Nested Else 'Close'
} // Initial Else 'Close'
} // End of Class.
}
The separate class actually generates the boolean value; as anytime that class is invoked it will assign a value to it. I didn’t want to expose too much with this class mostly because of its role in testing for Administration.
The problem:
Several methods will require Administrative Permission; my thought was to inherit on the base
CheckInstalled
. Which in it’s creation
will automatically check the boolean state of_level
. Then from
that point; thisCheckInstalled
can be inherited to other classes;
then I can simplyoverride IsInstalled
to bend to the desired task
for that item.
Is that a bad thought on my part?
2
Two points; Firstly the constructor should generally be used for initial setup only. In your case it would loading any dependencies and suchlike, not doing the actual check.
Secondly (and more importantly) inheritance should be used in a case when you can share code between similar classes. This case dosent promote code reuse. Its a very good candidate for Interface usage, you can define an interface with CheckInstalled() then you can decouple the specific implementation. (for more reading have a look at the strategy pattern)
3
// Base Class
internal class CheckInstalled : CheckUserLevel
{
public CheckInstalled()
{
// Check verify the CheckUserLevel token is true, if it isn't throw an exception and close.
if (!Verified)
{
// The above implementation would technically go here.
}
}
public virtual void IsInstalled()
{
// Implementation of initial check of general software.
}
}
internal class CheckUserLevel
{
//Derived classes inherit the Verified property
public bool Verified {get;set;}//Verified is more indicative of the action. _level implies an enumeration of sorts
public CheckUserLevel()//the base constructor is called prior to the derived constructor. {
Verify();//Caution: long running operations should not go in constructors
}
private void Verify()
{
bool verified = true;
//do work
Verified = verified;
}
}
The base constructor is called anytime a derived class is instantiated. For this and other reasons, CheckUserLevel should be a has-a relationship. In other words, CheckuserLevel should be a Member of CheckInstalled.
However, because it appears that you do not want to bind verification to construction…
Or does the _level token have to be called in each?
…a static Member makes more sense.
In other words convert to:
// Base Class
internal class CheckInstalled
{
public CheckInstalled()
{
// Check verify the CheckUserLevel token is true, if it isn't throw an exception and close.
if (!CheckUserLevel.Verified)
{
// The above implementation would technically go here.
}
}
public virtual void IsInstalled()
{
// Implementation of initial check of general software.
}
}
internal class CheckUserLevel
{
public static bool Verified {get;set;}
static CheckUserLevel()
{
Verify();
}
private static void Verify()
{
bool verified = true;
//do work
Verified = verified;
}
}
1