2014-08-28 38 views
5

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?

Trả lời

0

CẬP NHẬT:

Ok, mà không biết chính xác những gì bạn lớp khác như thế nào, đây là một ví dụ gần đúng về những gì tôi nghĩ rằng sẽ làm việc cho bạn với tất cả các lớp phụ trợ bao gồm. Tôi đặt điều này lại với nhau trong Visual Studio và xác nhận rằng nó biên dịch. Trong phương pháp này, các cá thể lớp con ResponseTypeEnum xác định loại bài viết nào để khởi tạo dựa trên ánh xạ kiểu thông qua phương thức ConstructItem trên lớp enum lớp con.

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

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? –

+7

lưu ý phụ ... Tôi chưa bao giờ thấy định dạng điên như vậy – AlexFoxGill

+0

Đị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ộ;}. –

Các vấn đề liên quan