<!-- index.php -->
{"id":37,"date":"2011-09-30T18:54:54","date_gmt":"2011-09-30T16:54:54","guid":{"rendered":"http:\/\/pustekuchen.xn--burkfrulein-q8a.de\/?p=37"},"modified":"2011-09-30T18:54:54","modified_gmt":"2011-09-30T16:54:54","slug":"how-to-work-with-domainservice-class","status":"publish","type":"post","link":"https:\/\/blog.bur-k.de\/?p=37","title":{"rendered":"How to work with DomainService Class"},"content":{"rendered":"<p>Hello folks,<\/p>\n<p>today i want to show you some tipps which you can need when you first work with the DomainService Class.<\/p>\n<p><strong>Tip 1:<\/strong><br \/>\n<em>Problem: <\/em>My Entities\/Tables are not shown at the creation Context<\/p>\n<p><em>Solution:<\/em> You have to Create your Web Project first. After that you should see all your Entities\/Tables of your Database in the context.<\/p>\n<p><strong>Tip 2: <\/strong><br \/>\n<em>Problem: <\/em>When you create a new DomainService two files were created. *.cs and a *.metadata.cs file.<br \/>\nIf you have selected several tables, the complete code will saved in one file. This can be very confusing.<br \/>\nI love it to have all structured and clear. So i create for each Entitie a new File.<\/p>\n<p><em>Solution: <\/em><br \/>\nFor example you create a DomainService with the name \u201cDomainService\u201d which contain two Entities \u201cExampleTable\u201d and \u201cAnotherTable\u201d.<br \/>\nNow open the DomainService.cs file. In this file you see something like this(shorten Version):<\/p>\n<p><strong>DomainService.cs<\/strong><\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>namespace TestProject.Web.Services\n{\n    using System;\n    [...] \/\/ Some other usings\n\n    [EnableClientAccess()]\n    public class DomainService : LinqToEntitiesDomainService&lt;TestEntities&gt;\n    {\n\t\/\/ Methods of ExampleTable\n\tpublic IQueryable&lt;ExampleTable&gt; GetExampleTable(){[...]}\n\n        public void InsertExampleTable(ExampleTable ExampleTable) {[...]}\n\n        public void UpdateExampleTable(ExampleTable currentExampleTable){[...]}\n\n        public void DeleteExampleTable(ExampleTable ExampleTable) {[...]}\n\n\t\/\/ Methods of AnotherTable\n\tpublic IQueryable&lt;AnotherTable&gt; GetAnotherTable(){[...] }\n\n        public void InsertAnotherTable(AnotherTable AnotherTable) {[...] }\n\n        public void UpdateAnotherTable(AnotherTable currentAnotherTable){[...]}\n\n        public void DeleteAnotherTable(AnotherTable AnotherTable){[...] }\n    }\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>What you now have to do is to create two new cs files. The names should be like this:<br \/>\n<code><br \/>\nDomainService.ExampleTable.cs<br \/>\nDomainService.AnotherTable.cs<\/code><\/p>\n<p>Now Copy the complete DomainService.cs file in it and delete<\/p>\n<p><code lang=\"csharp\">[EnableClientAccess()]<\/code> in each file<\/p>\n<p>After that you have to make the class to a partial class, so you have to change it like this:<\/p>\n<p><code lang=\"csharp\">public <strong>partial <\/strong>class DomainService : LinqToEntitiesDomainService<\/code><\/p>\n<p>Now delete in each file the other Entities Methods.<\/p>\n<p>The two files should look like this:<br \/>\n<strong><br \/>\nDomainService.ExampleTable.cs<\/strong><\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\u00a0\nnamespace TestProject.Web.Services\n{\n    using System;\n    [...]\n\n    public partial class DomainService : LinqToEntitiesDomainService&lt;TestEntities&gt;\n    {\n\t\/\/ Methods of ExampleTable\n\tpublic IQueryable&lt;ExampleTable&gt; GetExampleTable(){[...]}\n\n        public void InsertExampleTable(ExampleTable ExampleTable) {[...]}\n\n        public void UpdateExampleTable(ExampleTable currentExampleTable){[...]}\n\n        public void DeleteExampleTable(ExampleTable ExampleTable) {[...]}\n\t}\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><strong>DomainService.AnotherTable.cs<\/strong><\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>\u00a0\nnamespace TestProject.Web.Services\n{\n    using System;\n    [...]\n\n    public partial class DomainService : LinqToEntitiesDomainService&lt;TestEntities&gt;\n    {\n\t\/\/ Methods of AnotherTable\n\tpublic IQueryable&lt;AnotherTable&gt; GetAnotherTable(){[...] }\n\n        public void InsertAnotherTable(AnotherTable AnotherTable) {[...] }\n\n        public void UpdateAnotherTable(AnotherTable currentAnotherTable){[...]}\n\n        public void DeleteAnotherTable(AnotherTable AnotherTable){[...] }\n\t}\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>If you have done it in both files you now need to edit the DomainClass.cs file.<\/p>\n<p>Make it even to partial and delete the Methods in the class so it lookes like this:<\/p>\n<p><strong>DomainService.cs<\/strong><\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>namespace TestProject.Web.Services.DomainService\n{\n    using System;\n    [...]\n\n    [EnableClientAccess()]\n    public partial class DomainService : LinqToEntitiesDomainService&lt;TestEntities&gt;\n    {\n    }\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Now you are finished. This is a lot clearer for you now. The Compiler still see this as one file.<br \/>\nIf you want to append your Service with a new IQueryable Method you dont have to search through the complete file.<\/p>\n<p>Additional you can create a seperate folder for each DomainService.<\/p>\n<p><strong>Tip 3:<\/strong><\/p>\n<p><em>Problem: <\/em>How to make Query over many Tables?<\/p>\n<p><em>Solution: <\/em><br \/>\nThe magic word is \u201cInclude\u201d.<br \/>\nFirst you have to add the [Include] attribute in the metadata class to a EntityCollection of your Table you want to access.<\/p>\n<p>Now you have to have to include the Table in the Query.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>public IQueryable&lt;ExampleTable&gt; GetExampleTable()\n{\n\treturn this.ObjectContext.ExampleTable.Include(\"AnotherTable\").Inlcude(\"AnotherTable.TableStatus\").OrderBy(t =&gt; t.Table.TableID);\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Now all Data of \u201cAnotherTable\u201d and \u201cAnotherTable.TableStatus\u201d were included and you can access them.<\/p>\n<p><strong>Tip 4:<\/strong><\/p>\n<p><em>Problem:<\/em> How can i check what my DomainService returns and debug it?<\/p>\n<p><em>Solution: <\/em><br \/>\nThis is very simple, but a good trick.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>public IQueryable&lt;ExampleTable&gt; GetExampleTable()\n{\n\tvar query = this.ObjectContext.ExampleTable.Include(\"AnotherTable\").Inlcude(\"AnotherTable.TableStatus\").OrderBy(t =&gt; t.Table.TableID);\n\treturn query;\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>If you now add a breakpoint at \u201creturn query;\u201d you can see the result of the query and which data were given back.<\/p>\n<div class=\"twoclick_social_bookmarks_post_37 social_share_privacy clearfix 1.6.4 locale-de_DE sprite-de_DE\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_37')){$('.twoclick_social_bookmarks_post_37').socialSharePrivacy({\"services\":{\"facebook\":{\"status\":\"on\",\"txt_info\":\"2 Klicks f\\u00fcr mehr Datenschutz: Erst wenn Sie hier klicken, wird der Button aktiv und Sie k\\u00f6nnen Ihre Empfehlung an Facebook senden. Schon beim Aktivieren werden Daten an Dritte \\u00fcbertragen - siehe <em>i<\\\/em>.\",\"perma_option\":\"off\",\"action\":\"like\",\"language\":\"de_DE\"},\"twitter\":{\"reply_to\":\"\",\"tweet_text\":\"%20How%20to%20work%20with%20DomainService%20Class%20%C2%BB%20pustekuchens%20Blog%20%23Class%20%23Domain%20...\",\"status\":\"on\",\"txt_info\":\"2 Klicks f\\u00fcr mehr Datenschutz: Erst wenn Sie hier klicken, wird der Button aktiv und Sie k\\u00f6nnen Ihre Empfehlung an Twitter senden. Schon beim Aktivieren werden Daten an Dritte \\u00fcbertragen - siehe <em>i<\\\/em>.\",\"perma_option\":\"off\",\"language\":\"de\",\"referrer_track\":\"\"},\"gplus\":{\"status\":\"on\",\"txt_info\":\"2 Klicks f\\u00fcr mehr Datenschutz: Erst wenn Sie hier klicken, wird der Button aktiv und Sie k\\u00f6nnen Ihre Empfehlung an Google+ senden. Schon beim Aktivieren werden Daten an Dritte \\u00fcbertragen - siehe <em>i<\\\/em>.\",\"perma_option\":\"off\",\"referrer_track\":\"\"}},\"txt_help\":\"Wenn Sie diese Felder durch einen Klick aktivieren, werden Informationen an Facebook, Twitter, Flattr, Xing, t3n, LinkedIn, Pinterest oder Google eventuell ins Ausland \\u00fcbertragen und unter Umst\\u00e4nden auch dort gespeichert. N\\u00e4heres erfahren Sie durch einen Klick auf das <em>i<\\\/em>.\",\"settings_perma\":\"Dauerhaft aktivieren und Daten\\u00fcber-tragung zustimmen:\",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/blog.bur-k.de\\\/?p=37\",\"post_id\":37,\"post_title_referrer_track\":\"How+to+work+with+DomainService+Class\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div>","protected":false},"excerpt":{"rendered":"<p>Hello folks, today i want to show you some tipps which you can need when you first work with the DomainService Class. Tip 1: Problem: My Entities\/Tables are not shown at the creation Context Solution: You have to Create your&#8230; <a class=\"more-link\" href=\"https:\/\/blog.bur-k.de\/?p=37\">Weiterlesen &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[30,35,36,37,38,83,100,101,109],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-c","category-silverlight","tag-class","tag-domain","tag-domainservice","tag-domainserviceclass","tag-domaenendienstklasse","tag-partial","tag-server","tag-service","tag-structure"],"_links":{"self":[{"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":0,"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bur-k.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}