{"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":"

Hello folks,<\/p>\n

today i want to show you some tipps which you can need when you first work with the DomainService Class.<\/p>\n

Tip 1:<\/strong>
\nProblem: <\/em>My Entities\/Tables are not shown at the creation Context<\/p>\n

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

Tip 2: <\/strong>
\nProblem: <\/em>When you create a new DomainService two files were created. *.cs and a *.metadata.cs file.
\nIf you have selected several tables, the complete code will saved in one file. This can be very confusing.
\nI love it to have all structured and clear. So i create for each Entitie a new File.<\/p>\n

Solution: <\/em>
\nFor example you create a DomainService with the name \u201cDomainService\u201d which contain two Entities \u201cExampleTable\u201d and \u201cAnotherTable\u201d.
\nNow open the DomainService.cs file. In this file you see something like this(shorten Version):<\/p>\n

DomainService.cs<\/strong><\/p>\n

\n\n\n\n
\n
namespace TestProject.Web.Services\n{\n    using System;\n    [...] \/\/ Some other usings\n\n    [EnableClientAccess()]\n    public class DomainService : LinqToEntitiesDomainService<TestEntities>\n    {\n\t\/\/ Methods of ExampleTable\n\tpublic IQueryable<ExampleTable> 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<AnotherTable> 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

What you now have to do is to create two new cs files. The names should be like this:
\n
\nDomainService.ExampleTable.cs
\nDomainService.AnotherTable.cs<\/code><\/p>\n

Now Copy the complete DomainService.cs file in it and delete<\/p>\n

[EnableClientAccess()]<\/code> in each file<\/p>\n

After that you have to make the class to a partial class, so you have to change it like this:<\/p>\n

public partial <\/strong>class DomainService : LinqToEntitiesDomainService<\/code><\/p>\n

Now delete in each file the other Entities Methods.<\/p>\n

The two files should look like this:
\n
\nDomainService.ExampleTable.cs<\/strong><\/p>\n

\n\n\n\n
\n
\u00a0\nnamespace TestProject.Web.Services\n{\n    using System;\n    [...]\n\n    public partial class DomainService : LinqToEntitiesDomainService<TestEntities>\n    {\n\t\/\/ Methods of ExampleTable\n\tpublic IQueryable<ExampleTable> 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

DomainService.AnotherTable.cs<\/strong><\/p>\n

\n\n\n\n
\n
\u00a0\nnamespace TestProject.Web.Services\n{\n    using System;\n    [...]\n\n    public partial class DomainService : LinqToEntitiesDomainService<TestEntities>\n    {\n\t\/\/ Methods of AnotherTable\n\tpublic IQueryable<AnotherTable> 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

If you have done it in both files you now need to edit the DomainClass.cs file.<\/p>\n

Make it even to partial and delete the Methods in the class so it lookes like this:<\/p>\n

DomainService.cs<\/strong><\/p>\n

\n\n\n\n
\n
namespace TestProject.Web.Services.DomainService\n{\n    using System;\n    [...]\n\n    [EnableClientAccess()]\n    public partial class DomainService : LinqToEntitiesDomainService<TestEntities>\n    {\n    }\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n

Now you are finished. This is a lot clearer for you now. The Compiler still see this as one file.
\nIf you want to append your Service with a new IQueryable Method you dont have to search through the complete file.<\/p>\n

Additional you can create a seperate folder for each DomainService.<\/p>\n

Tip 3:<\/strong><\/p>\n

Problem: <\/em>How to make Query over many Tables?<\/p>\n

Solution: <\/em>
\nThe magic word is \u201cInclude\u201d.
\nFirst you have to add the [Include] attribute in the metadata class to a EntityCollection of your Table you want to access.<\/p>\n

Now you have to have to include the Table in the Query.<\/p>\n

\n\n\n\n
\n
public IQueryable<ExampleTable> GetExampleTable()\n{\n\treturn this.ObjectContext.ExampleTable.Include(\"AnotherTable\").Inlcude(\"AnotherTable.TableStatus\").OrderBy(t => t.Table.TableID);\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n

Now all Data of \u201cAnotherTable\u201d and \u201cAnotherTable.TableStatus\u201d were included and you can access them.<\/p>\n

Tip 4:<\/strong><\/p>\n

Problem:<\/em> How can i check what my DomainService returns and debug it?<\/p>\n

Solution: <\/em>
\nThis is very simple, but a good trick.<\/p>\n

\n\n\n\n
\n
public IQueryable<ExampleTable> GetExampleTable()\n{\n\tvar query = this.ObjectContext.ExampleTable.Include(\"AnotherTable\").Inlcude(\"AnotherTable.TableStatus\").OrderBy(t => t.Table.TableID);\n\treturn query;\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n

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>