Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to debug an application that is time-dependent? (using a current DateTime variable instead of DateTime.Now)
    primarykey
    data
    text
    <p>I'm working on an application (a web application, asp.net and c#) which is datetime-dependent, so, based on the current date, it will launch forms for the logged user to fill in.</p> <p>I've been thinking about how we're going to simulate real usage of the application, for debugging and testing purposes.</p> <p>So I'm talking about replacing all those:</p> <pre><code>DateTime currentDate = DateTime.Now; </code></pre> <p>with something like:</p> <pre><code>DateTime currentDate = MyDateClass.GetCurrentDate(); </code></pre> <p>And then I'll have a class:</p> <pre><code>public class MyDateClass { private DateTime _currentDate; public DateTime GetCurrentDate() { // get the date, which may be different from DateTime.Now return _currentDate; } public void SetCurrentDate(DateTime newCurrentDate) { // set the date to the value chosen by the user _currentDate = newCurrentDate; } } </code></pre> <p>allowing me to set the current data, by invoking the SetCurrentDate method, for example, in the code-behind of a link button and a calendar input.</p> <p>Question is... how should I exactly store the DateTime variable, throughout all the application? I can't work with the session in this class, right? Should I work with the Thread?</p> <p>Well, ideas are appreciated :) Thanks in advance for your suggestions!</p> <hr/> <p>Some updating on my question:</p> <p>I ran into this post: <a href="https://stackoverflow.com/questions/43711/whats-a-good-way-to-overwrite-datetime-now-during-testing">What&#39;s a good way to overwrite DateTime.Now during testing?</a> </p> <p>Just like you provided here with your answers (thank you!), great tips for good code organization, for both development and testing purposes, that I will definitely be considering in the time to follow.</p> <p>I still have the same question though: how will I "hold" the datetime value? </p> <p>Right now, I went with creating a single cell table in the data base to maintain "my" datetime value.</p> <p>I have a static class with a GetCurrentDate and a SetCurrentDate function:</p> <pre><code>public static class DateManagement { public static DateTime GetCurrentDate() { // return DateTime.Now; // OR: // ... // SqlCommand cmd = new SqlCommand("select Date from CurrentDate", conn); // ... } public static void SetCurrentDate(DateTime newDate) // used only in debugging { // ... // string insertStr = @"update CurrentDate set Date = '" + date + "'"; // SqlCommand cmd = new SqlCommand(insertStr, conn); // ... } } </code></pre> <p>However, storing the date in the database, with a table created and used just for debugging, doesn't seem like an elegant solution...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload