How do I place a precondition on the Invoke
method in the following interface stating that the object denoted by ObjectId
must exist?:
interface IDeleteObjectCommand {
Guid ObjectId { get; }
void Invoke();
}
Attempt #1
I already have a command called IObjectExistsCommand
which can be used to determine if objects exist. These commands can be instantiated via an IObjectExistsCommandFactory
. I have thought about doing the following, but this adds undesirable noise to the command's interface (IMO):
interface IDeleteObjectCommand {
IObjectExistsCommandFactory ObjectExistsCommandFactory { get; }
Guid ObjectId { get; }
// Contract.Requires(ObjectExistsCommandFactory.Create(ObjectId).Invoke());
void Invoke();
}
Attempt #2
Similar to above, except use ServiceLocator
. Undesirable for obvious reasons, but is cleaner:
interface IDeleteObjectCommand {
Guid ObjectId { get; }
// Contract.Requires(ServiceLocator.Get<ObjectExistsCommandFactory>().Create(ObjectId).Invoke());
void Invoke();
}
EDIT: Similarly, how would you define post-conditions on extrinsic state? I.e. saying that this method results in the existence of a new file.