Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:46:42+00:00 2026-05-27T09:46:42+00:00

I really like this suggestion on StackOverflow to use an IClock interface to supply

  • 0

I really like this suggestion on StackOverflow to use an IClock interface to supply your code with the current date/time so you can supply a different date/time when unit testing (not to mention factoring out all of the DateTime.Now references).

I can use dependency injection to supply an implementation of this interface to my service layer.

However, I have alot of DateTime.Now references in my entities (sample below). What is the preferred way of addressing this?

public class SampleEntity
{
  private DateTime someField;
  private DateTime someOtherDate;

  public SampleEntity()
  {
    someField = DateTime.Now;
  }

  public bool SomeProperty
  {
    get { return someOtherDate < DateTime.Now.Date; }
  }

  public bool SomeFunction()
  {
    return SomeOtherDate < DateTime.Now.Date;
  }
}

I can pass in parameters to the function and/or constructor, but that still requires me to explicitly set something if I retrieve the entity from an ORM.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:46:42+00:00Added an answer on May 27, 2026 at 9:46 am

    If you want to use IClock, you will have to replace all DateTime.Now with Clock.Now and have your class take an IClock instance in the constructor:

    public class SampleEntity
    {
        private DateTime someField;
        private DateTime someOtherDate;
        private readonly IClock _clock;
    
        public SampleEntity(IClock clock)
        {
            _clock = clock;
            someField = clock.Now;
        }
    
        public bool SomeProperty
        {
            get { return someOtherDate < _clock.Now.Date; }
        }
    
        public bool SomeFunction()
        {
            return SomeOtherDate < _clock.Now.Date;
        }
    }
    

    As far as passing the concrete implementation of an IClock when instantiating the entity most ORM frameworks provide hooks that could be used to provide custom instances of the entities.

    If your ORM doesn’t provide such hooks you could still use the accepted answer from the linked question which uses a public static Func<DateTime> Now = () => DateTime.Now; provider.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a silly question but I would really like your comments and
This is a really basic question but... I have some code like this var
Can anyone explain why using comments like this in css cause really weird effects
Can/should One use any ORM Solution is a Complex Situation like this ? can
I would really like to do something like this: Callable<MyObject> myCallable = .... Future<MyObject>
i want to fetch information from the database using objects. i really like this
I'd really like to handle this without monkey-patching but I haven't been able to
So I rewrote my paths to something like: URL/really/nice/paths/ using mod_rewrite rules like this:
I feel like this might be really simple but I'm just not getting it
So I have this program that I really like, and it doesn't support Applescript.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.