0

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.

Vinicius Gonçalves
  • 2,514
  • 1
  • 29
  • 54
  • Is this property mapped to your User Entity? – Marc Cals Jun 11 '15 at 17:58
  • 1
    Nothin related to your question but, I would change `Check.ValidId(id)` with concrete `UserId` and `CustomerId` VOs or a unified `EntityId` VO. Also, `DeactivatedData` looks a weird modeling concept to me. Why not something like `user.activationStatus = new InactiveStatus(date)` or even `user.status = new InactiveStatus(date)`. Would you really have a business expert saying, when the `DeactivateData` of a `User/Customer`.... – plalx Jun 12 '15 at 17:18

1 Answers1

3

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.

Marc Cals
  • 2,963
  • 4
  • 30
  • 48
  • Awesome, I agree with you Marc, but that would be a new call to the database and it makes me uncomfortable. – Vinicius Gonçalves Jun 11 '15 at 18:28
  • Why is gonna be a new call to Database? You can do it at the same time that you set user to Deactivated customer, or have you Deactivated column user and DeactivatedDateTime in separate tables? – Marc Cals Jun 11 '15 at 18:30
  • DeactivatedUserId and DeactivatedDateTime in the same table. I'm using Repository Pattern. I've a complex type that have DeactivatedUserId and DeactivatedDateTime. – Vinicius Gonçalves Jun 11 '15 at 18:32
  • If I understand you well with your model changing these two properties at the same time is only necessary one call to Databases, using DBContext SaveChanges, Entity Framework in the same update sentence will update the two columns in your database. – Marc Cals Jun 11 '15 at 18:35
  • Looking at your code I think that the two columns will be updated just with, one database call. Check it with Sql Server Profiler if it's done with just one call – Marc Cals Jun 11 '15 at 18:40
  • dateTimeProvider is implemented on my infraestructure layer, with a call from entity framework to the database to get GETDATE() – Vinicius Gonçalves Jun 11 '15 at 18:42
  • I thought that you were retrieving the from the user machine, Why you need to retrieve the date from the SqlServer? Can't you get from user machine? – Marc Cals Jun 11 '15 at 18:44
  • User can change the local machine datetime to a corrupt state. – Vinicius Gonçalves Jun 11 '15 at 18:44
  • Should i take a datetime from database or FORCE a correct date and time on the local machine? – Vinicius Gonçalves Jun 11 '15 at 18:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80312/discussion-between-marc-cals-and-vinicius-goncalves). – Marc Cals Jun 11 '15 at 18:45
  • Consider removing "in different timezones" by changing last sentence to "If you have users remember to save the DateTime in UTC". There is absolutely no reason to store date time values as local. – Alexei Levenkov Jun 11 '15 at 19:03
  • @AlexeiLevenkov Even if you know that your users will be always in the same timezone, for example an application for a local community. Do you recommend to save in UTC? If yes, why? Thanks – Marc Cals Jun 12 '15 at 08:40
  • @MarcCals Yes, I strongly believe that storing UTC (or time + offset as full ISO8601 format) is significantly easier to deal with. Clearly this is opinionated suggestion (http://stackoverflow.com/questions/11537106/is-it-always-a-good-idea-to-store-time-in-utc-or-is-this-the-case-where-storing). If you live in country with frequently adjusted daylight saving rules or more than one timezone (I mostly mean "not in China" with one single timezone) it is really hard to properly track what "local time" means. – Alexei Levenkov Jun 12 '15 at 15:34