1

So, I started a little pet project, just to have something to work on, but since last time I worked comercially I learnt some things about patterns and so on. So when I stumbled upon having to convert simple DTO to also simple model that holds its values in program I started to wonder - what would be best practice to convert it?

To calrify - I have something like this:

public class LoggedInDTO
{
   public String Token;
   public String Name;
}

And I want it to be converted to LoggedInUser like below:

public class LoggedInUser
{
   private String _token;
   private String _name;
}

(of course Logged in user will be a little bigger than that, the point is to not use DTO for anything other than transfer, and to have more specialized classes) Now, how to properly convert one to another to achieve best clarity and/or testability? I thought of just passing DTO to constructor and then just copying values, but my concern is that it will be "tightly coupled", which is thing that all websites are telling as horror story of programming. From the other side, one is the base of another, so why not? Or maybe converter to have everything in one place?

What would be the best practice for that? Or maybe I am overthinking everything and should not concern myself with such?

Adam Kaczmarski
  • 338
  • 1
  • 12
  • If one is the base class of the other, why do you have those attributes twice at all? – akuzminykh Oct 03 '20 at 09:59
  • Maybe base is wrong word, and example too simple. If I would have some DTO that gets the info from server in raw form (cuz API works that way or smth) and in app I would use different class, that holds those values in specific type with a lot of tweaks to the values (that I need), that would be more representative example. – Adam Kaczmarski Oct 03 '20 at 10:12
  • Okay, then check out posts like this: [Conversion of DTO to entity and vice-versa](https://stackoverflow.com/q/28703401/12323248). However, they are not talking about *best practices*. I don't think you should care to much for something like this. The constructor idea is fine, as long the DTO stays as it is. It's not "tightly coupled" when the problem is about converting something to another form of itself. – akuzminykh Oct 03 '20 at 10:27
  • There are sophisticated tools like Dozer, that map complex Java data structures from one class to another. It's a fairly common problem with, I suspect, no single perfect solution. Personally, I wouldn't worry too much about doing the mapping in explicit code, unless the classes were very complex, or there were many of them. – Kevin Boone Oct 03 '20 at 11:51

1 Answers1

-1

LoggedInDTO:

public class LoggedInDTO
{
    public String Token;
    public String Name;

    public LoggedInUser toLoggedInUser() {
        return new LoggedInUser(Token, Name);
    }
}

LoggedInUser:

public class LoggedInUser {

    public LoggedInUser(String token, String name) {
        _token = token;
        _name = name;
    }

    private String _token;
    private String _name;
}

Example:

public static void main(String[] args) {
        LoggedInDTO loggedInDTO = new LoggedInDTO();
        loggedInDTO.Name = "12eq";
        loggedInDTO.Token = "123234";

        LoggedInUser loggedInUser = loggedInDTO.toLoggedInUser();
}
  • This is very similar to what the OP already has mentioned in their post. You don't answer the actual question, which is about a *best practice*. Just showing some code won't explain anything. – akuzminykh Oct 03 '20 at 10:08