Say I have an entity as follows:
Restaurant
- Id : int
- Name : string
- CreatedOn : datetime (when it was added to the DB)
and I have a RestaurantService and RestaurantRepository classes. Both of these classes have an AddRestaurant method. The RestaurantService class does some work on the entities and then passes the entities to the repository for persistance.
Which of the two classes should be in charge of setting the CreatedOn property?
Should the repository be in charge of setting the CreatedOn property or should that be part of the Service class responsibility?
I would personally put it as close to the database as possible – including actually in the database, if that meets your needs. Otherwise, though, wherever is closest to the point where it is actually persisted, because that’s what is meant by
CreatedOnin the first place.