Generic Data Access Layer for WCF : Part 3

In Part 2: Generating a Generic Data Access Layer using WCF we learned basics of DataContext and attributes generated by the ADO.NET Data Entity Framework. 
In this article, we will see how we can modify what the ADO.NET Entity Framework has provided us to achieve a Generic Data Access Layer.
This is how the entity framework generated code looked like:
Generic Dal using WCF 
Notice that each of the partial class extends a common class EntityObject. We will Build our generics on this concept.
Create a dummy class without any functionality, called TEntity. Let's drive this from the EntityObject to make it an entity object.
  1. public class TEntity : EntityObject  
  2. {  
  3. }  
Now modify the EntityFramework generated code to extend from TEntity class instead of the EntityObject class.
Generic Dal using WCF 
We need to do some decoration still to get this ready. Add the following code.
  1. [Serializable()]  
  2. [DataContractAttribute(IsReference = true)]  
  3. public class TEntity : EntityObject  
  4. {  
  6. }  
That's it. We are done. Our data access layer is ready now. 
You can now open the Architecture Explorer and see how the classes are created, as shown below. The Explorer shows the following view. 
Generic Dal using WCF 
And the class diagram looks like following.
Generic Dal using WCF 
Now, our generic layer is created. All we need to create a WCF Service Layer. The idea is, we will create our Business Layer in the WCF Service and the WCF service will have methods that will talk to the data layer to access the database.
In this article, we saw how to create a generic entity class and how to build our TEntity class. In the next article, I will discuss how to build a WCF Business Layer that then will consume the data layer to provide the data accessibility.

Similar Articles