æçš¿è ïŒVyacheslav Mikhailovããœãªã¥ãŒã·ã§ã³ã¢ãŒããã¯ãã
ãã®èšäºã§ã¯ãRESTful APIã®èšèšã«ãããç§ã®çµéšãå ±æããŸã-å ·äœçãªäŸã䜿çšããŠãå°ãªããšãåçŽãªãµãŒãã¹ãçŸããããæ¹æ³ã瀺ããŸãã ãŸããAPIãšã¯äœãããªãå¿ èŠãªã®ããRESTã®åºæ¬ã«ã€ããŠã説æããŸããRESTã®å®è£ 察象ã«ã€ããŠè©±ãåããŸãã ãã®ãã¯ãããžãŒã«äŸåããŠãããäŸåããŠããªãäž»èŠãªWebãã©ã¯ãã£ã¹ã«è§ŠããŠã¿ãŸãããã ãŸããæå°éã®åŽåã§åªããããã¥ã¡ã³ããã³ã³ãã€ã«ããæ¹æ³ãåŠç¿ããRESTful APIã«ååšããããŒãžã§ã³ç®¡çæ¹æ³ã確èªããŸãã
ããŒã1.çè«
ãããã£ãŠããã¹ãŠã®äººãç¥ã£ãŠããããã«ãAPIã¯ã¢ããªã±ãŒã·ã§ã³ããã°ã©ãã³ã°ã€ã³ã¿ãŒãã§ã€ã¹ã§ããã1ã€ã®ã¢ããªã±ãŒã·ã§ã³ãŸãã¯ã³ã³ããŒãã³ããä»ã®ã¢ããªã±ãŒã·ã§ã³ãŸãã¯ã³ã³ããŒãã³ããšå¯Ÿè©±ããããã®äžé£ã®ã«ãŒã«ãšã¡ã«ããºã ã§ã
è¯ãAPIãéèŠãªã®ã¯ãªãã§ããïŒ
- 䜿ãããããšãµããŒã ã åªããAPIã¯äœ¿ãããããä¿å®ãç°¡åã§ãã
- éçºè éã®è¯å¥œãªå€æ ã 誰ããããªãã®APIãæ°ã«å ¥ã£ãããæ°ããã¯ã©ã€ã¢ã³ããšãŠãŒã¶ãŒãããªãã®ãšããã«æ¥ãŸãã
- ããªãã®ãµãŒãã¹ã®ããé«ãäººæ° ã APIãŠãŒã¶ãŒãå€ãã»ã©ããµãŒãã¹ã®äººæ°ãé«ãŸããŸãã
- ããè¯ãæç±æ ã APIã®æ§é ãåªããŠããã»ã©ãã³ã³ããŒãã³ãã®åé¢ãåäžããŸãã
- 補åã®è¯ãå°è±¡ ã APIã¯éçºè åãã®UIã®ãããªãã®ã§ãã ããã¯ãéçºè ãæåã«è£œåã«ééãããšãã«æ³šæãæããã®ã§ãã APIãæªãã§ããå Žåãããªãã¯ãæè¡å°é家ãšããŠããã®ãããªè£œåã䜿çšããããšãäŒæ¥ã«æšå¥šããããµãŒãããŒãã£ã®ãã®ãå ¥æããŸãã
ããã§ã¯ãAPIã®çš®é¡ãèŠãŠã¿ãŸãããã
å®è£ æ¹æ³å¥ã®APIã®çš®é¡ïŒ
- WebãµãŒãã¹API
- XML-RPCããã³JSON-RPC
- SOAP
- REST
- WebSockets API
- ã©ã€ãã©ãªããŒã¹ã®API
- Javaã¹ã¯ãªãã
- ã¯ã©ã¹ããŒã¹ã®API
- CïŒAPI
- Java
ã¢ããªã±ãŒã·ã§ã³ã«ããŽãªå¥ã®APIã¿ã€ãïŒ
- OSé¢æ°ãšã«ãŒãã³
- ãã¡ã€ã«ã·ã¹ãã ãžã®ã¢ã¯ã»ã¹
- ãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ãžã®ã¢ã¯ã»ã¹
- ãªããžã§ã¯ããªã¢ãŒãã£ã³ã°API
- CORBA
- .Netãªã¢ãŒãåŠç
- ããŒããŠã§ã¢API
- ãããªã¢ã¯ã»ã©ã¬ãŒã·ã§ã³ïŒOpenCL ...ïŒ
- ããŒããã£ã¹ã¯ãã©ã€ã
- PCIãã¹
- ...
ã芧ã®ãšãããWeb APIã«ã¯XML-RPCãšJSON-RPCãSOAPãRESTãå«ãŸããŠããŸãã
RPCïŒãªã¢ãŒãããã·ãŒãžã£ã³ãŒã«ïŒã¯éåžžã«å€ãæŠå¿µã§ãããå€ä»£ãäžãçŸä»£ã®ãããã³ã«ãçµã¿åãããããšã§ãå¥ã®ã¢ããªã±ãŒã·ã§ã³ã§ã¡ãœãããåŒã³åºãããšãã§ããŸãã XML-RPCã¯ãXMLã®ç»å ŽçŽåŸã«1998幎ã«ç»å Žãããããã³ã«ã§ãã æåã¯Microsoftã«ãã£ãŠãµããŒããããŠããŸããããããã«Microsoftã¯å®å šã«SOAPã«åãæ¿ãã£ãããã.Net Frameworkã§ã¯ããã®ãããã³ã«ããµããŒãããã¯ã©ã¹ã¯èŠã€ãããŸããã ããã«ãããããããXML-RPCã¯ããŸã ã«ããŸããŸãªèšèªïŒç¹ã«PHPïŒã«äœãã§ããŸã-ã©ããããåçŽãã®ããã«éçºè ã®æã«å€ããããã§ãã
SOAPã¯ãMicrosoftã®åªåã«ãã1998幎ã«ãç»å ŽããŸããã ããã¯ããœãããŠã§ã¢ã®äžçã«ãããé©åœãšããŠçºè¡šãããŸããã ããã¯ããã¹ãŠããã€ã¯ããœããã®èšç»éãã«é²ãã ãšèšã£ãŠããããã§ã¯ãããŸããããããã³ã«ã®è€éããšéãã®ããã«ãéåžžã«å€ãã®æ¹å€ããããŸããã åæã«ãSOAPãçã®çªç Žå£ãšèŠãªã人ãããŸããã 2003幎ã«W3Cãå§åãšããŠSOAP 1.2ãæ¿èªãããŸã§ããããã³ã«ã¯é²åãç¶ããå€æ°ã®æ°ããä»æ§ãšæ°ããä»æ§ãäœæããŸããã SOAPãã¡ããªã¯å°è±¡çã§ããããšãå€æããŸãããWS-AddressingãWS-EnumerationãWS-EventingãWS-TransferãWS-TrustãWS-FederationãWebã·ã³ã°ã«ãµã€ã³ãªã³ã§ãã
ãããããããã¯è«ççã§ãããããã§ãæ¬åœã«ç°¡åãªã¢ãããŒããç»å ŽããŸãã-RESTã RESTã®ç¥èªã¯ãè¡šçŸç¶æ ã®è»¢éãè¡šããŸã-ããã¬ãŒã³ããŒã·ã§ã³ã®ç¶æ ã転éãããããŸãã¯èšããŸã§ããªããã¯ã©ã€ã¢ã³ãã«ãšã£ãŠäŸ¿å©ãªåœ¢åŒã§ããŒã¿ã衚瀺ããããšã§ãã ãRESTããšããçšèªã¯ã2000幎ã«Roy Fieldingã«ãã£ãŠå°å ¥ãããŸãããRESTã®äž»ãªèãæ¹ã¯ããµãŒãã¹ãåŒã³åºããã³ã«ã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ãæ°ããç¶æ ã«ãªãããšã§ãã æ¬è³ªçã«ãRESTã¯ãããã³ã«ãŸãã¯æšæºã§ã¯ãªããAPIãèšèšããããã®ã¢ãŒããã¯ãã£ã¹ã¿ã€ã«ã§ããã¢ãããŒãã§ãã
RESTã®ååã¯äœã§ããïŒ
- ã¯ã©ã€ã¢ã³ã/ãµãŒããŒã¢ãŒããã¯ã㣠-ããããªããšãRESTã¯èããããŸããã
- ãã¹ãŠã®ããŒã¿ã¯ãªãœãŒã¹ã§ãã
- ãªãœãŒã¹ã«ã¯ãããŒã¿ãååŸããããã®IDããããŸãã
- ãªãœãŒã¹ã¯çžäºæ¥ç¶ã§ããŸããããã«ã¯ãIDããŸãã¯æãæšå¥šãããããã«ããªã³ã¯ãå¿çã®äžéšãšããŠéä¿¡ãããŸãã ãããããªã³ã¯ãç°¡åã«äœ¿çšã§ããã»ã©ãã¹ãŠãåªããŠãããšããç¹ã«ã¯ãŸã å°éããŠããŸããã
- æšæºã®HTTPã¡ãœãã ïŒGETãPOSTãPUTãDELETEïŒã䜿çšãããŸã-ãããã¯æ¢ã«ãããã³ã«ã«å«ãŸããŠããããããããã䜿çšããŠãµãŒããŒãšå¯Ÿè©±ããããã®ãã¬ãŒã ã¯ãŒã¯ãæ§ç¯ã§ããŸãã
- ãµãŒããŒã¯ç¶æ ãä¿åããŸãããããã¯ããµãŒããŒãåŒã³åºããåé¢ããªãããšãæå³ãããã¹ãŠã®ã»ãã·ã§ã³ãã¡ã¢ãªã«ä¿åããŸããã ãµãŒãã¹ãå®è£ ããäœããã®ãµãŒããŒãã¡ãŒã ã®ã¹ã±ãŒã©ãã«ãªã¯ã©ãŠããããå Žåãææãããã¹ãŠã®ããŒãéã§ãããã®ãµãŒãã¹ã®ã¹ããŒã¿ã¹ã®äžè²«æ§ã確ä¿ããå¿ èŠã¯ãããŸããã ããã«ãããã¹ã±ãŒãªã³ã°ãå€§å¹ ã«ç°¡çŽ åãããŸããå¥ã®ããŒããè¿œå ãããšããã¹ãŠãæ£åžžã«æ©èœããŸãã
RESTã¯ã©ã®ããã«åªããŠããŸããïŒ
- 圌ã¯ãšãŠãã·ã³ãã«ã§ãïŒ
- éåžžã«é·ãéååšããŠãããå€ãã®ããã€ã¹ã§äœ¿çšãããŠããæ¢åã®æšæºãåå©çšããŸãã
- RESTã¯HTTPã«åºã¥ããŠããŸã =>ãã¹ãŠã®ç¹å
žãå©çšå¯èœã§ãïŒ
- ãã£ãã·ã³ã°
- ã¹ã±ãŒãªã³ã°ã
- æå°ãªãŒããŒãããã
- æšæºãšã©ãŒã³ãŒãã
- éåžžã«é«ãæ®åç ïŒIoTããã€ã¹ã§ãããHTTPã§ã®äœæ¥æ¹æ³ãæ¢ã«ç¥ã£ãŠããŸãïŒã
ãã¹ããœãªã¥ãŒã·ã§ã³ïŒãã¯ãããžãŒéäŸåïŒ
ç¹å®ã®å®è£ ã«é¢é£ããªãçŸä»£äžçã§ã®æè¯ã®ãœãªã¥ãŒã·ã§ã³ã¯äœã§ããïŒ ãããã®ãœãªã¥ãŒã·ã§ã³ã䜿çšããããšããå§ãããŸãã
- SSLèªèšŒãšèªèšŒãªãã§ã¯ç¡æå³ãªã®ã§ã ã©ãã§ãSSLããµãŒãã¹ã®æãéèŠãªãã®ã§ãã
- ãµãŒãã¹ã®ææžåãšããŒãžã§ã³ç®¡ç -ä»äºåæ¥ããã
- POSTããã³PUTã¡ãœãã㯠ãå€æŽãŸãã¯äœæãããªããžã§ã¯ããè¿ãå¿ èŠããããŸããããã«ããããµãŒãã¹ã«ã¢ã¯ã»ã¹ããæéãååã«ççž®ãããŸãã
- ãã£ã«ã¿ãªã³ã°ããœãŒããããã³ããŒãžããŒã·ã§ã³ã®ãµããŒã -ãããæšæºã§ããããã®ãŸãŸäœ¿çšã§ããããšãéåžžã«æãŸããã§ãã
- MediaTypeããµããŒãããŸã ã MediaTypeã¯ãã³ã³ãã³ããåä¿¡ãã圢åŒããµãŒããŒã«æ瀺ããæ¹æ³ã§ãã Web APIã®æšæºå®è£ ã䜿çšããŠãã©ãŠã¶ããã¢ã¯ã»ã¹ãããšãAPIããXMLãæäŸãããPostmanã䜿çšãããšJSONãè¿ãããŸãã
- PrettyprintïŒgzip ã èŠæ±ãæå°åãããJSONïŒãµãŒããŒããéä¿¡ãããå¿çïŒãã³ã³ãã¯ãã«ããªãã§ãã ããã prettyprintã®ãªãŒããŒãããã¯é¢å¿ã®ããåäœã§ãããã¡ãã»ãŒãžã®åèšãµã€ãºã«å¯ŸããŠã¿ããããã€ããããèŠããšããããŸãã ã¿ããåé€ããŠãã¹ãŠã1è¡ã§éä¿¡ãããšããããã°ã«ç²ããŠããŸããŸãã gzipã«é¢ããŠã¯ãæã è³éãäžããŸãã T. hãprettyprintãšgzipã®äž¡æ¹ã䜿çšããããšã匷ããå§ãããŸãã
- æšæºã®ãã£ãã·ã³ã°ã¡ã«ããºã ïŒETagïŒãšLast-ModifiedïŒæçµå€æŽæ¥ïŒã®ã¿ã䜿çšããŸã-ãããã®2ã€ã®ãã©ã¡ãŒã¿ãŒã¯ããµãŒããŒãã³ã³ãã³ããæŽæ°ããå¿ èŠããªãããšãã¯ã©ã€ã¢ã³ãã«ç解ãããã®ã«ååã§ãã èªåã§äœããçºæããããšã¯æå³ããããŸããã
- åžžã«æšæºã®HTTPãšã©ãŒã³ãŒãã䜿çšããŸã ã ãããªããã°ããã€ããã¯ã©ã€ã¢ã³ããäœããã®çç±ã§æãã€ããæ¹æ³ã§ãããžã§ã¯ãã®ãšã©ãŒ419ã解éããå¿ èŠããããšå€æããçç±ã誰ãã«èª¬æããå¿ èŠããããŸãã ããã¯äžäŸ¿ã§andãã§ã-ãã®ãããã¯ã©ã€ã¢ã³ãã¯ããªãã«æè¬ãèšããªãã§ãããïŒ
HTTPã¡ãœããã®ããããã£
ä»æ¥ã¯ãGETãPOSTãPUTãDELETEã«ã€ããŠã®ã¿èª¬æããŸãã
è¡šã«ç€ºãããŠãããã®ä»ã«ã€ããŠç°¡åã«èª¬æããŸããOPTIONS-ã»ãã¥ãªãã£èšå®ã®åä¿¡ãHEAD-ã¡ãã»ãŒãžæ¬æã®ãªãããããŒã®åä¿¡ãPATCH-ã³ã³ãã³ãã®éšåçãªå€æŽã
ã芧ã®ãšãããè¡šã«ç€ºãããŠããPOST以å€ã®ãã¹ãŠã®ã¡ãœããã¯ã¹ãçã§ãã Içæ§-ãµãŒãã¹ã«å¯ŸããŠåãåŒã³åºããè€æ°åå®è¡ããèœåãçãã¯æ¯ååãã«ãªããŸãã èšãæããã°ããã®ã¢ã¯ã·ã§ã³ãå®è¡ããçç±ãšåæ°ã¯é¢ä¿ãããŸããã ãªããžã§ã¯ããå€æŽããã¢ã¯ã·ã§ã³ïŒPUTïŒãå®è¡ãããšã©ãŒãåãåã£ããšããŸãã äœãåå ã§ãã©ã®æç¹ã§ãªããžã§ã¯ããå€æŽããããã©ããã¯ããããŸããã ãã ããã¹ãçæ§ã®ãããã§ããã®ã¢ã¯ã·ã§ã³ãå床å®è¡ã§ããããšãä¿èšŒãããŸããã€ãŸãã顧客ã¯ããŒã¿ã®æŽåæ§ã«ã€ããŠå·éã«ãªããŸãã
ãå®å šããšã¯ããµãŒããŒã«ã¢ã¯ã»ã¹ããŠãã³ã³ãã³ããå€æŽãããªãããšãæå³ããŸãã ãã®ãããGETã¯äœåºŠãåŒã³åºãããšãã§ããŸãããã³ã³ãã³ãã¯å€æŽãããŸããã GETããã£ãã·ã¥ã§ãããšããäºå®ã®ããã«åœŒãã³ã³ãã³ããå€æŽããå Žåããã£ãã·ã¥ãåŠçããããã€ãã®ããªãããŒãªãã©ã¡ãŒã¿ãŒãäœæããå¿ èŠããããŸãã
ããŒã2.ç·Žç¿
æè¡ã®éžæ
RESTã®ããã¿ãç解ããã®ã§ãRESTã®ååãæºããRESTful API¬ãµãŒãã¹ã®èšè¿°ãéå§ã§ããŸãã ãã¯ãããžãŒã®éžæããå§ããŸãããã
æåã®ãªãã·ã§ã³ã¯WCFãµãŒãã¹ã§ãã éåžžããã®ãã¯ãããžãŒã䜿çšããŠäœæ¥ãã人ã¯ãããããã«æ»ããããããŸãããæ·±å»ãªæ¬ ç¹ããããããã€ãã®å©ç¹ããããŸãã
-webHttpBindingã®ã¿ïŒãããŠããã以å€ã¯ãªãã§ããïŒ..ïŒã
-HTTP Getããã³POSTïŒããã³ãã¹ãŠïŒã®ã¿ããµããŒããããŸãã
+ããŸããŸãªåœ¢åŒã®XMLãJSONãATOMã
2çªç®ã®ãªãã·ã§ã³ã¯Web APIã§ãã ãã®å Žåãå©ç¹ã¯æããã§ãã
+ãšãŠãã·ã³ãã«ã
+ãªãŒãã³ãœãŒã¹ã
+ãã¹ãŠã®HTTPæ©èœã
+ MVCã®ãã¹ãŠã®æ©èœã
+軜éã
+å€æ°ã®åœ¢åŒããµããŒãããŸãã
åœç¶ãWeb APIãéžæããŸãã 次ã«ãWeb APIã®é©åãªãã¹ãã£ã³ã°ãéžæããŸãã
Web APIãã¹ãã£ã³ã°ã®éžæ
ååãªãªãã·ã§ã³ããããŸãïŒ
- ASP.NET MVCïŒå€ãè¯ãïŒã
- AzureïŒã¯ã©ãŠãæ§é ïŒã
- OWIN-.NETçšã®ãªãŒãã³Webã€ã³ã¿ãŒãã§ã€ã¹ïŒMicrosoftã®æè¿ã®éçºïŒã
- IIS
- èªå·±ãã¹ã
OWI
OWINã¯ãã©ãããã©ãŒã ãŸãã¯ã©ã€ãã©ãªã§ã¯ãªããWebã¢ããªã±ãŒã·ã§ã³ãšãµãŒããŒå®è£ ã®åŒ·åãªæ¥ç¶ãæé€ããä»æ§ã§ãã OWINããµããŒãããä»»æã®ãã©ãããã©ãŒã ã§ãå€æŽãªãã§ã¢ããªã±ãŒã·ã§ã³ãå®è¡ã§ããŸãã å®éãä»æ§ã¯éåžžã«åçŽã§ãããã©ã¡ãŒã¿ãŒãšãã®å€ã®ãèŸæžãã«ãããŸããã åºæ¬ãã©ã¡ãŒã¿ãŒã¯ä»æ§ã§å®çŸ©ãããŠããŸãã
OWINã¯éåžžã«ã·ã³ãã«ãªãã¶ã€ã³ã«ãªããŸãã
ã¹ããŒã ã«ããã°ãããã©ã¡ãŒã¿-å€ãã®ãªã¹ãã§æ§æãããéåžžã«åçŽãªãèŸæžãããµããŒããããµãŒããŒããããã¹ããããããšãããããŸãã ãã®ãµãŒããŒã«æ¥ç¶ãããã¹ãŠã®ã¢ãžã¥ãŒã«ã¯ããã®ããã«æ§æãããŸãã ç¹å®ã®ãã©ãããã©ãŒã ã«ãã€ã³ãããããã®ã³ã³ãã©ã¯ãããµããŒããããµãŒããŒã¯ããããã®ãã©ã¡ãŒã¿ãŒããã¹ãŠèªèããããã«å¿ããŠã€ã³ãã©ã¹ãã©ã¯ãã£ãåæåã§ããŸãã OWINã§åäœãããµãŒãã¹ãäœæãããšãã³ãŒããå€æŽããããšãªãèªç±ã«ãã©ãããã©ãŒã éã§è»¢éããåããã®ãä»ã®OSã§äœ¿çšã§ããããšãããããŸãã
Katanaã¯ãMicrosoftã®OWINå®è£ ã§ãã IISã§OWINã¢ã»ã³ããªããã¹ãã§ããŸãã ããã¯ãéåžžã«åçŽãªå€èŠ³ã§ãã
[assembly: OwinStartup(typeof (Startup))] namespace RestApiDemo { public class Startup { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); app.UseWebApi(config); } } }
ã¹ã¿ãŒãã¢ãããã©ã®ã¯ã©ã¹ãã瀺ããŸãã ããã¯ãIISã«ãã£ãŠçæãããåçŽãªdllã§ãã ã³ã³ãã£ã®ã¥ã¬ãŒã¿ãŒãåŒã³åºãããŸãã ãã®ã³ãŒãã¯ãããæ©èœãããã®ã«ååã§ãã
èšèšã€ã³ã¿ãŒãã§ãŒã¹
次ã«ãã€ã³ã¿ãŒãã§ãŒã¹ãèšèšãããã¹ãŠãã©ã®ããã«èŠããããã©ã®ã«ãŒã«ã«æºæ ãããã確èªããŸãã RESTã®ãã¹ãŠã®ãªãœãŒã¹ã¯åè©ã§ããã觊ããã觊ãããããããšãã§ããŸãã
äŸãšããŠãé§ ã§ã®åè»ã®ã¹ã±ãžã¥ãŒã«ã䜿çšããç°¡åãªã¢ãã«ãèŠãŠã¿ãŸãããã ç°¡åãªRESTãªã¯ãšã¹ãã次ã«ç€ºããŸãã
- APIã®ã«ãŒãïŒç¬ç«ïŒãšã³ãã£ãã£ïŒ
- GET /ã¹ããŒã·ã§ã³-ãã¹ãŠã®ã¹ããŒã·ã§ã³ãååŸããŸãã
- GET / station / 123-ID = 123ã®ã¹ããŒã·ã§ã³ã«é¢ããæ å ±ãååŸããŸãã
- GET / trains-ãã¹ãŠã®åè»ã®æå»è¡šã
- äŸåïŒã«ãŒãïŒãšã³ãã£ãã£ïŒ
- GET /é§ / 555 /åºçº-é§ 555ãåºãåè»ã
次ã«ãDDDã«ã€ããŠã話ããŸãããªããããè¡ãã®ã§ããã
ã³ã³ãããŒã©ãŒ
ãã®ãããã¹ããŒã·ã§ã³ããããŸãã次ã«ãåçŽãªã³ã³ãããŒã©ãŒãäœæããå¿ èŠããããŸãã
[RoutePrefix("stations")] public class RailwayStationsController : ApiController { [HttpGet] [Route] public IEnumerable<RailwayStationModel> GetAll() { return testData; } RailwayStationModel[] testData = /*initialization here*/ }
ããã¯å±æ§ããŒã¹ã®ã«ãŒãã£ã³ã°ã§ãã ããã§ãã³ã³ãããŒã©ãŒã®ååãæå®ãããªã¹ãïŒãã®å Žåã¯ã©ã³ãã ãªãã¹ãããŒã¿ïŒãèŠæ±ããŸãã
ODataïŒwww.odata.orgïŒ
ã¯ã©ã€ã¢ã³ãã«å¿ èŠãªããŒã¿ãããå€ãã®ããŒã¿ããããšæ³åããŠãã ããïŒ100å以äžãã©ãã°ããŠãã»ãšãã©æå³ããããŸããïŒã åæã«ããã¡ãããç§ã¯èªåã§ããŒãžããŒã·ã§ã³ãæžããããããŸããã 代ããã«ãç°¡åãªæ¹æ³ããããŸã-Web APIã§ãµããŒããããŠããODataã®ã©ã€ãããŒãžã§ã³ã䜿çšããŸãã
[RoutePrefix("stations")] public class RailwayStationsController : ApiController { [HttpGet] [Route] [EnableQuery] public IQueryable<RailwayStationModel> GetAll() { return testData.AsQueryable(); } RailwayStationModel[] testData = /*initialization here*/ }
IQueryableã䜿çšãããšãã·ã³ãã«ã ãå¹æçãªã¯ã©ã€ã¢ã³ãåŽã®ãã£ã«ã¿ãªã³ã°ããã³ããŒã¿ç®¡çã¡ã«ããºã ã䜿çšã§ããŸãã è¡ãã¹ãããšã¯ãNuGetããODataã¢ã»ã³ããªãæ¥ç¶ããEnableQueryãæå®ããŠãiQueryableã€ã³ã¿ãŒãã§ã€ã¹ãè¿ãããšã ãã§ãã
ãã®ãããªã©ã€ãããŒãžã§ã³ãšãã«ããŒãžã§ã³ã®äž»ãªéãã¯ãã¡ã¿ããŒã¿ãè¿ãã³ã³ãããŒã©ãŒããªãããšã§ãã æ¬æ ŒçãªODataã¯çããå°ãå€æŽãïŒç¹å¥ãªã©ãããŒã§è¿ãã¢ãã«ãã©ããããŸãïŒããªããžã§ã¯ãã®ãªã³ã¯ãããããªãŒãè¿ãããšãã§ããŸãã ãŸããODataã®è»œéããŒãžã§ã³ã§ã¯ãçµåãã«ãŠã³ããªã©ã®ããšã¯ã§ããŸããã
ãªã¯ãšã¹ããã©ã¡ãŒã¿
ãããŠãããªããã§ããããšã¯æ¬¡ã®ãšããã§ãã
- $ filter-ååãªã©ã§ãã£ã«ã¿ãªã³ã°ããŸãã ãã¹ãŠã®æ©èœã¯OData Webãµã€ãã§èŠãããšãã§ããŸã -ãããã¯éåžžã«åœ¹ç«ã¡ãéžæãå€§å¹ ã«å¶éããããšãã§ããŸãã
- $ selectã¯éåžžã«éèŠã§ãã 倧ããªã³ã¬ã¯ã·ã§ã³ãããããã¹ãŠã®ãªããžã§ã¯ãã倪ããã衚瀺ãããIDãšåå以å€ã®ããããããŠã³ãäœæããå¿ èŠãããå Žåããã®é¢æ°ã圹ç«ã¡ãŸããããã«ããããµãŒããŒãšã®ããåããç°¡çŽ åãããé«éåãããŸãã
- $ orderby-䞊ã¹æ¿ãã
- $ topãš$ skipã¯ãµã³ãã«ã®å¶éã§ãã
ããã¯ãèªåã§è»èŒªãåçºæããªãããã«ååã§ãã ããã¯ãã¹ãŠãBreezeãªã©ã®æšæºJSã©ã€ãã©ãªã䜿çšããŠå®è¡ã§ããŸãã
EnableQueryå±æ§
å®éãODataã¯éåžžã«ç°¡åã«èªåã®è¶³ã§æã€ããšãã§ãããã®ã§ãã ããŒãã«ã«æ°çŸäžã®ã¬ã³ãŒããããããããããµãŒããŒããã¯ã©ã€ã¢ã³ãã«ãã«ããå¿ èŠãããå Žåãããã¯å°é£ã§ããããã®ãããªãªã¯ãšã¹ããå€æ°ããå Žåãå®å šã«èŽåœçã§ãã
ãã®ãããªå ŽåãEnableQueryå±æ§ïŒäžèšã®ã³ãŒããåç §ïŒã«ã¯ãå€ããå¶éããããã«äœ¿çšã§ãããã©ã¡ãŒã¿ãŒã®ã»ããããããŸããå¿ èŠä»¥äžã®è¡ãäžããããçµåãç®è¡æŒç®ãªã©ãäžãããããªãã§ãã ãããäœãå¿ èŠãããŸããã
- AllowedArithmeticOperators
- èš±å¯ãããæ©èœ
- AllowedLogicalOperators
- AllowedOrderByProperties
- AllowedQueryOptions
- EnableConstantParameterization
- EnsureStableOrdering
- HandleNullPropagation
- MaxAnyAllExpressionDepth
- MaxExpansionDepth
- MaxNodeCount
- MaxOrderByNodeCount
- ããã¯ã¹ã¹ããã
- ããã¯ã¹ããã
- PageSize
äŸåã³ã³ãããŒã©ãŒ
ãã®ããã以äžã«ç°¡åãªRESTãªã¯ãšã¹ãã瀺ããŸãã
- GET /ã¹ããŒã·ã§ã³-ãã¹ãŠã®ã¹ããŒã·ã§ã³ãååŸ
- GET /åè»-ãã¹ãŠã®åè»ã®ã¹ã±ãžã¥ãŒã«
- GET /é§ / 555 /å°ç
- GET /é§ / 555 /åºçº
555ã®ã¹ããŒã·ã§ã³ãããããã¹ãŠã®åºçºãšå°çãåãåããããšããŸãã æããã«ãããã§ã¯ãšã³ãã£ãã£ã䜿çšããå¿ èŠããããŸãããããã¯ã¹ããŒã·ã§ã³ã®æ§è³ªã«ãã£ãŠç°ãªããŸãã ããããã³ã³ãããŒã©ãŒã§ãããè¡ãæ¹æ³ã¯ïŒ ã«ãŒãã£ã³ã°å±æ§ã䜿çšããŠããããã¹ãŠå®è¡ããããã1ã€ã®ã¯ã©ã¹ã«å ¥ãããšããã®ãããªäŸã§ã¯åé¡ããªãããšã¯æããã§ãã ãã ãããã¹ãããããšã³ãã£ãã£ã1ããŒã¹ãããæ·±ããããã«å€§ãããªããšããµããŒããããŠããªã圢åŒã«ãªããŸãã
ãããŠãããã«ç°¡åãªè§£æ±ºçããããŸã-ã³ã³ãããŒã©ã®ã«ãŒãã£ã³ã°å±æ§ã§å€æ°ãäœæã§ããŸãïŒ
[RoutePrefix("stations/{station}/departures")] public class TrainsFromController : TrainsController { [HttpGet] [Route] [EnableQuery] public IQueryable<TrainTripModel> GetAll(int station) { return GetAllTrips().Where(x => x.OriginRailwayStationId == station); } }
ãããã£ãŠããã¹ãŠã®äŸåãšã³ãã£ãã£ãåå¥ã®ã³ã³ãããŒã©ãŒã«æã¡èŸŒã¿ãŸãã ãããã¯å¥ã ã«äœãã§ããã®ã§ãã©ãã ãå®å šã«éèŠã§ã¯ãããŸããã Web APIã®èŠ³ç¹ããã¯ããããã¯ç°ãªãã³ã³ãããŒã©ãŒã«ãã£ãŠèªèãããŸããURLã®ããã«èŠããã«ãããããããã·ã¹ãã èªäœã¯äŸåããŠããããšãèªèããŠããªãããã§ãã
å¯äžã®åé¡ã¯ãããã«ãã¹ããŒã·ã§ã³ããããããã®åã«ãã¹ããŒã·ã§ã³ãããã£ãããšã§ãã ããå Žæã§äœããå€æŽããå¥ã®å Žæã§ã¯äœãå€æŽããªããšãäœãæ©èœããŸããã ãã ããç°¡åãªè§£æ±ºçããããŸã- ã«ãŒãã£ã³ã°ã«å®æ°ã䜿çšããïŒ
public static class TrainsFromControllerRoutes { public const string BasePrefix = RailwayStationsControllerRoutes.BasePrefix + "/{station:int}/departures"; public const string GetById = "{id:int}"; }
次ã«ãäŸåã³ã³ãããŒã©ãŒã¯æ¬¡ã®ããã«ãªããŸãã
[RoutePrefix(TrainsFromControllerRoutes.BasePrefix)] public class TrainsFromController : TrainsController { [HttpGet] [Route] [EnableQuery] public IQueryable<TrainTripModel> GetAll(int station) { return GetAll().Where(x => x.OriginRailwayStationId == station); } }
äŸåããã³ã³ãããŒã©ãŒã«å¯ŸããŠç°¡åãªæäœãè¡ãããšãã§ããŸã-èªåã§ã«ãŒããååŸããŠèšç®ããã°ãééããããŸããã ããã«ããããã¯ãã¹ãã§äœ¿çšãããšäŸ¿å©ã§ãã ãã¹ããæžããããã管çããæ¯åäœçŸäžãã®ãã¹ããå®è¡ããããããã®çžå¯ŸURLã瀺ãããŠãããã¹ãŠã®è¡ãä¿®æ£ããå Žåã¯ããããã®å®æ°ã䜿çšããããšãã§ããŸãã ããããå€æŽãããšãããŒã¿ã¯ã©ãã§ãå€æŽãããŸãã ãšãŠã䟿å©ã§ãã
CRUD
ãã®ãããåçŽãªGETæäœãã©ã®ããã«èŠãããã«ã€ããŠèª¬æããŸããã 誰ããåäžã®GETãäœæããæ¹æ³ãç解ããŠããŸãã ãããã圌ã®ã»ãã«ãããã«3ã€ã®æäœã«ã€ããŠèª¬æããå¿ èŠããããŸãã
- POST-æ°ãããšã³ãã£ãã£ãäœæããŸã
- POST / Stations-ãšã³ãã£ãã£å šäœã®JSONèšè¿°ã ãã®ã¢ã¯ã·ã§ã³ã¯ãæ°ãããšã³ãã£ãã£ãã³ã¬ã¯ã·ã§ã³ã«è¿œå ããŸãã
- äœæããããšã³ãã£ãã£ãè¿ããŸãïŒæåã«ããµãŒããŒãžã®äºéã®ããªããããªãããã«ããŸãã次ã«ãå¿ èŠã«å¿ããŠããã®ãªããžã§ã¯ãã§ã«ãŠã³ããããã¯ã©ã€ã¢ã³ãã§å¿ èŠãªãã©ã¡ãŒã¿ãŒããµãŒããŒåŽããè¿ããŸãïŒã
- PUT-ãšã³ãã£ãã£ã®å€æŽ
- PUT / Stations / 12-ID = 12ã®ãšã³ãã£ãã£ãå€æŽããŸãããã©ã¡ãŒã¿ãŒã«å«ãŸããJSONã¯äžæžããããŸãã
- å€æŽããããšã³ãã£ãã£ãè¿ããŸãã äœåãé©çšããããã¹ã¯ãã·ã¹ãã ãåãç¶æ ã«ããå¿ èŠããããŸãã
- åé€
- DELETE / Stations / 12-ID = 12ã®ãšã³ãã£ãã£ãåé€ããŸã
ãã®ä»ã®CRUDã®äŸïŒ
- POST /ã¹ããŒã·ã§ã³-ã¹ããŒã·ã§ã³ãè¿œå ããŸãã
- POST /é§ / 1 /åºçº-é§ 1ããã®åºçºã«é¢ããæ å ±ãè¿œå ããŸãã
- åé€/é§ / 1 /åºçº/ 14-é§ 1ããã®åºçºã®èšé²ãåé€ããŸãã
- GET /é§ / 33 /åºçº/ 10 /ãã±ãã-é§ 33ããã®åºçº10ã§è²©å£²ããããã±ããã®ãªã¹ãã
ããŒãã¯å¿ ç¶çã«äœããã®ãšã³ãã£ãã£ã§ãããã觊ãããããšãã§ãããã®ïŒãã±ãããé»è»ãé»è»ãéããããšããäºå®ãªã©ïŒã§ããããšãç解ããããšãéèŠã§ãã
ã¢ã³ããã¿ãŒã³
ãããŠããããè¡ãæ¹æ³ã®äŸã次ã«ç€ºããŸãã
- GET / Stations /ïŒOp =åºçºãšåè»= 11
ããã§ã¯ãã¯ãšãªæååã¯ããŒã¿è»¢éã ãã§ãªããã¢ã¯ã·ã§ã³ã«ã䜿çšãããŸãã - GET /ã¹ããŒã·ã§ã³/ DeleteAll
ããã¯å®éã®äŸã§ãã ããã§ããã®ã¢ãã¬ã¹ã«å¯ŸããŠGETãå®è¡ããŸããçè«çã«ã¯ãã³ã¬ã¯ã·ã§ã³ãããã¹ãŠã®ãšã³ãã£ãã£ãåé€ããå¿ èŠããããŸãããã®çµæããã£ãã·ã¥ã«ããéåžžã«äºæž¬äžèœãªåäœãããŸãã - POST / GetUserActivity
å®éã«ã¯ãPOSTãšããŠèšè¿°ãããGETã§ãã ããã£ã®ãªã¯ãšã¹ããã©ã¡ãŒã¿ã®ããã«POSTãå¿ èŠã§ããããGETã§ããã£ã«æž¡ãããšã¯ã§ããŸãã-GETã¯ã¯ãšãªæååã«ã®ã¿æž¡ãããšãã§ããŸãã GETã¯ãæšæºã§ã¯æ¬äœããµããŒãããŠããŸããã - POST /ã¹ããŒã·ã§ã³/äœæ
ããã§ã¯ãã¢ã¯ã·ã§ã³ã¯URLã®äžéšãšããŠç€ºãããŠããŸã-ããã¯åé·ã§ãã
APIã®èšèš
人ã ã«æäŸãããAPIãããããã¡ã€ã³ã¢ãã«ããããšããŸãã APIãšã³ãã£ãã£ã¯ãã¡ã€ã³ã¢ãã«ã«ã©ã®ããã«é¢é£ããŠããŸããïŒ ã¯ãã圌ãã¯å®éã«ã¯ãããªãæ¹æ³ã§ãæ¥ç¶ãããŠããŸããã ããã¯å¿ èŠãããŸãããAPIã§è¡ãããšã¯ãå éšãã¡ã€ã³ã¢ãã«ãšã¯é¢ä¿ãããŸããã
質åãçºçããå¯èœæ§ããããŸãããCRUDã§ãªãå Žåã®APIã®èšèšæ¹æ³ ãããè¡ãã«ã¯ãã¢ã¯ã·ã§ã³ãå€æŽã³ãã³ããšããŠèšé²ããŸãã ã³ãã³ãã®ä¿åãèªã¿åããåé€ãGETããã®ã³ãã³ãã®ã¹ããŒã¿ã¹ã®ç¢ºèªãè¡ããŸãã ã³ãã³ãã®ã³ã¬ã¯ã·ã§ã³ããGET-ç¹å®ã®ãšã³ãã£ãã£ã«å¯ŸããŠéä¿¡ãããã¹ãŠã®ã³ãã³ãã®ãªã¹ããååŸããŸãã
ãã¡ã€ã³ã¢ãã«
ãã¡ã€ã³ã¢ãã«ãšãªããžã§ã¯ãã®é¢ä¿ã«ã€ããŠèª¬æããŸãã ãã®äŸã§ã¯ãããã«ïŒããã«ïŒããããäºçŽãéšå±ïŒéšå±ïŒãããã€ã¹ïŒããã€ã¹ïŒãæ¥ç¶ãããŠããŸãã ç§ãã¡ã®ãããžã§ã¯ãã§ã¯ããããã®ããã€ã¹ã䜿çšããŠéšå±ãå¶åŸ¡ã§ããŸããã
ããããããã«äžéããããŸããããã€ã¹ã¯ããèªèº«ã®ç掻ãå¶ãååšã§ãããããã«ããã©ã®ããã«åé¢ããã®ãã¯äžæã§ãã ããããããã«ãšããã€ã¹ã®åé¢ã¯å®éã«ã¯éåžžã«ç°¡åã§ã-DDDã圹ç«ã¡ãŸãã ãŸãããã¡ã€ã³é åã®å¢çãã©ãã«ããã®ããã·ã¹ãã ã®äžè²«æ§ãæ åœãããšã³ãã£ãã£ã®å¢çãã©ãã«ããã®ããææ¡ããå¿ èŠããããŸãã
æçã³ã³ããã¹ãïŒBCïŒ
éå®ãããã³ã³ããã¹ãïŒåé¢ããããµããã¡ã€ã³ïŒ-å®éã«ã¯ãäºãã«ç¬ç«ããŠãããå®å šã«ç¬ç«ããã¢ãã«ïŒç°ãªãïŒãæã€ãªããžã§ã¯ãã®ã»ããã ãã®äŸã§ã¯ãããã«ãšããã€ã¹ã2ã€ã®ç°ãªãBCã«åã蟌ãã§ãã«ã§ããŸãããããã¯çžäºæ¥ç¶ãããŠããŸããããéè€ããŠããŸãã è¿œå ã®ãšã³ãã£ãã£ïŒAttachedDeviceïŒããããŸãã
ããã§ã¯ãåãããã€ã¹ã®ç°ãªãè¡šçŸããããå¿é ããããšã¯ãããŸããã
DDDã§ã¯ãéçŽã«ãŒãã¯ãã¹ãŠã®åå«ãææãããšã³ãã£ãã£ã§ãã ãããããªãŒïŒããã«ïŒã®æäžéšã§ãã ããªããä»ã®ãã¹ãŠã䌞ã°ãããšãã§ããäœãã ããããAttachedDeviceã¯ãã®ããã«åãããšãã§ããŸãã-ããã¯ååšãããæå³ããããŸããã ãŸãã客宀ãšäºçŽã¯ã©ã¹ã¯æå³ããªãããããã«ãšã¯é¢å©ããŠããŸãã ãããã£ãŠãããããã¹ãŠã®ã¯ã©ã¹ãžã®ã¢ã¯ã»ã¹ã¯ãã«ãŒããšã³ãã£ãã£ããã®å Žåã¯Hotelã®ã¿ãä»ããŠè¡ãããŸãã ããã€ã¹ã¯æåããç°ãªãã«ãŒãã§ããããã£ãŒã«ãã®ã»ãããç°ãªãå¥ã®ããªãŒã§ãã
ãããã£ãŠã1ã€ã®ãšã³ãã£ãã£ã2ã€ã®ç°ãªããã¡ã€ã³ã§ãã¬ã€ããããšãç解ããŠããå Žåã¯ããããã«ããããã ãã§ãã ããšãã°ãAttachedDeviceã«ã¯éšå±çªå·ã®ãã£ãŒã«ãããããŸãããDeviceã§ã¯ãããã®ãã£ãŒã«ãã¯äžèŠã§ãã
ãã®ãããªãã¡ã€ã³ã¢ãã«ã§ã®ã¯ãšãªã®äŸã次ã«ç€ºããŸãã
- PUT /ããã«/ 555 /éšå±/ 105 / attachmentDevices-æ¥ç¶ãããããã€ã¹ã®ã³ã¬ã¯ã·ã§ã³å šäœãæ°ãããã®ã«çœ®ãæããŸãã
- POST /ããã«/ 555 /éšå±/ 105 / attachmentDevices-å¥ã®ããã€ã¹ããã€ã³ãããŸãã
- DELETE / hotels / 12-ID = 12ã®ããã«ã®èª¬æãåé€ããŸã
- POST /ããã«/ 123 /äºçŽ-ããã«ID = 123ã§æ°ããäºçŽãäœæããŸãã
CQRS-ã³ãã³ãã¯ãšãªã®è²¬ä»»åé¢
ããã§ã¯ããã®ã¢ãŒããã¯ãã£ã«ã€ããŠã¯èª¬æããŸãããããã®åäœåçã«ã€ããŠç°¡åã«èª¬æããŸãã CQRSã¢ãŒããã¯ãã£ã¯ãããŒã¿ã¹ããªãŒã ã®åé¢ã«åºã¥ããŠããŸãã
ãŠãŒã¶ãŒããã¡ã€ã³ããµãŒããŒã«å€æŽããã³ãã³ããéä¿¡ããã¹ã¬ããã1ã€ãããŸãã ãã ããå®éã«å€æŽãè¡ããããšããäºå®ã§ã¯ãããŸããããŠãŒã¶ãŒã¯ããŒã¿ãçŽæ¥æäœããŸããã ãããã£ãŠããŠãŒã¶ãŒããšã³ãã£ãã£ãå€æŽããã³ãã³ããéä¿¡ããåŸããµãŒããŒã¯ãããåŠçããèªã¿åãçšã«æé©åãããã¢ãã«ã«è»¢éããŸã-UIã¯ãããèªã¿åããŸãã
ãã®ã¢ãããŒãã«ãããRESTã®ååã«åŸãããšãéåžžã«ç°¡åã«ãªããŸãã ããŒã ãããå ŽåããããŒã ã®ãªã¹ããã®æ¬è³ªããããŸãã
PUTãªãã®REST
åçŽãªCRUDã®äžçã§ã¯ãPUTã¯ãªããžã§ã¯ããå€æŽãããã®ã§ãã ããããå³å¯ã«CQRSã®ååã«åŸãããã¹ãŠã®ã³ãã³ããå®è¡ãããšããªããžã§ã¯ããå€æŽã§ããªããããPUTã¯æ¶ããŸãã 代ããã«ããªããžã§ã¯ãã«å€æŽã³ãã³ãã®ã¿ãéä¿¡ã§ããŸãã åæã«ãå®è¡ã¹ããŒã¿ã¹ã®è¿œè·¡ãã³ãã³ãã®ãã£ã³ã»ã«ïŒDELETEïŒãå€æŽå±¥æŽã®ç°¡åãªä¿åããŠãŒã¶ãŒã¯äœãå€æŽãããåã«æå³ãå ±åããã ãã§ãã
PUTãã©ãã€ã ã䜿çšããªãRESTã¯ãŸã è°è«ã®äœå°ãããããŸã å®å šã«ã¯ãã¹ããããŠããŸããããå Žåã«ãã£ãŠã¯å®éã«ååã«é©çšã§ããŸãã
现ç²åºŠVSç²ç²åºŠ
ããªããçŽ æŽããããµãŒãã¹ãçŽ æŽãããæœèšãããŠãããšæ³åããŠãã ããã ããã«ã¯ã现ç²åºŠAPIãšç²ç²åºŠAPIïŒã现ç²åºŠãããã³ãç²ç²åºŠãAPIïŒã®2ã€ã®ã¢ãããŒãããããŸãã
ãã现ããAPIïŒ
- ããããã®å°ããªãªããžã§ã¯ãã
- ããžãã¹ããžãã¯ã¯ã¯ã©ã€ã¢ã³ãåŽã«è¡ããŸãã
- ãªããžã§ã¯ãã®æ¥ç¶æ¹æ³ãç¥ãå¿ èŠããããŸãã
ç²èŠåAPIïŒ
- ããã«ãšã³ãã£ãã£ãäœæããŸãã
- ããšãã°ãããŒã«ã«ã§å€æŽãå ããããšã¯å°é£ã§ã
- POST /ããã°/ {id} /ãããã
- ã¯ã©ã€ã¢ã³ãã®ã¹ããŒã¿ã¹ãç£èŠããå¿ èŠããããŸãã
- 倧ããªãªããžã§ã¯ãã¯éšåçã«ä¿åã§ããŸããã
éå§ããã«ã¯ãããã®çŽ°ããAPIãèšèšããããšããå§ãããŸãããªããžã§ã¯ããäœæãããã³ã«ããµãŒããŒã«éä¿¡ããŸãã ã¯ã©ã€ã¢ã³ãåŽã®ã¢ã¯ã·ã§ã³ããšã«ããµãŒããŒã«ã¢ã¯ã»ã¹ããŸãã ãã ããå°ããªãšã³ãã£ãã£ã®æäœã¯ã倧ããªãšã³ãã£ãã£ã®å Žåãããç°¡åã§ãã倧ããªãšã³ãã£ãã£ãèšè¿°ããå ŽåãåŸã§ãããèŠãããšã¯é£ãããªããå°ããªå€æŽãå ããŠç¬ç«ããéšåãåŒãåºãããšã¯å°é£ã«ãªããŸãã T. hãå°ããªãšã³ãã£ãã£ããå§ããŠãåŸã ã«ããããæ¡å€§ããæ¹ãããã
ããŒãžã§ã³çªå·
æ¥çã®å¥çŽã«å¯ŸããŠéåžžã«ãªã©ãã¯ã¹ããæ 床ãæã€ããã«ãªããŸããã äœããã®çç±ã§ã人ã ã¯ãAPIã䜿çšããŠäœæããå Žåããããäœã§ãã§ããAPIã§ãããšä¿¡ããŠããŸãã ããããããã¯ããã§ã¯ãããŸããã APIãäœæããŠå°ãªããšã1ã€ã®ååŒå ã«æäŸããå Žåãããã¯ãã¹ãŠããŒãžã§ã³1.0ã§ãã å€æŽãè¡ããšãããŒãžã§ã³ãå€æŽãããŸãã çµå±ã人ã ã¯ããªãã®ã³ãŒããããªããæäŸããããŒãžã§ã³ã«ãªã³ã¯ããã§ãããã
æåŸã®ãããžã§ã¯ãã§ã¯ãã¯ã©ã€ã¢ã³ãã«æäŸããããšããçç±ã ãã§APIãæ°åããŒã«ããã¯ããå¿ èŠããããŸããããšã©ãŒã³ãŒããå€æŽããŸããããã¯ã©ã€ã¢ã³ãã¯ãã§ã«å€ãã³ãŒãã«æ £ããŠããŸããã
Web APIã®çŸåšç¥ãããŠããããŒãžã§ã³ç®¡çãªãã·ã§ã³ã¯äœã§ããïŒ
æãç°¡åãªã®ã¯ãURLã§ããŒãžã§ã³ãæå®ããããšã§ãã
èªåã§äœãããå¿ èŠããªãå Žåã®æ¢è£œã®ãªãã·ã§ã³ã¯æ¬¡ã®ãšããã§ãã
- aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/NamespaceControllerSelector
- aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/RoutingConstraintsSample
- www.strathweb.com/2015/10/global-route-prefixes-with-attribute-routing-in-asp-net-web-api
- github.com/climax-media/climax-web-http
Climax.Web.Httpã©ã€ãã©ãª
èå³æ·±ãæ¢è£œã®ãªãã·ã§ã³ã1ã€ãããŸãã
ããã¯ãå¶çŽä»ãã®å±æ§ã®ã«ãŒãã£ã³ã°ã§ããæ·±å»ãªãªããžã§ã¯ããå®è¡ããå Žåã¯ãããããå¶çŽãå®è¡ããŸããã ãã®å±æ§ã®ããŒãžã§ã³çªå·ã«ãããå¶çŽãå®è£ ããŸããã ãããã£ãŠãç°ãªãããŒãžã§ã³ã§åãã³ã³ãããŒã©ãŒåãæã€åãå±æ§ã§ã2ã€ã®ç°ãªãã¯ã©ã¹ã«ãã³ã°ã¢ããããç°ãªãããŒãžã§ã³ã瀺ããŸãã ãã¹ãŠãç®±ããåºããŠåäœããŸã....
VersionedRoute("v2/values", Version = 2)]<br> <br> config.ConfigureVersioning(<br> versioningHeaderName: "version", vesioningMediaTypes: null);<br> <br> config.ConfigureVersioning(<br> versioningHeaderName: null, <br> vesioningMediaTypes: new [] { "application/vnd.model"});<source lang="cs"> <h6><b></b></h6> open-source-, - Swagger. â Swashbuckle. <ul> <li>http://swagger.io/ </li> <li>https://github.com/domaindrivendev/Swashbuckle</li> </ul> Swashbuckle: <source lang="cs">httpConfiguration .EnableSwagger(c => c.SingleApiVersion("v1", âDemo API")) .EnableSwaggerUi(); public static void RegisterSwagger(this HttpConfiguration config) { config.EnableSwagger(c => { c.SingleApiVersion("v1", "DotNextRZD.PublicAPI") .Description("DotNextRZD Public API") .TermsOfService("Terms and conditions") .Contact(cc => cc .Name("Vyacheslav Mikhaylov") .Url("http://www.dotnextrzd.com") .Email("vmikhaylov@dataart.com")) .License(lc => lc.Name("License").Url("http://tempuri.org/license")); c.IncludeXmlComme nts(GetXmlCommentFile()); c.GroupActionsBy(GetControllerGroupingKey); c.OrderActionGroupsBy(new CustomActionNameComparer()); c.CustomProvider(p => new CustomSwaggerProvider(config, p)); }) .EnableSwaggerUi( c => { c.InjectStylesheet(Assembly.GetExecutingAssembly(), "DotNextRZD.PublicApi.Swagger.Styles.SwaggerCustom.css"); }); } }
ã芧ã®ãšãããSwaggerã¯ç§ãã¡ãæã£ãŠãããã®ããã¹ãŠåŒãåºããXMLã³ã¡ã³ããåŒãåºããŸããã
以äžã¯ãGETã¢ãã«ã®å®å šãªèª¬æã§ãã ãã¿ã³ãã¯ãªãã¯ãããšã圌ã¯å®éã«ãã¿ã³ãå®è¡ããçµæãè¿ããŸãã
ãããŠãããã«POSTã®ããã¥ã¡ã³ãããããŸããæåã®éšåïŒ
2çªç®ã®éšåã¯æ¬¡ã®ãšããã§ãã
XMLã³ã¡ã³ãã§æžããããã¹ãŠãããã«ãããŸãã
ãœãŒã¹
- www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
- www.strathweb.com/2015/10/global-route-prefixes-with-attribute-routing-in-asp-net-web-api
- www.thoughtworks.com/insights/blog/rest-api-design-resource-modeling
- jacobian.org/writing/rest-worst-practices
- piwik.org/blog/2008/01/how-to-design-an-api-best-practises-concepts-technical-aspects
- www.toptal.com/api-developers/5-golden-rules-for-designing-a-great-web-api
- www.odata.org
- owin.org
- pietschsoft.com/post/2014/06/15/cqrs-command-query-responsibility-segregation-design-pattern
- blog.pivotal.io/pivotal-labs/labs/api-versioning