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:
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.
- public class TEntity : EntityObject
- {
- }
Now modify the EntityFramework generated code to extend from TEntity class
instead of the EntityObject class.
We need to do some decoration still to get this ready. Add the following code.
- [Serializable()]
- [DataContractAttribute(IsReference = true)]
- public class TEntity : EntityObject
- {
-
- }
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.
And the class diagram looks like following.
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.
Summary
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.