Sorry about the title, I couldn't figure out a suitable description.
I have four layers:
- Core Layer : Contains DTO's, interfaces for services and repositories.
- Business Layer : Contains "services" that handle business logic.
- Data Access Layer : Contains repositories that handle database access and conversion of entities to DTO's.
- Presentation Layer : UI stuff
I've run into an issue that I don't know how to resolve best. I am asynchronously adding an entity to the database, like so:
// The AdministrationRate has an ID property, Entity Framework does treat the property as an Identity and does increment it.
var adminRate = new AdministrationRate() {FosterChildID = fosterChild.ID};
await adminRateService.AddAdministrationRate(adminRate);
AdministrationRateService:
public async Task AddAdministrationRate(AdministrationRate administrationRate) => await repo.AddAdministrationRate(administrationRate);
AdministrationRateRepository:
//I use a generic repository to avoid code repition.
//Notice the DTO to entity conversion. ID is not set in the conversion.
public async Task AddAdministrationRate(AdministrationRate administrationRate) => await Add(administrationRate.ToEntity());
Repository:
public async Task Add(TEntity entity)
{
using (var db = new DatabaseContext())
{
db.Entry(entity).State = EntityState.Added;
await db.SaveChangesAsync();
}
}
And that does work, the problem is that after it has been added the newly generated id of the entity has not been reflected in the DTO (DTO.ID = 0).
I can't possibly return the updated entity because it would require me to convert the entity to a DTO in my presentation layer to keep it async all the way.
I also can't return just the ID because of the generic repository.
I really don't want to get rid of the generic repository as it is quite useful, but I can't really see another way to do it securely without altering the database (which I can't do).
So, what do I do?