leancodepl / contractsgenerator-dart

Dart contracts client generator for a CQRS API
3 stars 1 forks source link

Make all fields required for Commands, Queries, and Operations #71

Closed shilangyu closed 2 years ago

shilangyu commented 2 years ago

Currently, if a field is nullable, it is not required in the constructor of that class. I think while this is useful for DTOs, it can be a source of bugs for CQRS types. I think I'd rather pass an explicit null.

Nullability ≠ Optionality

Why?

When a field is nullable (lets say T?), it does not mean it only accepts a non-nullable T and a constant null. Instead, it means you can pass a variable that is possibly null. This is an important distinction, it means you always should pass a variable, but this variable just might happen to be null. In a customer project this has already been a problem, where a simple mistake of forgetting to pass a value to a nullable CQRS method field caused problems in the whole system afterwards.

Example of the change proposed


class Something implements Query {
  Something({
-    this.field1,
+    required this.field1,
     required this.field2,
  });

  final String? field1;
  final int field2;
}
bartekpacia commented 2 years ago

I think it's a good idea.