{"action":"created","issue":{"url":"https://api.github.com/repos/aL3891/CustomPackageReferences/issues/1","repository_url":"https://api.github.com/repos/aL3891/CustomPackageReferences","labels_url":"https://api.github.com/repos/aL3891/CustomPackageReferences/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/aL3891/CustomPackageReferences/issues/1/comments","events_url":"https://api.github.com/repos/aL3891/CustomPackageReferences/issues/1/events","html_url":"https://github.com/aL3891/CustomPackageReferences/issues/1","id":193734429,"number":1,"title":"Consider Using Surrogates (POCOs) Rather Than DOM Elements","user":{"login":"Mike-EEE","id":2931376,"avatar_url":"https://avatars.githubusercontent.com/u/2931376?v=3","gravatar_id":"","url":"https://api.github.com/users/Mike-EEE","html_url":"https://github.com/Mike-EEE","followers_url":"https://api.github.com/users/Mike-EEE/followers","following_url":"https://api.github.com/users/Mike-EEE/following{/other_user}","gists_url":"https://api.github.com/users/Mike-EEE/gists{/gist_id}","starred_url":"https://api.github.com/users/Mike-EEE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mike-EEE/subscriptions","organizations_url":"https://api.github.com/users/Mike-EEE/orgs","repos_url":"https://api.github.com/users/Mike-EEE/repos","events_url":"https://api.github.com/users/Mike-EEE/events{/privacy}","received_events_url":"https://api.github.com/users/Mike-EEE/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-12-06T10:24:31Z","updated_at":"2016-12-07T12:59:57Z","closed_at":null,"body":"This is a widespread problem and needs thorough ecosystem-wide correction.  :P \r\n\r\nI see a lot of this:\r\n[Example.](https://github.com/aL3891/CustomPackageReferences/blob/master/JsonPackageReference/JsonPackageReferenceTask.cs#L25-L32) \r\n\r\nThis is incredibly labor-intensive and error-prone.  Please consider a surrogate (or rather, POCO 😄) design instead:\r\n\r\n```\r\n[Fact]\r\npublic void BetterSerializationTrustMe()\r\n{\r\n\tvar file = File.ReadAllText(@\"C:\\path\\to\\file.txt\");\r\n\tvar definition = JsonConvert.DeserializeObject<ProjectDefinition>(file);\r\n\tforeach (var reference in definition.References)\r\n\t{\r\n\t\t// do stuff.\r\n\t}\r\n}\r\n\r\nclass ProjectDefinition\r\n{\r\n\t[JsonProperty(\"dependencies\")]\r\n\tpublic References References { get; set; } = new References();\r\n}\r\n\r\n[JsonArray(\"dependencies\")]\r\nclass References : Collection<PackageReference> {}\r\n\r\nclass PackageReference\r\n{\r\n\t[JsonProperty(\"name\")]\r\n\tpublic string Name { get; set; }\r\n\r\n\t[JsonProperty(\"version\")]\r\n\tpublic Version Version { get; set; }\r\n}\r\n```\r\n\r\nThis is super example code.  I did not run this, but made it to get the point across.  You will notice there is no linq statement as all of that is handled in `JsonConvert.DeserializeObject` for you.  \r\n\r\nThis can be made even better by generalizing the surrogates and decoupling JSON.NET-specific attributes (making using of the [`JsonSerializerSettings`](https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/JsonSerializerSettings.cs)) class.\r\n\r\nThis is why I am so hardup on POCO.  As you do not have to dick around for a schema and dive into LINQ land to translate your data to the target constructs in your code.  You use one line (`JsonConvert.DeserializeObject`) and boom! you're done. 😛 "},"comment":{"url":"https://api.github.com/repos/aL3891/CustomPackageReferences/issues/comments/265441066","html_url":"https://github.com/aL3891/CustomPackageReferences/issues/1#issuecomment-265441066","issue_url":"https://api.github.com/repos/aL3891/CustomPackageReferences/issues/1","id":265441066,"user":{"login":"Mike-EEE","id":2931376,"avatar_url":"https://avatars.githubusercontent.com/u/2931376?v=3","gravatar_id":"","url":"https://api.github.com/users/Mike-EEE","html_url":"https://github.com/Mike-EEE","followers_url":"https://api.github.com/users/Mike-EEE/followers","following_url":"https://api.github.com/users/Mike-EEE/following{/other_user}","gists_url":"https://api.github.com/users/Mike-EEE/gists{/gist_id}","starred_url":"https://api.github.com/users/Mike-EEE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mike-EEE/subscriptions","organizations_url":"https://api.github.com/users/Mike-EEE/orgs","repos_url":"https://api.github.com/users/Mike-EEE/repos","events_url":"https://api.github.com/users/Mike-EEE/events{/privacy}","received_events_url":"https://api.github.com/users/Mike-EEE/received_events","type":"User","site_admin":false},"created_at":"2016-12-07T12:59:57Z","updated_at":"2016-12-07T12:59:57Z","body":"Not enforcing a model is what makes it so error-prone and why MSBuild's (and .NET Configuration's for that matter) DOM is so disliked. :)  I think there is also a conceptual gap here which seems to be continually missed, in that \"data\" files should be seen as serialized objects by rule rather by exception.  That is, when you are using a data file, you should be describing describing a CLR object that will eventually be activated into memory -- not newed-up and translated XElement (or JToken) by XElement.  If you are going to go through all that work, why not simply serialize a POCO?  It's 1 line of code vs. O(n) (if I am applying my newly-acquired [O-Notation](https://en.wikipedia.org/wiki/Big_O_notation) knowledge correctly 😄) lines of code per new introduced element.\r\n\r\nIt's simply madness.  It doesn't help that MSFT continues to fall over itself by continuing to produce poor guidance as project.json suffers from this same malady.  This is also why I continue to sound like a broken record and point to Xaml as the premiere example of the correct way of handling serialization and \"configuration.\"\r\n\r\nAnd yes, such a shame about XAML.  [It is nearly 200 votes on the dedicated GitHub issue](https://github.com/dotnet/corefx/issues/5766) that continues to be ignored. 💢 "}}
{"actor":{"display_login":"Mike-EEE"}}