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
).
CommandeExecutor<TCommand>
per concrete class). This would result in a lot more classes in my project.I don't think you can use code contracts in this case. I think, you cannot add preconditions in overridden methods, only invariants and postconditions are possible there. Your best bet might be be to refactor from inheritance to composition:
ICommandExecutor
{
Execute(BaseCommand source);
}
public abstract class BaseCommand
{
public ICommandExecutor Executor { get; private set; }
public void Execute()
{
this.Executor.Execute(this);
State = CommandState.Executed;
}
}
public class DeleteCommandExecutor : ICommandExecutor
{
public void Execute(BaseCommand source)
{
Contract.Requires<VeryBadThingHappendException>(source.ItemId != default(int));
var if = AnyKindOfFactory.GetItemRepository();
if.DeleteItem(source.ItemId);
}
}
You could change the code to perform the execution in a different method:
public class DeleteItemCommand: BaseCommand
{
public int ItemId {get; set;}
public override void Execute()
{
PrivateExecute(ItemId);
}
private void PrivateExecute(int itemId)
{
Contract.Requires<VeryBadThingHappendException>(itemId != default(int));
var rep = AnyKindOfFactory.GetItemRepository();
rep.DeleteItem(itemId);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With