Today let’s see how to implement a WCF Data Service with Reflection Provider. The reflection provider exposes data in classes which returns an IQueryable<T>
I am going to use the same scenario which I used to demonstrate WCF Data Services with Entity Framework Provider.
I have three entities which are “Employee”, “Department” and “JobRole”. The relationships among these three entities are as follows.
- Employee can only belong to one Department and one Department can have many Employees.
- Employee can have many JobRoles and one JobRole can have many Employees.
I am deleting the service and it’s interface which was created initially when creating the project. I am going to add latest WCF Data Services references through Nuget. I am searching online for “WCF Data Services Server” and installing it.
Once all the needed references are installed, I am going to create a class which I am going to name as “MyDataContext”.
I am going to create three classes for each of my entity types.
OK, now you can see I have three classes. In the “Employee” class, I have two properties along with basic properties which are “Departments” and “JobRoles”. By having “Departments” property which is of type “Department”, I am mentioning the department a particular employee belong to. By having list of “JobRoles” which is of type “List<JobRole>”, I am mentioning the job roles a particular employee can have.
Now I need to have primary key fielda for each of these entity types. Of course, I have three properties which are “DepartmentId”,”JobRoleId” and “EmployeeId” which I can use as primary key fields. So for that I am going to decorate the class with the attribute DataServiceKeyAttribute.
So now I am going to create a method in each of these classes, which will return some data I want to expose to outside.
Once it is done, I have some entities and some methods which return some data. Now I am going to create my model or the context that I am going to expose through a WCF Data Service.
So I am modifying my “MyDataContext” class as follows.
I have added three Properties which is of IQueryable<T>. You might be wondering why specially IQueryable<T>, don’t worry about it now, we will discuss about it later.
Now I am almost done. Let’s add a WCF Data Service to the project and put “EntitySetRights.AllRead” as the entity access rule for all the entities, so consumers can read all entities.
Now when I hit F5, I can see the WCF Data Service up and running.
|WCF Data Service|
Now I am going to write the theoretical things related to Reflection Provider. The reason for me to didn’t mention these in the beginning and to mention now is, the best way to understand theory is, comparing it with the practical output.
Of course I will be copy and pasting the things from the MSDN, so you might will bored. But when you contrast them with WCF Data Service we just created, you will find them all interesting.
When you create the data service, the provider infers the data model by using reflection. The following list shows how the reflection provider infers the data model.
- Entity container - the class that exposes the data as properties that return an IQueryable<T> instance.
- Here it’s our class “MyDataContext”
- Entity sets - properties that return IQueryable<T> instances are treated as entity sets.
- Here it’s our properties in the “MyDataContext” class which are “Employees”, “Departments” and “JobRoles”. The reason to create properties that return IQueryable<T> instances are, basically it is a must. If you return IEnumerable<T>, you will not be able to see those entity sets in the WCF Data Service. Here is a nice post which describes IQueryable<T> vs. IEnumerable<T>
- Entity types - the type T of the IQueryable<T> that the entity set returns.
- Here it’s our entity classes which are “Employee”, “Department” and “JobRole”
- Entity keys - each data class that is an entity type must have a key property. This property is attributed with the DataServiceKeyAttribute attribute ([DataServiceKeyAttribute]).
- Here it’s “EmployeeId”, “DepartmentId” and “JobRoleId”.
- Entity type properties - other than the entity key, the reflection provider treats the accessible, non-indexer properties of a class that is an entity type as follows:
- Here it’s the properties of our entity classes.
- If the property returns a primitive type, then the property is assumed to be a property of an entity type.
- If the property returns a type that is also an entity type, then the property is assumed to be a navigation property that represents the "one" end of a many-to-one or one-to-one relationship.
- Departments property of Employee class
- If the property returns an IEnumerable<T> of an entity type, then the property is assumed to be a navigation property that represents the "many" end of a one-to-many or many-to-many relationship.
- JobRoles property of Employee
- If the return type of the property is a value type, then the property represents a complex type.
So that’s it. I am uploading the sample to my SkyDrive, Enjoy WCF Data Services with Reflection Provider.