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 6642599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:57:00+00:00 2026-05-25T23:57:00+00:00

I have in my code the concept of command : public abstract class BaseCommand

  • 0

I have in my code the concept of command :

public abstract class BaseCommand
{
    public BaseCommand() { this.CommandId = Guid.NewGuid(); this.State = CommandState.Ready; }
    public Guid CommandId { get; private set; }
    public CommandState State {get; private set; }
    protected abstract void OnExecute();
    public void Execute() {
         OnExecute();
         State = CommandState.Executed;
    }
}

And some concrete implementation like this one :

public class DeleteItemCommand
{
    public int ItemId {get; set;}
    protected override void OnExecute()
    {
        var if = AnyKindOfFactory.GetItemRepository();
        if.DeleteItem(ItemId);
    }
}    

Now I want to add some validation. The first thing I can do is add a if/throw check:

public class DeleteItemCommand
{
    public int ItemId {get; set;}
    protected override void Execute()
    {
        if(ItemId == default(int)) throw new VeryBadThingHappendException("ItemId is not set, cannot delete the void");
        var if = AnyKindOfFactory.GetItemRepository();
        if.DeleteItem(ItemId);
    }
}

Now, I’m trying to use Code Contracts, because I’m quite convinced of its usefulness to reduce bug risk. If I rewrote the method like this :

public class DeleteItemCommand
{
    public int ItemId {get; set;}
    public void Execute()
    {
        Contract.Requires<VeryBadThingHappendException>(ItemId != default(int));

        var if = AnyKindOfFactory.GetItemRepository();
        if.DeleteItem(ItemId);
    }
}

The method compiles, the check is done at run-time. However, I got warning :

warning CC1032: CodeContracts: Method ‘MyProject.DeleteItemCommand.Execute’ overrides ‘MyProject.BaseCommand.Execute’, thus cannot add Requires.

I understand this warning is issued because I’m breaking the Liskov Principle.

However, in my case, conditions are different from one concrete class to another. My BaseCommand class is actually defining some common attributes like CommandIdentifier, state, and other ultimate features I removed here to keep the question simple.

While I understand the concepts of this principle, I don’t know what are the step I have to do to remove the warning properly (don’t tell me about the #pragma warning remove).

  1. Should I stop using code contracts in this case, where concrete implementations have specific requirements ?
  2. Should I rewrite my commanding mechanism to have, for example, separation between the Command “arguments” and the Command “execution” ? (having one CommandeExecutor<TCommand> per concrete class). This would result in a lot more classes in my project.
  3. Any other suggestion ?
  4. [Edit] As suggested by adrianm, convert properties to readonly, add constructor parameters to populate the properties and check properties in the contructor
  • 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-25T23:57:01+00:00Added an answer on May 25, 2026 at 11:57 pm

    I would use the constructor to set all the correct values, instead of public property setters.

    public class DeleteItemCommand
    {
        public DeleteItemCommand(int itemId)
        {
            Contract.Requires<VeryBadThingHappendException>(itemId!= default(int));
            ItemId = itemId;
        }
    
        public int ItemId {get; private set;}
        public void Execute()
        {   
            var if = AnyKindOfFactory.GetItemRepository();
            if.DeleteItem(ItemId);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code similar to this filtering entries in an Array of Objects: var
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code like this to move the player in my game left, right,
I tend to have a class to describe a general concept and subclasses to
The concept is this, You place JS Code for a video in a div,
let's suppose to have following code: <body style=overflow: hidden> <div class=wall> <button>New</button> <div class=prova>
I am C++ noob studying functors. I have this code as below(NB - This
I have some proof concept code for a HTTP module. The code checks to
I have code that references a web service, and I'd like the address of

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.