-1

I am building an API which, amongst other things, needs to handle users. I am building it with C# 10 and .NET 6. In order to receive/return data in the API end points, I am using records. So for example, this could be an end point which returns all users:

[HttpGet]
public IActionsResult ListUsers()
{
    // The '.AsDto' method extracts the relevant information
    // from the domain model 'User' in the form of a record (dto).
    List<UserDto> users = _userRepo.GetAll().AsDto();
    return Ok()
}

public record UserDto(string Username, string FirstName, string LastName);

This works just fine and should be (I think) reasonably understandable. However, the UserRepository is using EntityFrameworkCore, to communicate with the database. So let's look at something like an AddUser() method on the repository:

internal void AddUser(UserModel user) 
{
    // The 'Validate' method validates the input and returns
    // the data in a EF Core friendly record (see below).
    UserDto userDto = Validate(user);
    _dbContext.Users.Add(userDto);
}

// This looks similar to the previous dto, but notice there also is an ID
public record UserDto(Guid Id, string UserName, string FirstName, string Lastname);

Now, suddenly, I have two almost identical DTO's/records. I could make them identical. This would solve the naming problem, but it doesn't feel right. After all, they serve different purposes.

I could also name them slightly differently like UserApiDto and UserContextDto, but this seems like a potential booby trap for future work with two so similar items.

This seems like a problem which other people should have encountered. Is there a best practice in regards to handling this "double dto" issue, when building API's with EF Core?

5
  • Use "User" for the EF class. I would refrain from using records with EF Core Commented Feb 5, 2022 at 15:30
  • whats the asDto() method and why are you using it? Commented Feb 5, 2022 at 15:37
  • i dont understand the question/problem. ef uses the same model to add/get, you create additional classes and then ask "why do i need additional classes?" Commented Feb 5, 2022 at 15:40
  • 1
    This question seems a roundabout way to ask why you need DTOs and not just entities; which has been asked and answered many times over both here and in plenty of other resources. Commented Feb 5, 2022 at 16:26
  • @Flater I think you might be on to something. The last time I used EF (pre-core) the standard seemed to be to used DTO's. That might have changed. You don't happen to know any good resources? Commented Feb 5, 2022 at 16:41

1 Answer 1

0

Decide first if you want to use DTOs and if that make sense to your purpose. You could use different DTOs depending on the context of the application but on this case you could reuse the same DTO which includes the id property, assuming that it could have default value when it has not being initialized(for example in an add request). Don’t see the problem on that :)

If the generated DTOs are aimed for totally different purpose and should have a totally different kind of properties you could start to think on splitting the original class in two or more types. It will make your life easier in the future

1
  • The dto for EF Core could include fields which are irrelevant to the dto for the API. An example could be something like CreatedAt (datetime) or UserRole (a key to a role table). So I think the question is not whether you can re-use the same dto, but rather if you can combine the model and ef-core dto (as suggested by @Flater in a comment to the original question). Commented Feb 5, 2022 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.