getter
code:Getter.cs
// Bad Pattern
public class User {
public UserId Id { get; }
}
var circle = new Circle(user.Id);
// Better Pattern: Userのドメインモデルがどう定義されているかにもよる.
public class User {
private readonly UserId id;
public Circle CreateCircle() => new Circle(id);
}
code:.cs
interface IUserNotification {
void Id(User id);
void Name(UserName name);
}
class UserDataModelBuilder: IUserNotification {
private UserId id;
private UserName name;
public void Id(UserId id) => this.id = id;
public void Name(UserName name) => this.name = name;
public UserDataModel Build()
=> new UserDataModel{ Id = id, Name = name };
}
class User {
// インスタンス変数は非公開.
private readonly UserId id;
private UserName name;
public void Notify(IUserNotification note) {
note.Id(id);
note.Name(name);
}
}