简体   繁体   English

我应该从数据库中获取DateTime.Now吗?

[英]Should i take DateTime.Now from Database?

I've a Windows Forms application and one of my business rules is when i deactivate a customer a DeactivationDateTime is defined with the current date and time. 我有一个Windows Forms应用程序,我的业务规则之一是当我停用客户时,会用当前日期和时间定义DeactivationDateTime。

I'm using Entity Framework and Domain Driven Design. 我正在使用实体框架和域驱动设计。

Should i take a datetime from database or force a correct date and time on the local machine? 我应该从数据库中获取日期时间还是在本地计算机上强制使用正确的日期和时间?

public void DeactivateCustomer(int customerId, int userId)
{
        Check.ValidId(customerId);
        Check.ValidId(userId);

        var u = userRepository.GetById(userId);
        if (u == null)
            throw new EntityNotFoundException(userId);

        var c = customerRepository.GetById(id);
        if (c == null)
            throw new EntityNotFoundException(id);

        c.DeactivateData = new DeactivateData(userId, dateTimeProvider.Now);
        customerRepository.SaveOrUpdate(c);
}

dateTimeProvider is an Infrastructure interface the is injected by constructor. dateTimeProvider是一个基础结构接口,由构造函数注入。

If the entity it's mapped in your object for me it's more natural to do it from Your app code in your business layer getting the date from user machine. 如果实体是为我映射的对象,那么从业务层中的应用程序代码中获取用户计算机的日期就更自然了。 If not if I'm a new developer working on this code It would be difficult for me discover where DeactivationDateTime is filled. 如果不是,如果我是使用此代码的新开发人员,那么对我来说很难发现DeactivationDateTime的填充位置。 I assume that you can trust that you users are not going to change your datetime. 我假设您可以相信您的用户不会更改日期时间。

If you haver users remember to save the DateTime in UTC format or with TimeZone information. 如果您有用户,请记住以UTC格式或带有TimeZone信息保存DateTime。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM