2014-08-28 4 views
5

मैं सेवा परत पर अनुरोध/प्रतिक्रिया पैटर्न का उपयोग कर रहा हूं। मैं उदाहरण के लिए,:प्रतिक्रिया/अनुरोध पैटर्न ... डीटीओ कैसे आकार दें?

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 
    } 

} 

मुझे यकीन है कि यह संभव या यहां तक ​​कि सबसे अच्छा तरीका यह करने के लिए है नहीं कर रहा हूँ ...

मूल रूप से मैं हैंडलर "बता" करने की जरूरत है जो प्रारूप में मैं प्रतिक्रिया में शामिल डीटीओ की आवश्यकता है।

मैं यह कैसे कर सकता हूं या मुझे यह करना चाहिए?

उत्तर

0

UPDATED:

ठीक है, ठीक जानते हुए भी कि तुम क्या अन्य वर्गों की तरह लग रही बिना, यहाँ मैं क्या सोचता सभी सहायक वर्गों के साथ आप के लिए काम करेगा शामिल की एक अनुमानित उदाहरण है। मैंने इसे विजुअल स्टूडियो में एक साथ रखा और पुष्टि की कि यह संकलित है। इस दृष्टिकोण में, ResponseTypeEnum उप-वर्ग के उदाहरण निर्धारित करते हैं कि subclassable enum वर्ग पर ConstructItem विधि के माध्यम से टाइप मैपिंग के आधार पर तत्काल पोस्ट किस प्रकार की पोस्ट है।

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace TestRequestResponse 
{ 

    #region Generic portions 

    /// <summary> 
    /// Base request class 
    /// </summary> 
    public class  Request {} 

    /// <summary> 
    /// Base response class 
    /// </summary> 
    public class  Response {} 

    /// <summary> 
    /// Generic typed namespace for Response/Request support 
    /// </summary> 
    /// <typeparam name="tRequestResponseSet">Request/Response set type parameter (for namespace constraining)</typeparam> 
    /// <typeparam name="tItem">Item type parameter</typeparam> 
    /// <typeparam name="tSourceItem">Source item type parameter</typeparam> 
    public class  RequestResponseSet<tRequestResponseSet, tItem, tSourceItem> 
      where  tRequestResponseSet : RequestResponseSet<tRequestResponseSet, tItem, tSourceItem> 
      where  tItem    : RequestResponseSet<tRequestResponseSet, tItem, tSourceItem>.Item 
    { 

     /// <summary> 
     /// Base item class 
     /// </summary> 
     public 
     abstract class Item 
     { 
      public 
      abstract void Initialize(tSourceItem sourceItem); 
     } 

     /// <summary> 
     /// Base type specific subclassable enum class (see https://github.com/TyreeJackson/atomic/blob/master/Atomic.Net/DataTypes/SubclassableEnum.cs for a more generic version) 
     /// </summary> 
     public 
     abstract class ResponseTypeEnum 
     { 
      private 
      static  Dictionary 
         < 
          string, 
          ResponseTypeEnum 
         >      allValues      = new Dictionary<string,ResponseTypeEnum>(); 
      private  string     type; 
      private  Func<tItem>    constructItem; 
      protected       ResponseTypeEnum 
               (
                string  type, 
                Func<tItem> constructItem 
               ) 
      { 
       this.type   = type; 
       this.constructItem = constructItem; 
      } 

      public  tItem     ConstructItem(tSourceItem sourceItem) 
      { 
       var returnItem = this.constructItem(); 
       returnItem.Initialize(sourceItem); 
       return returnItem; 
      } 

      public 
      static  ResponseTypeEnum  Select(string type) 
      { 
       ResponseTypeEnum returnTypeEnum = null; 
       return type == null || !ResponseTypeEnum.allValues.TryGetValue(type, out returnTypeEnum) 
         ? null 
         : returnTypeEnum; 
      } 

      public 
      static 
      implicit 
      operator       ResponseTypeEnum(string type) 
      { 
       return ResponseTypeEnum.Select(type); 
      } 

      public 
      static 
      implicit 
      operator       string(ResponseTypeEnum typeEnum) 
      { 
       return typeEnum == null ? null : typeEnum.type; 
      } 

     } 

    } 

    #endregion Generic portions 

    #region Post specific portions 

    /// <summary> 
    /// Stored post /entity 
    /// </summary> 
    public class  StoredPost 
    { 
     public String   Type { get; set; } 
     public Int32   Id  { get; set; } 
     public String   Title { get; set; } 
     public String   Text { get; set; } 
     public List<String> Tags { get; set; } 
    } 

    /// <summary> 
    /// Post specific typed namespace 
    /// </summary> 
    public class  PostsSet : RequestResponseSet<PostsSet, PostsSet.Post, StoredPost> 
    { 

     public class Types : ResponseTypeEnum 
     { 

      public static  Types Basic  = new Types("basic", ()=>new Post()); 
      public static  Types WithTags = new Types("withTags",()=>new PostWithTags()); 

      protected     Types(string type, Func<Post> createPost) : base(type, createPost) {} 
     } 

     public class Post : Item 
     { 
      public  Int32 Id  { get; set; } 
      public  String Title { get; set; } 
      public  String Text { get; set; } 

      public 
      override void Initialize(StoredPost sourceItem) 
      { 
       this.Id  = sourceItem.Id; 
       this.Title = sourceItem.Title; 
       this.Text = sourceItem.Text; 
      } 

     } 

     public class PostWithTags : Post 
     { 
      public  List<String> Tags { get; set; } 

      public 
      override void   Initialize(StoredPost sourceItem) 
      { 
       base.Initialize(sourceItem); 
       this.Tags = sourceItem.Tags; 
      } 
     } 

    } 

    /// <summary> 
    /// Post specific response class 
    /// </summary> 
    public class  FindPostsResponse : Response 
    { 
     public IList<PostsSet.Post> Posts { get; set; } 
    } 

    /// <summary> 
    /// Post specific request class 
    /// </summary> 
    public class  FindPostsRequest : Request 
    { 
     public string Type { get; set; } 
    } 

    /// <summary> 
    /// Post specific context 
    /// </summary> 
    public interface IContext 
    { 
     IEnumerable<StoredPost> Posts { get; set; } 
    } 

    /// <summary> 
    /// Post specific handler 
    /// </summary> 
    public class  FindPostHandler 
    { 

     private IContext   context; 

     public      FindPostHandler(IContext context) 
     { 
      this.context = context; 
     } 

     public FindPostsResponse Handle(FindPostsRequest request) 
     { 
      var type = PostsSet.Types.Select(request.Type); 

      return 
      type == null 
      ? null 
      : new FindPostsResponse() 
       { 
        Posts = 
        this.context.Posts 
        .Where(x => x.Type == request.Type) 
        .Select(x => type.ConstructItem(x)) 
        .ToList() 
       }; 
     } 

    } 

    #endregion Post specific portions 

} 
+0

वास्तव में नहीं। मुझे कुछ और सामान्य की ज़रूरत है और मुझे अनुरोध में प्रवेश करने की ज़रूरत है कि मैं किस प्रकार चाहता हूं। आप अपने उदाहरण पर प्रेषक का उपयोग कैसे करेंगे? –

+7

साइड नोट ... मैंने कभी ऐसा पागल स्वरूपण नहीं देखा है – AlexFoxGill

+0

स्वरूपण उपयोगी है जब वे ctrl m-O का उपयोग करके ध्वस्त हो जाते हैं। विचार एक बार में कई बार एक विधि को देखने के लिए है। मूल रूप से पहला टैब स्टॉप संशोधक कॉलम होता है, दूसरा प्रकार/रिटर्न प्रकार कॉलम होता है, तीसरा संपत्ति/फ़ील्ड/विधि का नाम होता है और चौथा वैकल्पिक फील्ड प्रारंभिकरण या संपत्ति {get; सेट;}। –

संबंधित मुद्दे