Factory
Domain.Models.Users.UserとDomain.Models.Users.IUserFactoryのように,同じ名前空間にFactoryを置くべきである. Domain.Models.Users.Userだけ見てもFactoryの存在に気付きにくいため. code:UserApplicationService.cs
public class UserApplicationService {
private readonly IUserFactory userFactory;
public void Register(UserRegisterCommand command) {
var userName = new UserName(command.Name);
// 利用側
var user = userFactory.Create(userName);
...
}
}
// 実装例 IDの振り方を実装によって自由にできる.
class InMemoryUserFactory: IUsrFactory {
private int currentId;
public User Create(UserName name) {
currentId++;
return new User(
new UserId(currentId.ToString()), name
);
}
}