jeudi 30 août 2018

Best Practice to Get 1 object from Database using WebApi C# Layered Architecture

So I know my question seems basic but I want to know something that's been bugging me for a while, My backend is done following the Layered Architecture(repo-services-controllers)

I have an api call that should return a json of an employee after providing his id, so the url is something like api.mywebsite.com/api/employees/1

and my controller will look like this:

public async Task<EmployeeDto> GetEmployee([FromUri] int eId)
{
    return GetService<IEmployeeService>().GetEmployeeById(eId);
}

my question is, what are the checks I'm supposed to do when I get this object? Should I do a check if the employee is deleted (soft deleted that is)? I obviously should do a check if it returns a null (didnt find an employee with such an id)

But if I want to do a check if the entity is deleted, should I do it in the repository layer or the service layer? repo layer:

public Task<Employee> GetSingle(int id)
{
    return GetDatabase().Employees.Where(x => x.EmployeeId== id && !x.Deleted).SingleOrDefaultAsync();
}

or on the service layer:

var emp= await GetTenantRepository<IEmployeeRepository>().GetSingle(eId);
if (emp==null)
{
    throw ...
}
if (emp.Deleted)
{
    throw ...
}

Am I too overthinking it and it doesnt matter if I put it here or there?

Aucun commentaire:

Enregistrer un commentaire