Should i take DateTime.Now from Database?

145 views Asked by At

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.

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.

1

There are 1 answers

13
Marc Cals On BEST ANSWER

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. 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.