c# - How to set private fields on a domain model in the repository -


I am currently working with a codebase that uses anemic domain model, and I have more logic Attempting to move to the domain model in a step towards domain model and domain driven design, but I'm struggling with the following problem.

I have a working model model that looks like this,

  public class jobs (private datetime / somewared; Private date time updated; // - Different services can be called Public Zero SetLastUpdated () {_lastUpdated = DateTime.UtcNow;}}  

In some time, during the processing of a job, the date of last updated job I want to set it to that specific point. Public Setter is made as you can see above

An issue arises when I am pulling jobs back from the database in my repository, because now I have no public setter for this field because Can I please give this advice to me SetLastUpdated () .

Can someone please advise me how can I set this property in the repository implementation while getting the workplace, but from the service No, where it is restricted for calling SetLastUpdated () .

update 1) I updated the question using the start date It was a bad example.

Update 2) With the given answers, I can see that the only way this is being done is not using Automapers in the repository, for the _lastUpdated setting By adding a constructor to the job class, and using the repository's job retrieval method.

As I see, you have several options.

Option 1

Assuming that your repository has two modes:

  public IEnumerable & lt; Job & gt; ReadAll () {...} Public int CreateJob {Job} {...}  

You can give the class two constructors, whatever it takes Is a date time and which is not

  public sector job (public job (date time start date) {this.start.date = start date;} public job (): It does not prevent the service from calling "wrong" constructor, but at least (datetime.utcno) {} public datetime startdate {received; private set;}}  

Less call this Converts the option to call without startdate .

Option 2

Two different jobs < / Code> Work with classes.

Your repository may look like this instead:

  Public IEnumerable  ReadAll () {. ..} Public int CreateJob (NewJob newJob) {...}  

and NewJob class can look like this:

  Public Sector NewJob {Public News ()} {this.StartDate = DateTime.UtcNow; } Public DataTime StartDate {Received; Private set; }}  

This is a better intention, because the build method of the repository accepts only an event of NewJob so that the model User NewJob instead of Job .

Option 3

must be compelled to create the repository in StartDate Code> method and always set it to dateTime.UtcNow within the method. Or even trigger a database to create a insert .


Comments