{"id":683,"date":"2012-07-02T16:28:05","date_gmt":"2012-07-02T16:28:05","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2012\/07\/02\/trying-out-the-prerelease-odata-client-t4-template\/"},"modified":"2024-02-12T16:29:38","modified_gmt":"2024-02-12T23:29:38","slug":"trying-out-the-prerelease-odata-client-t4-template","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/trying-out-the-prerelease-odata-client-t4-template\/","title":{"rendered":"Trying out the prerelease OData Client T4 template"},"content":{"rendered":"<p><strong>TL;DR<\/strong><\/p>\n<p>We recently prereleased an updated <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Data.Services.Client.T4\/\" target=\"_blank\" rel=\"noopener\">OData Client T4<\/a> template to NuGet. The template will codegen classes for working with OData v3 services, but full support has not been added for all v3 features.<\/p>\n<h3>What is the OData Client T4 template?<\/h3>\n<p>The <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Data.Services.Client.T4\/\" target=\"_blank\" rel=\"noopener\">OData Client T4<\/a> template is a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/bb126445.aspx\" target=\"_blank\" rel=\"noopener\">T4 template<\/a> that generates code to facilitate consumption of OData services. The template will generate a DataServiceContext and classes for each of the entity types and complex types found in the service description. The template should produce code that is functionally equivalent to the Add Service Reference experience. The T4 template provides some compelling benefits:<\/p>\n<ul>\n<li>Developers can customize the T4 template to generate exactly the code they desire<\/li>\n<li>The T4 template can be <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee847423.aspx\" target=\"_blank\" rel=\"noopener\">executed at build time<\/a>, ensuring that service references are always fully up-to-date (and that any discrepancies cause a build error rather than a runtime error)<\/li>\n<\/ul>\n<p>In this blog post, we will walk through how to obtain and use the OData Client T4 template. We will not be covering other details of T4; much of that information is available on <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/bb126445.aspx\" target=\"_blank\" rel=\"noopener\">MSDN<\/a>.<\/p>\n<h3>OData Client T4 template walkthrough<\/h3>\n<ol>\n<li>\n<p>Let\u2019s begin by creating a new <strong>Console Application<\/strong> project.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/6082.image_thumb_2C9E5FEE.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/6082.image_thumb_2C9E5FEE.png\" width=\"644\" height=\"447\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>Next we need to use NuGet to get the T4 template. We can do this by right-clicking the project in the <strong>Solution Explorer<\/strong> and choosing <strong>Manage NuGet Packages<\/strong>. NOTE: You may need to <a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/27077b70-9dad-4c64-adcf-c7cf6bc9970c\" target=\"_blank\" rel=\"noopener\">get the NuGet Package Manager<\/a> if you are using VS 2010.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/5428.image_thumb_0F2C0EE2.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/5428.image_thumb_0F2C0EE2.png\" width=\"527\" height=\"340\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>In the Package Explorer window, choose <strong>Online<\/strong> from the left nav, ensure that the prerelease dropdown is set to <strong>Include Prerelease<\/strong> (not <strong>Stable Only<\/strong>), and search for <strong>Microsoft.Data.Services.Client.T4<\/strong>. You should see the <strong><a href=\"http:\/\/nuget.org\/packages\/microsoft.data.services.client.t4\" target=\"_blank\" rel=\"noopener\">OData Client T4<\/a><\/strong> package appear; click the <strong>Install <\/strong>button to install the package.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/3441.image_thumb_67F1C5AC.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/3441.image_thumb_67F1C5AC.png\" width=\"644\" height=\"432\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>When the OData Client T4 package has been added to the project, you should see a file called <strong>Reference.tt<\/strong> appear in the <strong>Solution Explorer<\/strong>. (NOTE: It\u2019s perfectly fine at this point to change the name of the file to something more meaningful for your project.) Expand <strong>Reference.tt<\/strong> using the caret to the left of the file and double-click to open <strong>Reference.cs<\/strong>. Note that the contents of <strong>Reference.cs<\/strong> were generated from the T4 template and that they provide further instructions on how to use the template.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/2867.image_thumb_1C9221E8.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/2867.image_thumb_1C9221E8.png\" width=\"644\" height=\"116\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>Double-click Reference.tt to open the T4 template. On line 30, add the following: <code>MetadataUri = <a href=\"http:\/\/odata.netflix.com\/Catalog\/$metadata\">http:\/\/odata.netflix.com\/Catalog\/$metadata<\/a>;<\/code>. NOTE: The T4 template does not currently support actions; if you specify a $metadata that contains actions, code generation will fail.<\/p>\n<\/li>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/8737.image_thumb_0A495B26.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/8737.image_thumb_0A495B26.png\" width=\"545\" height=\"125\" \/><\/a><\/p>\n<li>\n<p>Save Reference.tt and expand Reference.cs in the Solution Explorer to see the newly generated classes. (NOTE: If you\u2019re using VS 2010, you\u2019ll need to open Reference.cs and browse the code in the file.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/5187.image_thumb_78009463.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/5187.image_thumb_78009463.png\" width=\"247\" height=\"244\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>Finally, add the following code to Program.cs.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/7563.image_thumb_37CA7AE9.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/7563.image_thumb_37CA7AE9.png\" width=\"644\" height=\"225\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>Press Ctrl+F5 to see the result.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/8637.image_thumb_2581B427.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2012\/07\/8637.image_thumb_2581B427.png\" width=\"644\" height=\"328\" \/><\/a><\/p>\n<\/li>\n<\/ol>\n<h3>Why should I use the T4 template?<\/h3>\n<p>Currently the <strong>Add Service Reference<\/strong> and <strong>DataSvcUtil <\/strong>experiences are both based on a DLL that does not allow for customizable code generation. We believe that in the future both of these experiences will use the T4 template internally, with the hope that you would be able to customize the output of either experience. (Imagine the simplicity of ASR with the power of T4!) To do that, we need to get the T4 template to a quality level equal to or better than the <strong>ASR<\/strong> experience.<\/p>\n<h3>Feedback, please!<\/h3>\n<p>We\u2019d love to hear from you! Specifically, we would really benefit from answers to the following questions:<\/p>\n<ul>\n<li>Did the T4 template work for you? Why or why not?<\/li>\n<li>What customizability should be easier in the T4 template?<\/li>\n<li>Do you use DataSvcUtil today? For what reason?<\/li>\n<li>If you only use DataSvcUtil for msbuild chaining, could you <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee847423.aspx\" target=\"_blank\" rel=\"noopener\">chain a T4 template<\/a> instead?<\/li>\n<li>Do you want the T4 template to generate C# or VB.NET?<\/li>\n<\/ul>\n<p>Thanks,    <br \/>The WCF Data Services Team<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR We recently prereleased an updated OData Client T4 template to NuGet. The template will codegen classes for working with OData v3 services, but full support has not been added for all v3 features. What is the OData Client T4 template? The OData Client T4 template is a T4 template that generates code to facilitate [&hellip;]<\/p>\n","protected":false},"author":512,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[48,71,78,85],"class_list":["post-683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata","tag-odata","tag-t4","tag-wcf-data-services","tag-whats-new"],"acf":[],"blog_post_summary":"<p>TL;DR We recently prereleased an updated OData Client T4 template to NuGet. The template will codegen classes for working with OData v3 services, but full support has not been added for all v3 features. What is the OData Client T4 template? The OData Client T4 template is a T4 template that generates code to facilitate [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/users\/512"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/683\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media?parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}