猿问

在微服务中创建域模型和视图模型的正确方法

我正在尝试创建一个微服务,并且我正在努力确保我遵循最佳实践/模式来设计我的对象。

我计划分离我的对象,以便有一个用于返回(使用 dapper 查询)到客户端(下面的类代码)和一个实际执行状态更改(代码不在这个问题中),它将提交更改到 db

我在数据库中有一个实体CalendarEvents并且我有一个视图模型用于调用CalendarEvent它,它具有用于映射的每个实体字段的基本属性,并且我有一堆其他属性为 CalendarEvent 是实体字段的聚合。

问题

  1. 填充此 CalendarEvent 对象(视图模型)的正确方法是什么,如果从从数据库读取的实体创建时所有属性都通过构造函数传递,还是有更好的方法来做到这一点。

  2. 我应该采用 _dateformat 还是有更优雅的方式(这来自配置文件)

下面是我的 CalendarEntity 类(用作视图模型的类)

此外,如果有人可以通过我的方法发现任何其他问题(可能很多),我将不胜感激。


HUX布斯
浏览 132回答 1
1回答

MMMHUHU

我将假设您使用的是 Web Api,但同样的原则也适用于 MVC 应用程序。我通常有一个映射器层,它接受一个域对象并将其映射到视图模型或 dto。这样,您可以在任何时候想要返回相同的视图模型时重用映射器。因此,如果您对视图模型进行更改,它都位于一个地方。请参阅下面的代码以回答问题 1。问题 2:为什么您的 mircoservice 关心客户想要的日期。如果您发回 utc 日期,那么如果客户想要它的格式,那么由他们来格式化。//Fake view modelpublic class CalendarEventViewModel{&nbsp; &nbsp; public int EventId { get; set; }&nbsp; &nbsp; public string Title { get; set; }&nbsp; &nbsp; public DateTime? StartDateTimeUtc { get; set; }&nbsp; &nbsp; public DateTime? EndDateTimeUtc { get; set; }&nbsp; &nbsp; public string Summary { get; set; }&nbsp; &nbsp; public bool IsApproved { get; set; }&nbsp; &nbsp; public string TimeZoneId { get; set; }}public interface IMapper<in TIn, out TOut>{&nbsp; &nbsp; &nbsp; &nbsp; TOut Map(TIn model);}public class CalendarEventViewModelMapper : IMapper<CalendarEvent, CalendarEventViewModel>{&nbsp; &nbsp; public CalendarEventViewModel Map(CalendarEvent model)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return new CalendarEventViewModel&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EndDateTimeUtc = model.EndDateTimeUtc,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EventId = model.EventId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsApproved = model.IsApproved,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StartDateTimeUtc = model.StartDateTimeUtc,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Summary = model.Summary,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeZoneId = model.TimeZoneId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Title = model.Title&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}[Route("api/Values")]public class ValuesController&nbsp;{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public ValuesController( IMapper<CalendarEvent, CalendarEventViewModel> calendarMapper)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_calendarMapper = calendarMapper;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; // GET api/values/5&nbsp; &nbsp; &nbsp; &nbsp; [HttpGet("{id}")]&nbsp; &nbsp; &nbsp; &nbsp; public IActionResult Get(int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var calendarEvent = GetMyCalendarEventFromDB(id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.Ok(_calendarMapper.Map(calendarEvent));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private CalendarEvent GetMyCalendarEventFromDB(int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new CalendarEvent("yyyy-dd-MM")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EndDateTimeUtc = DateTime.UtcNow.AddHours(3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EventId = id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsApproved = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StartDateTimeUtc = DateTime.UtcNow.AddHours(2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Summary = "My magical Event",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeZoneId = "UTC",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Title = "Magical Event"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答