【发布时间】:2020-07-03 23:06:03
【问题描述】:
我有一个 Web 解决方案,其中包括一个 Web UI、Web API 层、服务对象和一个封装存储库(有时使用工作单元模式)和实体框架实体的数据层。我的 Web 服务正在返回我在 UI 控制器中反序列化的 JSON,如下所示:
var response = await httpClient.GetAsync(endpoint);
var result = JsonConvert.DeserializeObject<anyTypeHere>(await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync());
我的 web api 层调用各种“服务”(都表示为它们自己的库项目 - 即:Accounting Service、EnrollmentService、CalendarService)。每个服务对象中都有通过存储库访问数据实体的代码:
// returns entity object
var customer = UnitOfWork.PatientRepository.GetCustomerById();
我想使用 AutoMapper 之类的工具将实体对象映射到 dto/domain 对象,从而将 dto 对象返回给 UI 调用者。
我的问题是:
-
我应该在哪里添加 Automapper 参考和映射代码? (即:WebApi、服务项目、存储层等……)
-
我是否应该创建一个包含 DTO 对象的单独库并在我的 UI 客户端以及 api 和服务层中引用它?
【问题讨论】:
标签: asp.net-core asp.net-web-api asp.net-core-mvc automapper automapper-collections-ef-core