I have an application architectured by: View -> ViewModel -> Repository -> Datasource.
My DataSource is consuming a webservice and then receiving a Soap object, which I want to transform to a custom Pojo object. So, by using RxJava I am calling the DataSource method by following this flow:
ViewModel
repository.webserviceCall(data)...
.subscribe();
Repository
public Single<SoapObject> webserviceCall(String data) {
return dataSource.webserviceCall(data);
}
Datasource
public Single<SoapObject> webserviceCall(String data) {
WSSoapDAO soapDAO = new WSSoapDAO("webserviceMethodName");
soapDAO.addProperty("data", data);
return soapDAO.call();
}
and then I would like to know where should I transform the SoapObject received in the DataSource call, either in the Repository class, in the ViewModel class or in thee DataSource class itself?
In addition to Onik's answer, I'd like to expand it with a few things.
This is close to opinion based facts, so be careful where you walk. :)
I agree this is normally a Repository problem (there's a "but"). In general, if the data you're receiving from your remote service needs to be transformed, then you want to abstract this away from the consumers of said data (A ViewModel, and even your local database).
If the data is to be differently transformed for different usages, then perhaps you need to re-think your architecture (or rather, your remote services!); in any case, the viewModel should only do (in my opinion) things related to modifying your local domain objects/models/entities into something different for the purpose of the views that need it. In other words, if a specific "screen" needs to transform the data (for e.g. to flatten a few models into a single List<Things>
to simplify a Recyclerview, then that could belong on a ViewModel, assuming you inject a "mapper" object (transformer, mapper, whateverDelegate, youNameIt) so the ViewModel delegates this transformation (and therefore simplifies testing).
In any case, keep in mind this is very opinion based, and domain specific, but in the end, you want a clean repository that gives you data, and data for which you don't want to think (or know) where it came from, other than it's in the format you care, every time.
In terms of your architecture I'd say the transformation should go to the repository. Let's call the entity that is to do the transformation Mapper. The mapper is supposed to transform a DTO object, SoapObject, to a model object, PojoObject.
For example
public interface Mapper {
public PojoObject map(SoapObject dto)
}
Now the presentation layer (View and ViewModel) knows nothing about DTO objects exposed by the Datasources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With