Tôi đang sử dụng mẫu Yêu cầu/Phản hồi trên một lớp dịch vụ. Tôi có, ví dụ:Mẫu trả lời/Yêu cầu ... Cách định dạng DTO?
public class FindPostsByTypeRequest : Request {
public PostType Type { get; set; }
}
public class FindPostsByTypeResponse : Response {
public IList<PostDto> Posts { get; set; }
public class PostDto {
public Int32 Id { get; set; }
public String Title { get; set; }
public String Text { get; set; }
}
}
Để xử lý yêu cầu này, tôi có một handler:
public class FindPostsByTypeHandler : Handler<FindPostsByTypeRequest, FindPostsByTypeResponse> {
private IContext _context;
public FindPostsByTypeHandler(IContext context) {
_context = context;
}
public FindPostsByTypeResponse Handle(FindPostsByTypeRequest request) {
IList<FindPostsByTypeRequest.PostDto> posts = _context.Posts
.Where(x => x.Type == request.Type)
.Select(x => new FindPostsByTypeRequest.PostDto {
Id = x.Id,
Title = x.Title,
Text = x.Text
}).ToList();
return new FindPostsByTypeResponse { Posts = posts };
}
}
Sau đó, tôi có một điều phối và sử dụng nó như sau:
FindPostsByTypeRequest request = new FindPostsByTypeRequest { Type = type };
FindPostsByTypeResponse response = _dispatcher.Send<FindPostsByTypeResponse>(request);
Vấn đề Tôi đang cố giải quyết:
Khi tôi tìm bài đăng theo loại s đôi khi tôi cần các thẻ ... Đôi khi tôi không. Tất nhiên tôi luôn luôn có thể nhận được các thẻ vào DTOs của tôi và sử dụng nó hay không ... Nhưng tải một cái gì đó mà tôi không cần nên tránh ...
Vì vậy, về cơ bản tôi cần để có được những bài viết theo loại và "cho biết" người xử lý dữ liệu nào tôi cần.
Ý tưởng của tôi sẽ là một cái gì đó như:
_dispatcher.Send<FindPostsByTypeResponse<PostWithTagsModel>>(request);
đâu PostWithTagsModel sẽ là DTO tôi sẽ cần. Sau đó, trong Handler của tôi, tôi sẽ có:
public class FindPostsByTypeHandler : Handler<FindPostsByTypeRequest, FindPostsByTypeResponse> {
private IContext _context;
public FindPostsByTypeHandler(IContext context) {
_context = context;
}
public FindPostsByTypeResponse<PostsByType> Handle(FindPostsByTypeRequest request) {
IList<FindPostsByTypeResponse.PostDto> posts = _context.Posts
.Where(x => x.Type == request.Type)
.Select(x => new FindPostsByTypeResponse.PostDto {
Id = x.Id,
Title = x.Title,
Text = x.Text
}).ToList();
return new FindPostsByTypeResponse { Posts = posts };
}
public FindPostsByTypeResponse<PostsWithoutTagsDto> Handle(FindPostsByTypeRequest request) {
IList<FindPostsByTypeResponse.PostsWithoutTagsDto> posts = _context.Posts
.Where(x => x.Type == request.Type)
.Select(x => new FindPostsByTypeResponse.PostsWithoutTagsDto {
Id = x.Id,
Title = x.Title,
Text = x.Text
}).ToList();
return new FindPostsByTypeResponse { Posts = posts };
}
public FindPostsByTypeResponse<PostsWithTagsDto> Handle(FindPostsByTypeRequest request) {
// Remaining code
}
}
Tôi không chắc chắn này có thể hoặc thậm chí là cách tốt nhất để làm điều này ...
Về cơ bản tôi cần phải "nói" xử lý, trong đó định dạng tôi cần các DTO được bao gồm trong Phản hồi.
Làm cách nào để tôi có thể thực hiện việc này?
Không thực sự. Tôi cần một cái gì đó chung chung hơn và tôi cần phải đi vào yêu cầu loại nào tôi muốn. Làm thế nào bạn sẽ sử dụng dispacther trên ví dụ của bạn? –
lưu ý phụ ... Tôi chưa bao giờ thấy định dạng điên như vậy – AlexFoxGill
Định dạng này hữu ích khi xem các phương thức khi chúng được thu gọn bằng cách sử dụng ctrl M-O. Ý tưởng là xem xét một phương pháp tại một thời điểm thay vì một phương pháp cùng một lúc. Về cơ bản, tab dừng đầu tiên là cột sửa đổi, thứ hai là cột kiểu/trả về, thứ ba là tên thuộc tính/trường/phương thức và tùy chọn thứ tư sẽ là khởi tạo trường hoặc thuộc tính {get; bộ;}. –