Quantcast
Channel: User Álvaro García - Software Engineering Stack Exchange
Viewing all articles
Browse latest Browse all 40

How to design this case in DDD (domain-driven design)?

$
0
0

I have an employee that has to record the time when it starts to work and the time when it finishes.

This records can be associated with a proyect or not. Project could be null.

The proyect has an ID and a proyect number.

The employee gives the proyect number, not the ID of the proyect.

So I was thinking in this code:

//Application servicepublic class AddApplicationService(){    public static async Task<Record> AddStartRecord(long paramEmployeeId, long? paramProjectNumber)    {        //I get the employee, it is which has the business logic        Employee myEmployee = await _employeeRepository.GetEmployeeAsync(paramEmployeeId);        //I get the project from the project number, to know the ID        Project? myProject = await _employeeRepository.GetProjectAsync(paramProjectNumber);        //The employee create the new record. It adds to its records collection.        Record myNewRecord = myEmployee.CreateRecord(myProject.Id);        //The repository creates the new record because it is a new entity in the collection of the employee.        await _employeeRepository.SaveChangesAsync();    }}public class Employee : Entity, IAggregateRoot{    public Employee()    {        Id = 0;    }    public long Id {get; private set; }    public ICollection<Record> Records { get; private set; } = new HashSet<Record>();    public Record CreateRecord(long? paramProjectId)    {        Record myRecord = new Record(DateTime.Now, paramProjectId);        this.Records.Add(myRecord);        return myRecord;    }}public class Record : Entity{    public Record(DateTime paramRecordTime, long? paramProjectId)    {        Id = 0;        RecordTime = paramRecordTime;        ProjectId = paramProjectId;    }    public long Id {get; private set; }    public DateTime RecordTime { get; private set; }    public long? ProjectId { get; private set; }}public class Project : Entity : IAggregateRoot{    public long Id {get; private set; }    public long Number {get; private set; }}

One of my doubts is about the project entity.

If I am not wrong, it has to be an entity, not a value object, because it has an ID.

But this entity has not business logic, I use it only to get the Id of the project to can be assigned to the record. This makes me think that it is an anemic model. But if not, how to get the Id of the project from the project number given by the user?

I have this problem or consideration because a rule of the domain is that a project doesn't get a number until it is aproved, so it can't be the identitfier of the entity, I have to use an ID, that in the databse is a surrogate key, an autonumeric bigint.

I comment this relation with the database to try to be clear, but I know the domain shouldn't be affected by the persistence, but as I told, in the domain there is a rule that tells the project doesn't get a number until it is aproved.

Also it seems a bit odd to have the entity project, because it is not used as property in the record entity, it uses the id of the project, because one of the common advises in DDD is not to point to the instances of another root entites, but uses the id instead.

There is better ways to design this? I am sure yes, because I am starting with DDD, and I I have the general concepts, but when I have to design a case, it is when I get the doubts.

Thanks so much.


Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>