CQRS 원칙의 명령 측면에서 코드 중복에 대한 질문이 있습니다.명령에서 CQRS 코드 중복
https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91 https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=92
그것의 자신의 클래스에 각 명령을 분리하는이 방법은 데이터에서 개체를 검색 할 때 몇 가지 코드 중복을 줄려고하고 있다는 것을 나에게 보인다
는에서 기사를 따르고 저장.약간의 문제가있을 수 있지만 예를 들어 전자 메일 주소와 사용자의 마지막 로그인 날짜를 업데이트하려는 명령에 대해 사용자 암호를 재설정하려는 명령이 있다고 가정 해 봅시다.
public class ResetPasswordCommandHandler : CommandHandler<ResetPasswordCommand>
{
public override void Execute(ResetPasswordCommand command)
{
**// duplication here**
var user = from c in db.Users
where c.EmailAddress = command.EmailAddress
select c;
user.EmailAddress = command.EmailAddress;
...
db.Save();
}
}
public class UpdateLastLoginCommandHandler : CommandHandler<UpdateLastLoginCommand>
{
public override void Execute(UpdateLastLoginCommand command)
{
**// duplication here**
var user = from c in db.Users
where c.EmailAddress = command.EmailAddress
select c;
user.LastLogin = DateTime.Now;
...
db.Save();
}
}
두 명령 모두 사용자의 전자 메일 주소를 기반으로 사용자를 검색합니다. 이제 데이터베이스를 쿼리하기 전에 UI 입력을 트리밍하려면 두 곳에서이를 변경해야합니다.
I 예를 들어 GetUserByEmailAddress 메소드가 있고 해당 IUserRepository를 CommandHandlers의 생성자에 삽입 할 수있는 UserRepository를 작성할 수 있습니다. 그러나 결국 Save, GetById, GetByUsername 등으로 "god 저장소"를 만들지 않을까요?
그리고 리포지토리를 만드는 경우 왜 별도의 쿼리 개체를 만드나요?
이 코드를 어떻게하면 건조하게 만들 수 있습니까?
여러 인터페이스를 구현하기 위해 명령 처리기를 리팩토링하지 않더라도 비 대한 저장소 대신 "god"명령 처리기 만 만들겠습니까? – user4419743
나는 두 번째 부분을 좋아한다. 확장 메서드를 만들 수 있습니다. 같은 것 : 'public static IQueryable WithEmailAddress (이 IQueryable 소스, 문자열 emailaddress)' –
user4419743