मैं सेवा परत पर अनुरोध/प्रतिक्रिया पैटर्न का उपयोग कर रहा हूं। मैं उदाहरण के लिए,:प्रतिक्रिया/अनुरोध पैटर्न ... डीटीओ कैसे आकार दें?
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; }
}
}
इस अनुरोध को पूरा करने के लिए मैं एक हैंडलर है:
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 };
}
}
तो मैं एक डिस्पैचर है और इसका इस्तेमाल करते हैं इस प्रकार है:
FindPostsByTypeRequest request = new FindPostsByTypeRequest { Type = type };
FindPostsByTypeResponse response = _dispatcher.Send<FindPostsByTypeResponse>(request);
समस्या मैं को हल करने की कोशिश कर रहा हूं:
जब मैं प्रकार के द्वारा पोस्ट की तलाश करता हूं ometimes मुझे टैग की जरूरत है ... कभी-कभी मैं नहीं करता। बेशक मैं हमेशा अपने DTOs में टैग हो जाते हैं और इसका इस्तेमाल करते हैं या नहीं ... लेकिन कुछ है जो मैं बचा जाना चाहिए की जरूरत नहीं है लोड हो रहा है ...
तो बुनियादी तौर पर मैं प्रकार के पोस्ट प्राप्त करने की आवश्यकता सकता है और हैंडलर को "बताएं" मुझे किस डेटा की आवश्यकता है।
मेरे विचार होगा कुछ की तरह:
_dispatcher.Send<FindPostsByTypeResponse<PostWithTagsModel>>(request);
कहाँ PostWithTagsModel डीटीओ मैं की आवश्यकता होगी होगा। तब मेरे हैंडलर में मैं होगा:
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
}
}
मुझे यकीन है कि यह संभव या यहां तक कि सबसे अच्छा तरीका यह करने के लिए है नहीं कर रहा हूँ ...
मूल रूप से मैं हैंडलर "बता" करने की जरूरत है जो प्रारूप में मैं प्रतिक्रिया में शामिल डीटीओ की आवश्यकता है।
मैं यह कैसे कर सकता हूं या मुझे यह करना चाहिए?
वास्तव में नहीं। मुझे कुछ और सामान्य की ज़रूरत है और मुझे अनुरोध में प्रवेश करने की ज़रूरत है कि मैं किस प्रकार चाहता हूं। आप अपने उदाहरण पर प्रेषक का उपयोग कैसे करेंगे? –
साइड नोट ... मैंने कभी ऐसा पागल स्वरूपण नहीं देखा है – AlexFoxGill
स्वरूपण उपयोगी है जब वे ctrl m-O का उपयोग करके ध्वस्त हो जाते हैं। विचार एक बार में कई बार एक विधि को देखने के लिए है। मूल रूप से पहला टैब स्टॉप संशोधक कॉलम होता है, दूसरा प्रकार/रिटर्न प्रकार कॉलम होता है, तीसरा संपत्ति/फ़ील्ड/विधि का नाम होता है और चौथा वैकल्पिक फील्ड प्रारंभिकरण या संपत्ति {get; सेट;}। –