{"action":"created","issue":{"url":"https://api.github.com/repos/Starcounter/Starcounter/issues/3105","labels_url":"https://api.github.com/repos/Starcounter/Starcounter/issues/3105/labels{/name}","comments_url":"https://api.github.com/repos/Starcounter/Starcounter/issues/3105/comments","events_url":"https://api.github.com/repos/Starcounter/Starcounter/issues/3105/events","html_url":"https://github.com/Starcounter/Starcounter/issues/3105","id":118919239,"number":3105,"title":"Use case: Modelling view with data from independent models","user":{"login":"per-samuelsson","id":2902045,"avatar_url":"https://avatars.githubusercontent.com/u/2902045?v=3","gravatar_id":"","url":"https://api.github.com/users/per-samuelsson","html_url":"https://github.com/per-samuelsson","followers_url":"https://api.github.com/users/per-samuelsson/followers","following_url":"https://api.github.com/users/per-samuelsson/following{/other_user}","gists_url":"https://api.github.com/users/per-samuelsson/gists{/gist_id}","starred_url":"https://api.github.com/users/per-samuelsson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/per-samuelsson/subscriptions","organizations_url":"https://api.github.com/users/per-samuelsson/orgs","repos_url":"https://api.github.com/users/per-samuelsson/repos","events_url":"https://api.github.com/users/per-samuelsson/events{/privacy}","received_events_url":"https://api.github.com/users/per-samuelsson/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Starcounter/Starcounter/labels/question","name":"question","color":"02d7e1"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-11-25T20:33:48Z","updated_at":"2015-11-26T10:12:19Z","closed_at":null,"body":"Consider this imaginary model:\r\n\r\n```C#\r\n[Database]\r\nclass Product {\r\n  string Id;\r\n  string Name;\r\n  string Description;\r\n}\r\n\r\n[Database]\r\nclass ProductSupplier {\r\n  int Rating;\r\n}\r\n```\r\n\r\nBoth classes have a relationship, but solving it is not trivial. Lets say we must do a set of selects to find a certain supplier we want. Bottomline: it's not warrant to expose suppliers from the `Product` class in terms of a property, not even one with a path (like `Product,Something.SomehingElse.Supplier`).\r\n\r\nNow lets say I want to design a view model that support showing a grid where the first column is the product basics (Id, Name, Description), but the last column is the product supplier with the highest rating.\r\n\r\nId | Name | Description | Supplier rating\r\n---- | ----- | ------ | ------\r\n1 | Win10 | Windows 10 licence | 8\r\n\r\nWhats my take here?\r\n\r\nThe most intuitive way would be to model a view that support an array of products.\r\n\r\n**Products.json**\r\n```json\r\n{\r\n  \"Items\" : [\r\n    { \"Id\": 0, \"Name\" : \"\", \"Description\" : \"\", \"SupplierRating\": 0 }\r\n  ]\r\n}\r\n```\r\n\r\nAnd to use `Product` instances as my data:\r\n\r\n```C#\r\nvar json = new Products();\r\njson.Items = Db.SQL<Products>(\"SELECT ...\");\r\n```\r\n\r\nBut that would leave the rating empty.\r\n\r\nOne technically possible way would be do introduce some intermediate POCO, like `ProductWithSupplierRating`, that I feed with the product and in which I implement the complex finding of the highest supplier rating.\r\n\r\n```C#\r\nclass ProductWithSupplierRating {\r\n  Product product;\r\n  int rating;\r\n\r\n  public ProductWithSupplierRating(Product p) {\r\n    product = p;\r\n    FindRating();\r\n  }\r\n\r\n  int Rating { get { return rating; } }\r\n  string Id { get { return product.Id; } }\r\n  // [...]\r\n}\r\n```\r\n\r\nAnd then\r\n\r\n```C#\r\nvar json = new Products();\r\njson.Items = Helper.SelectProductsAndCreateWithRatingList();\r\n```\r\n\r\nBut that would mean I'd have to maintain more code that I want, and also make me pay a performance penalty and bloated memory.\r\n\r\nLets try find some best practice / pattern for this, as I think its a case we'll need to be prepared for.\r\n\r\n/cc @jwillmer"},"comment":{"url":"https://api.github.com/repos/Starcounter/Starcounter/issues/comments/159867374","html_url":"https://github.com/Starcounter/Starcounter/issues/3105#issuecomment-159867374","issue_url":"https://api.github.com/repos/Starcounter/Starcounter/issues/3105","id":159867374,"user":{"login":"k-rus","id":1726044,"avatar_url":"https://avatars.githubusercontent.com/u/1726044?v=3","gravatar_id":"","url":"https://api.github.com/users/k-rus","html_url":"https://github.com/k-rus","followers_url":"https://api.github.com/users/k-rus/followers","following_url":"https://api.github.com/users/k-rus/following{/other_user}","gists_url":"https://api.github.com/users/k-rus/gists{/gist_id}","starred_url":"https://api.github.com/users/k-rus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/k-rus/subscriptions","organizations_url":"https://api.github.com/users/k-rus/orgs","repos_url":"https://api.github.com/users/k-rus/repos","events_url":"https://api.github.com/users/k-rus/events{/privacy}","received_events_url":"https://api.github.com/users/k-rus/received_events","type":"User","site_admin":false},"created_at":"2015-11-26T10:12:19Z","updated_at":"2015-11-26T10:12:19Z","body":"One possibility to write a query and then populate the result directly into json collection of objects. We discuss that such scenarios can be valuable and new SQL processor can support this by creating objects as result of the query. But this is future plans and details will need to be investigated further."}}