我需要理解这段代码,resoCoder 是在 DDD 播放列表上完成的。为什么他在冻结的内部实现 IEntity?
代码是:
@freezed
abstract class TodoItem with _$TodoItem implements IEntity {
const factory TodoItem({
@required UniqueId id,
@required TodoName name,
@required bool done,
}) = _TodoItem;
factory TodoItem.empty() => TodoItem(
id: UniqueId(),
name: TodoName(''),
done: false,
);
}
}
IEntity 代码是:
abstract class IEntity {
UniqueId get id;
}
UniqueId 代码为:
class UniqueId extends ValueObject<String> {
@override
final Either<ValueFailure<String>, String> value;
// We cannot let a simple String be passed in. This would allow for possible non-unique IDs.
factory UniqueId() {
return UniqueId._(
right(Uuid().v1()),
);
}
/// Used with strings we trust are unique, such as database IDs.
factory UniqueId.fromUniqueString(String uniqueIdStr) {
assert(uniqueIdStr != null);
return UniqueId._(
right(uniqueIdStr),
);
}
const UniqueId._(this.value);
}