So, let's assume we have an Entity Article
which has a huge underlying data structure. We need this article on clientside for 2 Purposes:
- Get a List of Articles only containing the most relevant attributes, e.g. id, title, price
- Get a detailview of one article containing the whole data structure.
There's a WCF-Service method like this, producing a JSON Response:
Public List<Article> GetArticles(string anId, bool isMinimal)
{
using (var client = new MyApp.Model.MyAppEntities())
{
var articles = new List<Article>();
myObjects = //Linq-magic to get values from db...
if (!isMinimal)
{
//add a huge datastructure from other tables to each of myObjects
// these are vitamins, allergeens, nutrients and so on for some article.
});
}
return articles;
}
}
So, when isMinimal is true, the whole underlying datastructure does not get filled. But (somewhat correctly, as the method signature shows it), the whole List<Article>
is returned, with null-values for the attributes not filled.
What I want to achieve is: Let this method return only the filled values, when isMinimal is true, and "cut out the rest from the json returned". I thought about creating a "general json response" (a string?) and then let it return the desired values, so changing the Methods signature to something like this:
Public List<JSONresponse> GetArticles(string anId, bool isMinimal)
{
using (var client = new MyApp.Model.MyAppEntities())
{
var articles = new List<JSON>();
myObjects = getMyObjectsFromDb();//Linq-magic to get values from db...
//adding the whole datastructe here now...
var articleJSON = articles.toJSON();
if (isMinimal)
{
articleJSON = articleJSON.Select(s => new JSON(){ title: s.title, price: s.price, Id: s.Id});
}
return articleJSON;
}
}
But to be honest, I have no idea if this is generally possible and so I struggle very much with this.
Because of using the wcfservices in many of my applications, i would prefer not to create another method with another contract for an Object(DTO) like ArticleMinimal
, because there would be some overhead involved updating the service references everywhere.
Would be great if someone could help me out here!
Best Regards, Dominik