【问题标题】:Entity Framework - Should I edit an object in a function, or after a function completes实体框架 - 我应该在函数中编辑对象,还是在函数完成后编辑
【发布时间】:2015-04-03 16:37:53
【问题描述】:

我正在编写一个 MVC 5 互联网应用程序,我在其中检索许多需要发送电子邮件的 Account 对象,然后我发送电子邮件。发送电子邮件后,我需要更新每个 Account 对象中的 DateTime 字段以存储一个值以表明电子邮件已发送。

这是我的代码:

public async Task SendDailyExpirationEmails(int dayInterval)
{
    IEnumerable<Account> freeTrialAccounts = GetFreeTrialAccountsForSendDailyExpirationEmails(dayInterval).ToList();
    IEnumerable<Account> paidServiceAccounts = GetPaidServiceAccountsForSendDailyExpirationEmails(dayInterval).ToList();
    await SendFreeTrialSubscriptionExpirationEmails(freeTrialAccounts);
    await SendPaidSubscriptionExpirationEmails(paidServiceAccounts);
}

对于freeTrialAccountspaidServiceAccounts,SendEmail 函数使用ForEach Loop 循环遍历IEnumerable 中的每个Account

我的问题是这样的:

我应该在完成 SendEmail 函数之后还是在 SendEmail 函数中更新 DateTime 字段?

对于这种情况有通用的编码实践吗?

提前致谢。

【问题讨论】:

    标签: asynchronous asp.net-mvc-5 entity-framework-6 ienumerable updatemodel


    【解决方案1】:

    为了保持 DateTime 值的精确度,使其尽可能正确,同时减少数据库调用,您将希望在电子邮件发送后立即对其进行记录,但要等到您的电子邮件处理完成后才保留信息完成。

    我会说有一个类属性来跟踪每封电子邮件的发送时间,然后在所有电子邮件都发送完毕后,调用数据库以更新发送日期/时间。

    也就是说,如果您有其他工作/任务/应用程序尽快依赖该信息,那么您需要在发送电子邮件后立即保存数据。否则我看不出延迟它有什么问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-15
      • 2018-08-28
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      相关资源
      最近更新 更多