© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
40 replies
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?
}
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?
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ What should web api controller return?
C#CC# / help
4y ago
What should 'Service Layer' do? (MVC; layered architecture)
C#CC# / help
4y ago
Controller-Service architecture. What is service?
C#CC# / help
3y ago
❔ How to properly handle exceptions between API controller and service layer?
C#CC# / help
3y ago