Conformity

DOGs offers three different options for defining a data model which can be used in different use cases and therefore possess different properties

Dataclass

The dataclass is the most advanced and capable conformity, offering equality and hashCode implementations as well as a generated builder and of course serialization. The properties of a dataclass are defined by adding them in the primary constructor or a secondary constructor named 'dog'. All fields used inside this constructor must be effectively final and the class must mixin the Dataclass<T> mixin with T referencing the class itself.

@serializable
class Person with Dataclass<Person> {

  final String name;
  final int age;
  final Set<String>? tags;
  
  Person(this.name, this.age, this.tags);
  
}

You can also utilize named parameters inside your constructor with a single restriction: Default values will be completely ignored by the serializer as all parameters are always specified. You can still use them to simply creating new objects with defaults.

@serializable
class Person with Dataclass<Person> {

  final String name;
  final int age;
  final Set<String>? tags;
  
  Person({
    required this.name,
    this.age = 18,
    this.tags
  });
  
}

Basic

The basic class is a light-version of the dataclass which does not generated equality and hashCode implementations and is allowed to be mutable. The properties definition of this conformity type is equivalent to the dataclass property definition.

@serializable
class Person {

  String name;
  int age;
  Set<String>? tags;
  
  Person(this.name, this.age, this.tags);
  
}

Use Case

Basic structures can be used when comparisons aren't required and/or fields are meant to be mutable.

Bean

The bean definition is the simplest but also unsafest conformity level offered by the dogs framework. Bean structures don't have a constructor and fully rely on property accessor methods. The generator will not generate any builders for this type of structure but will create static factory for the class (ClassName + Factory). All fully accessible fields are treated as structure property but can ignored using the beanIgnore annotation.

@serializable
class Person {

  late String name;
  int? age;
  Set<String>? tags;

  @beanIgnore
  late String ignored;

}

Defining code inside the constructor method should be avoided but isn't a problem as long as it's a pure operation.

Use Case

Bean-type structures can be used for persisting ioc-like services.

Last updated