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 Web Project first. After that you should see all your Entities/Tables of your Database in the context.

Tip 2:
Problem: When you create a new DomainService two files were created. *.cs and a *.metadata.cs file.
If you have selected several tables, the complete code will saved in one file. This can be very confusing.
I love it to have all structured and clear. So i create for each Entitie a new File.

Solution:
For example you create a DomainService with the name “DomainService” which contain two Entities “ExampleTable” and “AnotherTable”.
Now open the DomainService.cs file. In this file you see something like this(shorten Version):

DomainService.cs

namespace TestProject.Web.Services
{
    using System;
    [...] // Some other usings

    [EnableClientAccess()]
    public class DomainService : LinqToEntitiesDomainService<TestEntities>
    {
	// Methods of ExampleTable
	public IQueryable<ExampleTable> GetExampleTable(){[...]}

        public void InsertExampleTable(ExampleTable ExampleTable) {[...]}

        public void UpdateExampleTable(ExampleTable currentExampleTable){[...]}

        public void DeleteExampleTable(ExampleTable ExampleTable) {[...]}

	// Methods of AnotherTable
	public IQueryable<AnotherTable> GetAnotherTable(){[...] }

        public void InsertAnotherTable(AnotherTable AnotherTable) {[...] }

        public void UpdateAnotherTable(AnotherTable currentAnotherTable){[...]}

        public void DeleteAnotherTable(AnotherTable AnotherTable){[...] }
    }
}

What you now have to do is to create two new cs files. The names should be like this:

DomainService.ExampleTable.cs
DomainService.AnotherTable.cs

Now Copy the complete DomainService.cs file in it and delete

[EnableClientAccess()] in each file

After that you have to make the class to a partial class, so you have to change it like this:

public partial class DomainService : LinqToEntitiesDomainService

Now delete in each file the other Entities Methods.

The two files should look like this:

DomainService.ExampleTable.cs

 
namespace TestProject.Web.Services
{
    using System;
    [...]

    public partial class DomainService : LinqToEntitiesDomainService<TestEntities>
    {
	// Methods of ExampleTable
	public IQueryable<ExampleTable> GetExampleTable(){[...]}

        public void InsertExampleTable(ExampleTable ExampleTable) {[...]}

        public void UpdateExampleTable(ExampleTable currentExampleTable){[...]}

        public void DeleteExampleTable(ExampleTable ExampleTable) {[...]}
	}
}

DomainService.AnotherTable.cs

 
namespace TestProject.Web.Services
{
    using System;
    [...]

    public partial class DomainService : LinqToEntitiesDomainService<TestEntities>
    {
	// Methods of AnotherTable
	public IQueryable<AnotherTable> GetAnotherTable(){[...] }

        public void InsertAnotherTable(AnotherTable AnotherTable) {[...] }

        public void UpdateAnotherTable(AnotherTable currentAnotherTable){[...]}

        public void DeleteAnotherTable(AnotherTable AnotherTable){[...] }
	}
}

If you have done it in both files you now need to edit the DomainClass.cs file.

Make it even to partial and delete the Methods in the class so it lookes like this:

DomainService.cs

namespace TestProject.Web.Services.DomainService
{
    using System;
    [...]

    [EnableClientAccess()]
    public partial class DomainService : LinqToEntitiesDomainService<TestEntities>
    {
    }
}

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

Additional you can create a seperate folder for each DomainService.

Tip 3:

Problem: How to make Query over many Tables?

Solution:
The magic word is “Include”.
First you have to add the [Include] attribute in the metadata class to a EntityCollection of your Table you want to access.

Now you have to have to include the Table in the Query.

public IQueryable<ExampleTable> GetExampleTable()
{
	return this.ObjectContext.ExampleTable.Include("AnotherTable").Inlcude("AnotherTable.TableStatus").OrderBy(t => t.Table.TableID);
}

Now all Data of “AnotherTable” and “AnotherTable.TableStatus” were included and you can access them.

Tip 4:

Problem: How can i check what my DomainService returns and debug it?

Solution:
This is very simple, but a good trick.

public IQueryable<ExampleTable> GetExampleTable()
{
	var query = this.ObjectContext.ExampleTable.Include("AnotherTable").Inlcude("AnotherTable.TableStatus").OrderBy(t => t.Table.TableID);
	return query;
}

If you now add a breakpoint at “return query;” you can see the result of the query and which data were given back.