Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Last active February 20, 2019 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkhorikov/521abe04e4c1270824edc31e4767cc7b to your computer and use it in GitHub Desktop.
Save vkhorikov/521abe04e4c1270824edc31e4767cc7b to your computer and use it in GitHub Desktop.
When to validate commands in CQRS
public sealed class EnrollCommand : ICommand
{
public long StudentId { get; }
public string Course { get; }
public string Grade { get; }
public EnrollCommand(long studentId, string course, string grade)
{
if (course == null || grade == null) // Precondition checks
throw ArgumentException();
Id = id;
Course = course;
Grade = grade;
}
}
public sealed class EnrollCommand : ICommand
{
public long StudentId { get; }
public string Course { get; }
public string Grade { get; }
/* ... */
public static Result<EnrollCommand> Create(long studentId, string course, string grade)
{
/* Validate, return either success or failure */
}
}
public sealed class EnrollCommand : ICommand
{
public long StudentId { get; }
public string Course { get; }
public string Grade { get; }
public EnrollCommand(long studentId, string course, string grade)
{
// No invariant checks
Id = id;
Course = course;
Grade = grade;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment