C#C
C#2y ago
80 replies
Cyclomatic

Model, Dto, Entity - Id?

Hi,

I'm messing with a simple CRUD application.

My PatientEntity looks like this:

public class PatientEntity
{
public int Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}

And my model looks like this (no id):

public class Patient
{
public string FirstName { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}

And my dto looks like this (no id):

public class PatientDto
{
public string FirstName { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}


And currently a method in my DatabaseService looks like this:

public async Task<Patient> GetPatientById(int id)
{
PatientEntity patientEntity = await _context.Patients.FindAsync(id);
if (patientEntity != null)
{
return patientEntity.ToPatient(); // i.e. returning the Patient model
}
_logger.LogInformation("Patient not found in the database.");
return null;
}

Should my DatabaseService return a PatientEntity i.e. with an id, and my PatientDto contain the Id? My PatientDto is returned by a web api controller.

I feel like my PatientDto should contain the Id. I guess the problem is that the DatabaseService is returning a Patient (Model) rather than the PatientEntity. Does it make sense to return the model from the Database service?

Here's the git:

https://github.com/billymaat/MedTrackDash
GitHub
Contribute to billymaat/MedTrackDash development by creating an account on GitHub.
GitHub - billymaat/MedTrackDash
Was this page helpful?