Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 670547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:16:34+00:00 2026-05-14T00:16:34+00:00

Let’s assume that our system can perform actions, and that an action requires some

  • 0

Let’s assume that our system can perform actions, and that an action requires some parameters to do its work.
I have defined the following base class for all actions (simplified for your reading pleasure):

public abstract class BaseBusinessAction<TActionParameters> 
    : where TActionParameters : IActionParameters
{
    protected BaseBusinessAction(TActionParameters actionParameters)
    {
        if (actionParameters == null) 
            throw new ArgumentNullException("actionParameters"); 

        this.Parameters = actionParameters;

        if (!ParametersAreValid()) 
            throw new ArgumentException("Valid parameters must be supplied", "actionParameters");
    }

    protected TActionParameters Parameters { get; private set; }

    protected abstract bool ParametersAreValid();

    public void CommonMethod() { ... }
}

Only a concrete implementation of BaseBusinessAction knows how to validate that the parameters passed to it are valid, and therefore the
ParametersAreValid is an abstract function. However, I want the base class constructor to enforce that the parameters passed are always valid, so I’ve added a
call to ParametersAreValid to the constructor and I throw an exception when the function returns false. So far so good, right? Well, no.
Code analysis is telling me to “not call overridable methods in constructors” which actually makes a lot of sense because when the base class’s constructor is called
the child class’s constructor has not yet been called, and therefore the ParametersAreValid method may not have access to some critical member variable that the
child class’s constructor would set.

So the question is this: How do I improve this design?

Do I add a Func<bool, TActionParameters> parameter to the base class constructor? If I did:

public class MyAction<MyParameters>
{
    public MyAction(MyParameters actionParameters, bool something) : base(actionParameters, ValidateIt)
    {
        this.something = something;
    }

    private bool something;

    public static bool ValidateIt()
    {
       return something;
    }

}

This would work because ValidateIt is static, but I don’t know… Is there a better way?

Comments are very welcome.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:16:34+00:00Added an answer on May 14, 2026 at 12:16 am

    This is a common design challenge in an inheritance hierarchy – how to perform class-dependent behavior in the constructor. The reason code analysis tools flag this as a problem is that the constructor of the derived class has not yet had an opportunity to run at this point, and the call to the virtual method may depend on state that has not been initialized.

    So you have a few choices here:

    1. Ignore the problem. If you believe that implementers should be able to write a parameter validation method without relying on any runtime state of the class, then document that assumption and stick with your design.
    2. Move validation logic into each derived class constructor, have the base class perform just the most basic, abstract kinds of validations it must (null checks, etc).
    3. Duplicate the logic in each derived class. This kind of code duplication seems unsettling, and it opens the door for derived classes to forget to perform the necessary setup or validation logic.
    4. Provide an Initialize() method of some kind that has to be called by the consumer (or factory for your type) that will ensure that this validation is performed after the type is fully constructed. This may not be desirable, since it requires that anyone who instantiates your class must remember to call the initialization method – which you would think a constructor could perform. Often, a Factory can help avoid this problem – it would be the only one allowed to instantiate your class, and would call the initialization logic before returning the type to the consumer.
    5. If validation does not depend on state, then factor the validator into a separate type, which you could even make part of the generic class signature. You could then instantiate the validator in the constructor, pass the parameters to it. Each derived class could define a nested class with a default constructor, and place all parameter validation logic there. A code example of this pattern is provided below.

    When possible, have each constructor perform the validation. But this isn’t always desirable. In that case, I personally, prefer the factory pattern because it keeps the implementation straight forward, and it also provides an interception point where other behavior can be added later (logging, caching, etc). However, sometimes factories don’t make sense, and in that case I would seriously consider the fourth option of creating a stand-along validator type.

    Here’s the code example:

    public interface IParamValidator<TParams> 
        where TParams : IActionParameters
    {
        bool ValidateParameters( TParams parameters );
    }
    
    public abstract class BaseBusinessAction<TActionParameters,TParamValidator> 
        where TActionParameters : IActionParameters
        where TParamValidator : IParamValidator<TActionParameters>, new()
    {
        protected BaseBusinessAction(TActionParameters actionParameters)
        {
            if (actionParameters == null) 
                throw new ArgumentNullException("actionParameters"); 
    
            // delegate detailed validation to the supplied IParamValidator
            var paramValidator = new TParamValidator();
            // you may want to implement the throw inside the Validator 
            // so additional detail can be added...
            if( !paramValidator.ValidateParameters( actionParameters ) )
                throw new ArgumentException("Valid parameters must be supplied", "actionParameters");
    
            this.Parameters = actionParameters;
        }
     }
    
    public class MyAction : BaseBusinessAction<MyActionParams,MyActionValidator>
    {
        // nested validator class
        private class MyActionValidator : IParamValidator<MyActionParams>
        {
            public MyActionValidator() {} // default constructor 
            // implement appropriate validation logic
            public bool ValidateParameters( MyActionParams params ) { return true; /*...*/ }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume that a user votes for some movies in a scale of 1
Let's assume that we are building a high traffic site that will be used
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I can call a method like this: core::get() . What is the
Let's say there is a graph and some set of functions like: create-node ::
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.