c# - 在某个确切时间采取行动的应用程序
问题描述
假设我有一个模型
public class SomeModel
{
public DateTime StartDate { get; set; }
public bool IsActive { get; set; }
}
我的问题是:当 StartDate 中确定的时间到来时,将属性 IsActive 更改为 true 的最佳做法是什么?我应该使用委托/事件还是计时器?我将不胜感激一些简短的例子。
关键是 IsActive 存储在数据库中,因此在更改时我希望模型在 db 中更新。
解决方案
不需要计时器,因为您不需要设置任何内容。只需编写一个计算IsActive
.
public class SomeModel
{
public DateTime StartDate { get; set; }
[NotMapped]
public bool IsActive { get; } => DateTime.UtcNow >= StartDate;
}
推荐阅读
- javascript - 如何在 Angular ace 编辑器中根据特定场景启用和禁用默认代码段关键字
- python - 使用 python 电子邮件库解析原始电子邮件添加不需要的字符
- python - 如何在不指定 excel 名称的情况下从文件夹中读取 excel?
- c# - 如何使用实体框架 LINQ 获取列的总和
- c# - 最长会话持续时间
- reactjs - 为什么在 React Select 的容器上添加 stopPropagation() 会完全停止 React Select 事件?
- android - 如果我在 edittext 中使用 android:digits 则单行限制不起作用
- r - 最佳实践:R 中的查找表
- python - RQ 作业超时
- node.js - Moxios 在 NodeJs 中模拟外部端点