我有一个Employee
实体,方法Authenticate(SecureString password)
在此方法中,LastAuthenticatedDate
私有成员被更改,期望它将被保留。
虽然这使得"呼叫层"非常尴尬。因为他们现在需要做以下事情(没有失败,否则数据将不正确):
if (myEmployee.Authenticate(password))
{
// do stuff
employeeRepository.Update(myEmployee); // so that the last authentication date is persisted.
}
这是非常尴尬的,我可以看到这个模式在我的域模型中随处可见,我的期望是它应该自动保存到持久性存储中,但是没有以某种方式注入存储库(或服务?)我不会&#39 ;看看这是怎么可能的。
域模型是持久性无知的,但可以注入IAuthenticationService
,并且通过双重调度可以为我更新存储库。这是DDD方面的正确方法吗?
更新 以下代码段可能有助于更好地了解我正在做的事情。
public bool Authenticate(SecureString password)
{
bool isSuccess = false;
var hash = new HashedPassword(password, Password.Salt);
if (Password.Validate(hash))
{
_lastLogin = DateTimeOffset.Now;
isSuccess = true;
}
DomainEvents.RaiseEvent(new EmployeeAuthenticationEvent(this, isSuccess));
return isSuccess;
}
答案 0 :(得分:1)
我不会将存储库或服务层注入您的域模型,如果这是您要求的。
我会在域服务层中放置您正在讨论的代码段。
class EmployeeAuthenticationService implements AuthenticationService {
public void authenticate(Employee employee, String password) {
if (employee.Authenticate(password)) {
// do stuff
employeeRepository.Update(myEmployee);
}
}
}