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 8718819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:38:25+00:00 2026-06-13T06:38:25+00:00

I use Entity Framework and want to use DDD principles. However, there are some

  • 0

I use Entity Framework and want to use DDD principles. However, there are some information regarding the entities that is on the borderline between what is logging/persistence information and what is information about the domain objects.

I my situation these are put in an abstract base class that all entities inherit from:

 public abstract class BaseEntity: IBaseEntity
{

    /// <summary>
    /// The unique identifier
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// The user that created this instance
    /// </summary>
    public User CreatedBy { get; set; }

    /// <summary>
    /// The date and time the object was created
    /// </summary>
    public DateTime CreatedDate { get; set; }

    /// <summary>
    /// Which user was the last one to change this object
    /// </summary>
    public User LastChangedBy { get; set; }

    /// <summary>
    /// When was the object last changed
    /// </summary>
    public DateTime LastChangedDate { get; set; }

    /// <summary>
    /// This is the status of the entity. See EntityStatus documentation for more information.
    /// </summary>
    public EntityStatus EntityStatus { get; set; }

    /// <summary>
    /// Sets the default value for a new object
    /// </summary>
    protected BaseEntity()
    {
        CreatedDate = DateTime.Now;
        EntityStatus = EntityStatus.Active;
        LastChangedDate = DateTime.Now;
    }

}

Now a Domain Object can’t be instantiated without providing the date and time. However, I feel it is the wrong place to put it. I can argue for both really. Maybe it should not be mixed with the domain at all?

Since I’m using EF Code First it makes sense to put it there, or else I would need to create new classes that inherit from the base class in the DAL also, duplicating code and needing to map to both domain objects and MVC models which does seem more messy than the approach above.

The question(s):

Is it Ok to use DateTime.Now in the Domain model at all? Where do you put this kind of information using DDD and EF Code First? Should User to be set in the domain object or require it in the Business Layer?

Update

I think jgauffin har the right answer here – but it is really quite a fundamental change. However, on my search for an alternate solution I almost had it solved with this. I used the ChangeTracker.Entries to find ut if an entity is added or modified and set the fields accordingly. This is done in my UnitOfWork Save() method.

The problem is loading navigation properties, like User (DateTime is set correctly). It might be since the user is a property on the abstract base class the entity inherits from. I also don’t like putting strings in there, however it might solve some simple scenarios for someone, so I post the solution here:

        public void SaveChanges(User changedBy)
    {
        foreach (var entry in _context.ChangeTracker.Entries<BaseEntity>())
        {
            if (entry.State == EntityState.Added)
            {
                entry.Entity.CreatedDate = DateTime.Now;
                entry.Entity.LastChangedDate = DateTime.Now;
                entry.Entity.CreatedBy = changedBy;
                entry.Entity.LastChangedBy = changedBy;
            }
            if (entry.State == EntityState.Modified)
            {
                entry.Entity.CreatedDate = entry.OriginalValues.GetValue<DateTime("CreatedDate");
                entry.Entity.CreatedBy = entry.OriginalValues.GetValue<User>("CreatedBy");
                entry.Entity.LastChangedDate = DateTime.Now;
                entry.Entity.LastChangedBy = changedBy;
            }
        }


        _context.SaveChanges();
    }
  • 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-06-13T06:38:26+00:00Added an answer on June 13, 2026 at 6:38 am

    Is it Ok to use DateTime.Now in the Domain model at all?

    Yes.

    Where do you put this kind of information using DDD and EF Code First? Should User to be set in the domain object or require it in the Business Layer?

    Well. First of all: A DDD model is always in a valid state. That’s impossible with public setters. In DDD you work with the models using methods since the methods can make sure that all required information has been specified and is valid.

    For instance, if you can mark an item as completed it’s likely that the UpdatedAt date should be changed too. If you let the calling code make sure of that it’s likely that it will be forgotten somewhere. Instead you should have something like:

    public class MyDomainModel
    {
        public void MarkAsCompleted(User completedBy)
        {
            if (completedBy == null) throw new ArgumentNullException("completedBy");
            State = MyState.Completed;
            UpdatedAt = DateTime.Now;
            CompletedAt = DateTime.Now;
            CompletedBy = completedBy;
        }
    }
    

    Read my blog post about that approach: http://blog.gauffin.org/2012/06/protect-your-data/

    Update

    How to make shure that noone changes the “CreatedBy” and “CreatedDate” later on

    I usually have two constructors for the models which also fits the DB. one protected one which can be used by my persistance layer and one which requires the mandatory fields. Put the createdby in that constructor and set the createdate in it:

    public class YourModel
    {
        public YourModel(User createdBy)
        {
            CreatedDate = DateTime.Now;
            CreatedBy = createdby;
        }
    
        // for persistance
        protected YourModel()
        {}
    }
    

    Then have private setters for those fields.

    I get a lot of R# warning “Virtual member call in constructor”, I’ve read about it before and it is not supposed to be a good practice.

    That’s usually not a problem. Read here: Virtual member call in a constructor

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

Sidebar

Related Questions

I use Entity Framework 4.2 and want to call a stored procedure that has
I have some code that I use with Entity Framework like class Person{ pubic
I want to use Entity Framework(with Self Track Entities) in my winform application; but
I use Entity Framework code-first. DataBaseName - I want to create a database in
In a silverlight applicaiton i use entity framework as Data Access Layer. I want
I use Entity Framework 4.0. Is it possible that SaveChanges() returns 0 but doesn't
My question here is why should i want use the entity framework instead of
i'm using MS northwind database, and use Entity Framework. I want to create new
I want to use Entity Framework for my data access on a project I'm
I want to use Entity Framework to a load a number of sql tables

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.