Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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:

  1. Sql
  2. IIs
  3. 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; this CheckInstalled can be inherited to other classes; then I can simply override IsInstalled to bend to the desired task for that item.

Is that a bad thought on my part?

share|improve this question
1  
Where is _level declared? –  pdr Feb 1 '13 at 18:07
    
I've added the CheckUserLevel class which indicates how the value is filled. –  Greg Feb 1 '13 at 19:16
add comment

2 Answers

up vote 4 down vote accepted

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)

share|improve this answer
    
Thanks I'll take a look. I was essentially in this boat, I can't perform this task without proper Administration level. But I have a series that is similar to each other. But those similar classes need to ensure that Administration level first. –  Greg Feb 1 '13 at 19:10
    
Sounds like your check admin level code can either be moved into a separate class or delegate. Its best to keep your classes just doing a single thing (single responsibility principal.) –  Tom Squires Feb 4 '13 at 14:11
    
Well, I do have it as a separate class already. I could simply call the class; I think I found the exact goal of what I was trying to do. Which I'll post later today. –  Greg Feb 4 '13 at 22:31
add comment
// 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;
    }
}
share|improve this answer
    
I appreciate your reply, but I don't quite follow. I understand what you mean I think. My goal was to have a Derived Class to fill the value; your Property implementation is a nice way. But the problem is before a series of methods can invoke they need to have Administrative permission; but I didn't want to attach the class to each additional class; that was why I wanted to inherit to the Base Class then simply inherit the physical object with its similarities on all the other classes; then override them to test for IsInstalled. Is that wrong thought process? –  Greg Feb 1 '13 at 19:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.