ASP.NET Web API - DTOs

大部分來說, 想要拿到 DB裡面的資料可以直接去 DB拿, 但是這樣會有缺點, 假設資料庫有密碼, 地址, 這一些比較隱密的資訊, 而且這一些資料並不需要透過 API 傳給其他的服務, 那麼直接去 資料庫拿資料就不會是一個好方法
所以DTO就是先過濾掉有需要的資料給其他的 Services,然後再把這一些資料給需要的 Clients

定義

A DTO is an object that defines how the data will be sent over the network.

  • GET:

AutoMapper

上面的例子是我們手動產生 DTO 的 class, 但是在真實世界中會用 AutoMapper 來自動產生我們所想要的 DTO

留言

這個網誌中的熱門文章

Scaffold Identity into the Current Project

Passing data from controller to the view