我正在.net核心Web API环境中的成员资格模块上工作。我需要公开用于更新成员资格的Update方法。到目前为止,这是我的实现;
//Entity
public class Membership
{
public int Id { get; set; }
public int UserId { get; set; }
public MembershipType MembershipType { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool AutoRepeat { get; set; }
public virtual User User { get; set; }
}
public enum MembershipType
{
Standard = 1,
Premium,
Ultimate
}
我的控制器方法希望该模型更新相关实体
public class MembershipUpdateModel
{
public int Id { get; set; }
public MembershipType MembershipType { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool AutoRepeat { get; set; }
}
此类包含我的业务逻辑,例如验证等。
public class MembershipManager
{
//This instance handles db related works.
IMembershipService membershipService;
public MembershipManager(IMembershipService membershipService)
{
this.membershipService = membershipService;
}
public bool UpdateMembership(MembershipUpdateModel updateModel)
{
if (updateModel is null)
return false;
//Below I need to control the dates if they are changed
if (false == ValidateUpdateModelDates(updateModel.StartDate, updateModel.EndDate))
return false;
return membershipService.UpdateMembership(updateModel);
}
public bool ValidateUpdateModelDates(DateTime stara tDate, DateTime endDate)
{
var now = DateTime.Now;
if (startDate >= endDate)
return false;
if (startDate <= now)
return false;
return true;
}
}
我的问题:当客户端在startDate值之后发送旧日期而没有任何更改时,我的ValidateUpdateModelDates方法r变为false。客户端只能发送没有日期的更改值,但更新模型会反序列化为DateTime.MinValue和ValidateUpdateModelDates再次返回false。我可以打数据库检查StartDate是否更改。但是我不确定这是否是解决这种情况的最佳方法。有什么方法可以知道客户端在反序列化数据时是否更改了数据?
谢谢。