0

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:

  1. Get a List of Articles only containing the most relevant attributes, e.g. id, title, price
  2. 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

Dominik
  • 2,801
  • 2
  • 33
  • 45
  • 2
    If you just want to not emit null values, you can put `[DataMember(EmitDefaultValue = false)]` on your properties. See [DataContractJsonSerializer to skip nodes with null values](http://stackoverflow.com/questions/13506630/datacontractjsonserializer-to-skip-nodes-with-null-values). – dbc Oct 18 '15 at 06:56
  • Thank you, I think that really fits my case the best, except there comes some overhead when changing the contracts. But well, it's wcf, so what should I do ;) IF you wnat, just makean answer out of this comment. – Dominik Oct 19 '15 at 06:40
  • I suggest just marking this as a duplicate then. – dbc Oct 19 '15 at 07:16

0 Answers0