C#C
C#2y ago
ibby

What should the service layer return back to a controller?

Confused about what the service layer should return to the controller, Dtos? Result object?

Is this the correct approach?

public async Task<CreateUserResult> CreateUserAsync(string username, string email, string password)
{
    ...input validation

    username = username.ToLower();
    email = email.ToLower();

    if (_dbContext.Users.Any(x => x.Username.Equals(username)))
    {
        return; // Return a result object with a bool flag "IsDuplicateUsername"?
    }

    if (_dbContext.Users.Any(x => x.Email.Equals(email)))
    {
        return; // Return a result object with a bool flag "IsDuplicateEmail"?
    }

    var user = new User(username, email, password.ToHash());

    await _dbContext.Users.AddAsync(user);

    await _dbContext.SaveChangesAsync();
    
    // return result with everything false?
}
Was this page helpful?