Reputation: 8151
I have a fairly trivial WebAPI that returns a result, from another Webservice.
[HttpGet]
public List<MyList> GetResults()
{
var result = Webservice.GetResults().ToList();
return result;
}
The above returns all values from the Webservice call, and with the same prop names.
My question is, how do I get the data from the WebService, extend some of the data, with my own data, change some prop-names, and then return that new result? Without using Automapper or similar components?
[HttpGet]
public List<MyList> GetResults()
{
var result = Webservice.GetResults().ToList();
foreach (res in results) {
var newTitle = result.Title + " - my custom value";
var newData = "01/01/2019";
var newLink = GetCustomLink(result);
}
return newValues;
}
Upvotes: 0
Views: 546
Reputation: 3778
you can also use LINQ methods like :
[HttpGet]
public List<MyList> GetResults()
{
var result = Webservice.GetResults().ToList();
return results.Select(xx=> new MyList{ // ,-- HERE YOU CAN ALSO USE A GENERIC NEW
newTitle = result.Title + " - my custom value",
newData = "01/01/2019",
newLink = GetCustomLink(result)
}).ToList()
}
... then .. if this mapping is recurrent .. you can create an extension method to do this like:
public static MyList ToMyCustomType(this YOURWSTYPE input){
return new MyList{
newTitle = result.Title + " - my custom value",
newData = "01/01/2019",
newLink = GetCustomLink(result)
}
}
public static List<MyList> ToMyCustomType(this IEnumerable<YOURWSTYPE> input){
return input.Select(xx=> xx.ToMyCustomType()).ToList();
}
... and then you can use it like:
[HttpGet]
public List<MyList> GetResults()
{
return Webservice.GetResults().ToList().ToMyCustomType();
}
Hope it helps you!!
Upvotes: 1
Reputation: 3634
you can use Select extension of Linq to create custom result.
var result = Webservice.GetResults().ToList();
return result.Select( s => new MyList{
newTitle = result.Title + " - my custom value",
newData = "01/01/2019",
newLink = GetCustomLink(result)
});
Upvotes: 0
Reputation: 6335
what you are talking about is a common scenario in many applications / APIs.
if you are looking at the problem from an an engineering point of view then this is what I would do:
create a model which reflects the data coming back from the external API. Let's call that ApiSourceDataDto.
your API wants to take that data and transform it somehow and return a new format, let's call that MyApiDataDto.
create an extra layer, for now let's say a new class library where you add a translation layer from ApiSourceDataDto to ApiSourceDataDto. Let's say we have a class called TranslationLayer.
Call this class from your controller, using a functional approach.
What I mean by this is that your translation layer will not have any knowledge about the external API, all it knows is that it will receive some data in a specific format, it will transform it and return the result, nothing more. This makes it very easy to test because you can inject data any way you'd like and test what happens without having to mock anything.
Something like this:
[HttpGet]
public List<MyApiDataDto> GetResults()
{
List<ApiSourceDataDto> externalApiResults = Webservice.GetResults().ToList();
List<MyApiDataDto> myResults = new TranslationLayer().Translate(externalApiResults)
return myResults;
}
in a separate class library, let's say you have something like:
public class TranslationLayer{
public List<MyApiDataDto> Translate(List<ApiSourceDataDto> input)
{
//apply your business logic and transformations here.
//return the transformed data.
}
}
Now you have SOC (separation of concerns) and you can start doing some proper testing around your translation method(s). Your controller is simple and doesn't have much code, as it should be. You want thin not fat controllers.
Of course this is just to give you an initial idea. If you have a lot of translation methods then you need to do the separation in a way which makes sense for your application. Do keep things separate and any business logic outside of the controllers.
Of course you would also add some code to deal with errors returned by the external API.
Upvotes: 1