.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Generic Dal using WCF: Part 2

Posted By:Mahadesh Mahalingappa       Posted Date: September 20, 2011    Points: 200    Category: WCF    URL: http://www.dotnetspark.com  

In this post we will take a deeper look at what Entity Framework provides us and how we can modify it to achieve a Generic Data Access Layer.
 

  
Generic Dal using WCF - Part 2:
 
In this post we will take a deeper look at what Entity Framework provides us and how we can modify it to achieve a Generic Data Access Layer.

You can also follow my 1st post here
  
As we saw in the last post , Entity Framework helped us create a Data Model . Lets take a deeper look at the code behind that was generated by the Entity Framework .
 
Entity Framework creates a ObjectContext class for us . This class provides us access to all the Tables in the Database and inturn provides access to the Database .
 
Below is how the class Generated by Entity Framework will look like :

public partial class PublishingCompanyEntities : ObjectContext
{
   
}
 
 
The class extends the ObjectContext class . Lets take a look at what ObjectContext looks like :
 
 

 
As shown in the screenshot above , ObjectContext provides us with methods which help us access the Database .
 
 
Now lets take a look at the Entities . Entity Framework creates partial classes for each of the tables in the Data Model . These partial classes extend the EntityObject Class as shown below :
 

 
 
Below is the code for the Author Entity :
  
  
[EdmEntityTypeAttribute(NamespaceName = "PublishingCompanyModel", Name = "Author")]
[Serializable()]
[DataContractAttribute(IsReference = true)]
public partial class Author : EntityObject
{
    #region Factory Method

    /// 
    /// Create a new Author object.
    /// 
    /// Initial value of the AuthorID property.
    public static Author CreateAuthor(global::System.Int32 authorID)
    {
        Author author = new Author();
        author.AuthorID = authorID;
        return author;
    }

    #endregion
    #region Primitive Properties

    /// 
    /// No Metadata Documentation available.
    /// 
    [EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
    [DataMemberAttribute()]
    public global::System.Int32 AuthorID
    {
        get
        {
            return _AuthorID;
        }
        set
        {
            if (_AuthorID != value)
            {
                OnAuthorIDChanging(value);
                ReportPropertyChanging("AuthorID");
                _AuthorID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("AuthorID");
                OnAuthorIDChanged();
            }
        }
    }
    private global::System.Int32 _AuthorID;
    partial void OnAuthorIDChanging(global::System.Int32 value);
    partial void OnAuthorIDChanged();

    /// 
    /// No Metadata Documentation available.
    /// 
    [EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
    [DataMemberAttribute()]
    public global::System.String FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            OnFirstNameChanging(value);
            ReportPropertyChanging("FirstName");
            _FirstName = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("FirstName");
            OnFirstNameChanged();
        }
    }
    private global::System.String _FirstName;
    partial void OnFirstNameChanging(global::System.String value);
    partial void OnFirstNameChanged();

    /// 
    /// No Metadata Documentation available.
    /// 
    [EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
    [DataMemberAttribute()]
    public global::System.String LastName
    {
        get
        {
            return _LastName;
        }
        set
        {
            OnLastNameChanging(value);
            ReportPropertyChanging("LastName");
            _LastName = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("LastName");
            OnLastNameChanged();
        }
    }
    private global::System.String _LastName;
    partial void OnLastNameChanging(global::System.String value);
    partial void OnLastNameChanged();

    #endregion

    #region Navigation Properties

    /// 
    /// No Metadata Documentation available.
    /// 
    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("PublishingCompanyModel", "FK_Article_Author", "Article")]
    public EntityCollection
Articles { get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection
("PublishingCompanyModel.FK_Article_Author", "Article"); } set { if ((value != null)) { ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection
("PublishingCompanyModel.FK_Article_Author", "Article", value); } } } /// /// No Metadata Documentation available. /// [XmlIgnoreAttribute()] [SoapIgnoreAttribute()] [DataMemberAttribute()] [EdmRelationshipNavigationPropertyAttribute("PublishingCompanyModel", "FK_Payroll_Author", "Payroll")] public EntityCollection Payrolls { get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection("PublishingCompanyModel.FK_Payroll_Author", "Payroll"); } set { if ((value != null)) { ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection("PublishingCompanyModel.FK_Payroll_Author", "Payroll", value); } } } #endregion }


Each partial class is decorated with attributes  : Lets discuss each attribute :
 
EdmEntityTypeAttribute :


 Attribute that indicates that the class represents an entity type. Without this attribute on top of the partial class , the Class would not be recognised as a Entity Type .

 
Example of using this attribute is shown below :
 
[EdmEntityTypeAttribute(NamespaceName="PublishingCompanyModel", Name="Author")]
   
Serializable :
 
Indicates that a class can be serialized.  Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.
Example is given below :
  
 [Serializable()]
 
 
DataContractAttribute:
 
Specifies that the type defines or implements a data contract and is serializable by a serializer, such as the DataContractSerializer. To make their type serializable, type authors must define a data contract for their type.
Example is given below :
 
 
  [DataContractAttribute(IsReference=true)]
 
 
In this was Entity Framework creates a very good DataAcess Layer over which we could Build our Business Layer by accessing the entities in the DataModel but we want to develop a Generic data access layer .
 
Lets see in our next post how we would achieve this .


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend