2014-08-28 15 views
5

Sto utilizzando il modello di richiesta/risposta su un livello di servizio. Ho, per esempio:Modello risposta/richiesta ... Come modellare i 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; } 
    } 
} 

Per gestire questa richiesta ho un gestore:

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 }; 

    } 

} 

Poi ho un dispatcher e lo uso come segue:

FindPostsByTypeRequest request = new FindPostsByTypeRequest { Type = type }; 

FindPostsByTypeResponse response = _dispatcher.Send<FindPostsByTypeResponse>(request); 

Il Problema Sto cercando di risolvere:

Quando cerco messaggi per tipo s a volte ho bisogno dei tag ... A volte no. Naturalmente potrei sempre ottenere i tag nei miei DTOs e usarlo o no ... Ma caricare qualcosa che non ho bisogno deve essere evitato ...

Quindi, in pratica ho bisogno di ottenere i messaggi di tipo e "dire" al gestore di quali dati ho bisogno.

La mia idea sarebbe qualcosa di simile:

_dispatcher.Send<FindPostsByTypeResponse<PostWithTagsModel>>(request); 

Dove PostWithTagsModel sarebbe il DTO avrei bisogno. Poi nella mia Handler avrei:

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 
    } 

} 

io non sono sicuro che questo è possibile, o anche il modo migliore per farlo ...

Fondamentalmente ho bisogno di "raccontare" il gestore in quale formato ho bisogno dei DTO inclusi nella risposta.

Come posso o dovrei farlo?

risposta

0

aggiornamento:

Ok, senza sapere esattamente quello che altre classi assomigli, ecco un esempio approssimativa di quello che penso avrebbe funzionato per voi con tutte le classi accessori compresi. L'ho messo insieme in Visual Studio e ho confermato che si compila. In questo approccio, le istanze della classe secondaria ResponseTypeEnum determinano il tipo di post da istanziare in base alla mappatura del tipo tramite il metodo ConstructItem sulla classe enum sottoclasse.

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

Non proprio. Ho bisogno di qualcosa di più generico e ho bisogno di passare alla richiesta che tipo voglio. Come useresti il ​​dispatchher sul tuo esempio? –

+7

nota a margine ... Non ho mai visto una formattazione così pazzesca – AlexFoxGill

+0

La formattazione è utile quando si osservano i metodi quando vengono compressi usando ctrl M-O. L'idea è di guardare un metodo alla volta invece che diversi contemporaneamente. Fondamentalmente il primo tab stop è la colonna del modificatore, il secondo è il tipo/la colonna del tipo di ritorno, il terzo è il nome della proprietà/campo/metodo e il quarto facoltativo sarebbe l'inizializzazione del campo o la proprietà {get; impostato;}. –