I'm going to proceed to create the "Model" part of my MVVM architecture in Swift. And for this I am going to create the DTOs of each component/element of my application. I've never done it before so if I mess up in any way I hope they correct me.
For example, I have a component that displays a list of friends, with their first and last names. Here, would I have to perform two DTOs? I explain:
One for each friend, a "Friend" struct:
struct Amigo { var nombre: String, var apellidos: String }
And one for the list of friends:
struct listadoAmigos { var amigos: [Amigo] }
And how would I then connect them to the logical part in the viewmodel and use them?
Thanks in advance for the help.
Well, to begin with, I advise you to use the Decodable protocol for the DTO object, so that you can obtain the data from your JSON there is a lot of information about this type of decoding that is very simple and very good, and then you need to create a List of friends this object if it comes in a data in the json then you need to also create that object, for example... let's say that we are receiving our object in the following way:
{ "status": "Ok", "statusCode": 200 "data": [ { "name": "Albert", "lastName": "Einstein" }, { "name": "Fyodor", "lastName": "Dostoevsky" "birthday": "11/11/1821" } ] }
what our DTO will do is get the data from our json and then fill it in our structure to show it in the view then we create a method to fill it or pass it to our "Friends" structure
Why two almost equal structures? the DTO only gives us the data of our web service, or backend, and will be treating any change that occurs within what we receive, so we only register any data that changes or is "different or missing", for example the birthdate that in the first case is absent, and that is why we place it as optional, and in the Friends structure, there will always be the data that we need to use them in our ViewModel.
The viewModel can directly inject the data with an Init(friends: [Friends]) "this if you use one of the three forms of Dependency Injection", or create the request in the same viewModel and fill the data you need, as you like or as you see necessary. but in essence the DTO is to treat and maintain the data that we receive from the backend in a flexible way, if there is any change the DTO is in charge of receiving that data, if it thunders because I change a data, it is only to adjust the DTO and that's it, more Later you can adjust the structures that use them to fill the data, now for example our viewModel could be like this:
now with the init in your ViewController you can initialize the viewModel for example:
This is in case you already have the Friends data to call another's viewController... for example, let's say you would start it using the dto directly... in a request that when successful you go to that Controller....
if it's a bit cumbersome, and there are different methods to fill in the data, this is just a small example that may serve as a guide to what a DTO does. and how you can pass the data to your vieModel, either directly in it or from another class.