.Net DateTime Precision

in my .net domain object I track every state transition. This is done by putting the estate in a state history collection. Thus, later you can see a list sorted in order to find out what , the state has been changed in that .

So there is a way:

private void SetState(RequestState state)
{
    var stateHistoryItem = new RequestStateHistoryItem(state, this);

    stateHistoryItems.Add(stateHistoryItem);
}

When you create a new RequestStateHistoryItemcurrent date is automatically assigned. Like this:

protected IdentificationRequestStateHistoryItem()
{
    timestamp = EntityTimestamp.New();
}

An object EntityTimestampis an object that contains a suitable user and is created and modified.

When listing state history, I do a decreasing order with Linq:

public virtual IEnumerable<RequestStateHistoryItem> StateHistoryItems
{
    get { return stateHistoryItems.OrderByDescending(s => s.Timestamp.CreatedOn.Ticks); }
}

, Request, SetState(RequestState.Received) Received. , - , Started. ( db) Finished.

, , Received Started. System.Threading.Thread.Sleep(1000) Started . , , Started CreatedOn - OLDER, Received ?!

TimeOfDay   {17:04:42.9430318} FINSHED
Ticks   634019366829430318

TimeOfDay   {17:04:39.5376207} RECEICED
Ticks   634019366795376207

TimeOfDay   {17:04:39.5367815} STARTED
Ticks   634019366795367815

? , , , ?

new DateTimePrecise().Now, (. DateTimePrecise) . .

- , ?

public virtual bool Finish()
{
    // when I put the SetState(State.Received) from the constructor into here, the timestamp of finish still is BEFORE received
    SetState(IdentificationRequestState.Received); 
    SetState(IdentificationRequestState.Finished);        

    // when I put the SetState(State.Received) after Finished, then the Received timestamp is BEFORE Finished  
    SetState(IdentificationRequestState.Finished);        
    SetState(IdentificationRequestState.Received); 

    var match = ...

    if (match != null)
    {
        ...
    }
    else
    {
        ...
    }
}
+3
3

DateTime.Now . , 30 15 ( , , IIRC).

System.Diagnostics.Stopwatch - . UTC .. DateTimePrecise DateTime , , DateTime.Now .

+2

, .

- , .

, , - :

lock(syncLock)
{
    // Timestamp is generated here...
    var stateHistoryItem = new RequestStateHistoryItem(state, this);
    // ... but an indeterminate time can pass before ...
    ...
    // ... it added to the collection here.
    stateHistoryItems.Add(stateHistoryItem);
}
+2

Received Started (.. Received , Started?).

, , , . .NET , , .

0
source

Source: https://habr.com/ru/post/1733033/


All Articles