2016-11-14 45 views
1

我正在做關於如何實現特定資源超媒體的一些研究,但無法找到一個真正的實現例子,只是抽象...正確的方法來創建在C#中的WebAPI超媒體

要知道,在各種物品,這傢伙創建等的方法:

public List<Link> CreateLinks(int id) 
{ 
    ...//Here the guy put these three dots, whyyyyyyyyyy? 
} 

我有什麼至今:

public Appointment Post(Appointment appointment) 
    { 
     //for sake of simplicity, just returning same appointment 
     appointment = new Appointment{ 
      Date = DateTime.Now, 
      Doctor = "Dr. Who", 
      Slot = 1234, 
      HyperMedia = new List<HyperMedia> 
      { 
       new HyperMedia{ Href = "/slot/1234", Rel = "delete" }, 
       new HyperMedia{ Href = "/slot/1234", Rel = "put" }, 
      } 
     }; 

     return appointment; 
    } 

和約會類:

public class Appointment 
{ 
    [JsonProperty("doctor")] 
    public string Doctor { get; set; } 

    [JsonProperty("slot")] 
    public int Slot { get; set; } 
    [JsonProperty("date")] 
    public DateTime Date { get; set; } 

    [JsonProperty("links")] 
    public List<HyperMedia> HyperMedia { get; set; } 
} 

public class HyperMedia 
{ 
    [JsonProperty("rel")] 
    public string Rel { get; set; } 

    [JsonProperty("href")] 
    public string Href { get; set; } 
} 

有沒有適當的方法呢?我的意思是,沒有硬編碼鏈接?如何爲給定類型動態創建它們,例如Appointment類?我正在使用c#Webapi,而不是c#MVC。

回答

2
  1. 對於動態添加路由到超媒體集合,你可以利用路線命名的:

    • 與特定的名稱來定義你的路線(例如用於刪除):

      [Route("{id:int}", Name = "AppointmentDeletion")] 
      public IHttpActionResult Delete(int slot) 
      { 
          //your code 
      } 
      
    • 使用方法:UrlHelper.Link方法:

      public Appointment Post(Appointment appointment) 
      { 
          appointment = new Appointment 
          { 
           HyperMedia = new List<HyperMedia> 
           { 
            new HyperMedia 
            { 
             Href = Url.Link("AppointmentDeletion", new { slot = 1234 }), 
             Rel = "delete" 
            } 
           } 
      
          return appointment; 
      }; 
      
  2. 也可以動態地添加鏈接到一個結果對象不宣超媒體屬性,每類:

    • 定義一個類無鏈接:

      public class Appointment 
      { 
          [JsonProperty("doctor")] 
          public string Doctor { get; set; } 
      
          [JsonProperty("slot")] 
          public int Slot { get; set; } 
      
          [JsonProperty("date")] 
          public DateTime Date { get; set; } 
      } 
      
    • 定義擴展方法:

      public static class LinkExtensions 
      { 
          public static dynamic AddLinks<T>(this T content, params object[] links) 
          { 
           IDictionary<string, object> result = new ExpandoObject(); 
      
           typeof (T) 
            .GetProperties(BindingFlags.Public | BindingFlags.Instance) 
            .ToList() 
            .ForEach(_ => result[_.Name.ToLower()] = _.GetValue(content)); 
      
           result["links"] = links; 
      
           return result; 
          } 
      } 
      
    • 使用它:

      public IHttpActionResult Post(Appointment appointment) 
      { 
          return Ok(appointment.AddLinks(new HyperMedia 
          { 
           Href = Url.Link("AppointmentDeletion", new { slot = 1234 }), 
           Rel = "delete" 
          })); 
      } 
      
+0

謝謝。所以我的模型並非如此...接受答案。 –

1

你絕對可以提取RelEnum(其實2,一個標準 - DeletePut等 - 和一個自定義 - 後者可能含有像customer-by-id自定義關係)。

您也可以動態構建Href params(從對象的屬性中拉取參數),但對於資源本身...您可能堅持使用硬編碼(您也可以查看反射)。

+0

謝謝,加1的幫助。我會記住這一點。 –

相關問題