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

  • Home
  • SEARCH
  • 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 6115297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:04:29+00:00 2026-05-23T15:04:29+00:00

I use parameter objects to encapsulate the parameters that are passed to my business

  • 0

I use parameter objects to encapsulate the parameters that are passed to my business rules. The rule is created using a context parameter, which may then be modified, then at a later time the rule is executed. Some of the properties of this object are required, otherwise the method would throw a NullReferenceException. However, if I throw an ArgumentNullException, I get a warning saying that the parameter name does not match one of my parameters. What would be the appropriate exception for this situation?

public class GetAttributes : BusinessRuleBase 
{
    private readonly IGetAttributesContext _context;


    public GetAttributes(IGetAttributesContext context)
    {
        _context = context;
    }

    public override void Execute()
    {
        if (_context.AttributeModel == null)
        {
            //Exception would be thrown here
        }
        _context.Attributes = _context.AttributeModel
                                      .DoSomething(_context.EntityType);
    }
}
  • 1 1 Answer
  • 0 Views
  • 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-23T15:04:30+00:00Added an answer on May 23, 2026 at 3:04 pm

    If you are using Code Contracts and you are willing to publicly expose a HasAttributeModel property, I recommend

    Contract.Requires(this.HasAttributeModel);
    

    Otherwise, you should throw a custom exception that derives from InvalidOperationException here. The method that you are trying to execute is not valid given the object’s current state. From the documentation:

    The exception that is thrown when a method call is invalid for the object’s current state.

    Your custom message should say that the instance’s AttributeModel is null.

    The larger question is, why are you allowing your instances to be in a state where a method could be invoked on them while the object is in an invalid state? You should avoid this, if you can. For example, why isn’t GetAttributes checking that context.AttributeModel is not null? You could have

    Contract.Requires(context.AttributeModel != null);
    

    as a precondition for this method. However, there is some danger here in that if someone retains a live reference to the referrent of the parameter context, they could destroy the state since you maintain a reference via the private variable _context. Be aware of this, and avoid it if you can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The init() is able to use the parameter passed to it in this example:
I know that you can use a dummy int parameter on operator++ and operator--
I have a list of objects, which has a method that has a couple
I use several NSComparators in my iOS app which compare objects by NSString or
Is it possible to use the __unused attribute macro on Objective-C object method parameters?
I am trying to use parameter substitution with SQLite within Python for an IN
How use output parameter from procedure?
How would I use the apc.filters parameter in APC opcode caching to not cache
I have to use an other application (console) to pass some parameter to this
When I use file_get_contents and pass it as a parameter to another function, without

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.