Click here to Skip to main content
15,885,875 members
Articles / Web Development / IIS
Article

Create Sample Application Using Entity Objects In C#.Net

Rate me:
Please Sign up or sign in to vote.
1.53/5 (14 votes)
15 Oct 2007CPOL3 min read 92.6K   1.2K   31   14
Create Sample Application Using Entity Objects Using C#.Net

Introduction:

I will explain the entity classes which is More superior than datasets and typed datasets.
In this article I will just explain the basics of entity Classes and how you can retrieve data using them.<o:p>

DataSets:

Normally We are using Dataset for storing database values.When Compared to Entity Classes Dataset is very slow.As you all Know Dataset contains Collection of Tables its takes large memory for storing these tables.It reduces productivity. DataSets keep your data in a relational format, making them powerful and easy to use with relational databases. Unfortunately, this means you lose out on all the benefits of OOPS.

Problems With DataSets:

1.The conversion can fail because:· The value could be null· The developer might be wrong about the underlying data type (againintimate knowledge of the database schema is required). If you are using ordinal values, who knows what column is actually atposition X2. 2.ds.Tables(0) might return a null reference (if any part of the DAL method or thestored procedure failed

Entity Classes:

Entity classes provides more flexibility since they access the data using special methods defined in the class library.Take advantage of OOPS Techniques such as Inheritance and Encaptulation.

Steps For Sample:

<o:p>

1. Create a Customer class :

In this Customer Class is Used to get and set the Customer Details.It contains ContactTitle,ContactName,CompanyName,City Properties.

Customer Class:

 /// <summary>
/// Class For Setting Customer Details to Property
/// </summary>
public class Customer
{
    /// <summary>
    /// Property For Setting Customer ContactTitle
    /// </summary>
    private string contactTitle;

    public string ContactTitle
    {
        get { return contactTitle; }
        set { contactTitle = value; }
    }
    /// <summary>
    /// Property For Setting Customer ContactName
    /// </summary>
    private string contactName;

    public string ContactName
    {
        get { return contactName; }
        set { contactName = value; }
    }
    /// <summary>
    /// Property For Setting Customer CompanyName
    /// </summary>
    private string companyName;

    public string CompanyName
    {
        get { return companyName; }
        set { companyName = value; }
    }
    /// <summary>
    /// Property For Setting Customer City
    /// </summary>
    private string city;

    public string City
    {
        get { return city; }
        set { city = value; }
    }
}

2.Establish the DataBase Connection :

Establish the DataBaseConnection By Using the Below Method.Here I am Using DataReader For Reading a DataRow Values and Return the DataReader Object.For Establish a Connection I am Using Connection String.Connection String is Placed in Web.config file.Here I am Using Northwind DataBase For Getting Customer Details.

"select ContactTitle,ContactName,CompanyName,City from Customers"

3. Get Single Customers :

If you Want to Get Single Row(ie.In First Row) I am using the Following Method.Set the Datas to Datareader Object.It Returns the Method PopulateCustomer.

Please Refer attached code to GetDatasFromDataBase() method

/// <summary>
/// Method For Getting SingleValues Customer
/// </summary>
/// <returns></returns>
public Customer GetSingleCustomer()
{
    IDataReader dr = null;
    try
    {
        //set The Datas to Datareader
        dr=GetDatasFromDataBase();
        //Read the Single Row
        dr.Read();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return PopulateCustomer(dr);
}

<o:p> <o:p>

Populate Customer:

PoulateCustomer is the Method,it is used to Get the Single Values From the DataReader Object and then set into the Customer Properties likeContactTitle.ContactName,CompanyName,City.The PopulateCustomer method simply checks that if the "ContactTitle" and "ContactName" and "CompanyName" and "City" fields are not null and if its not null adds it to the Customerobject and returns the Customer object to the calling program.

For Mapping function we use IDataRecord,insted of using SqlDataReader.

/// <summary>
/// PopulateCustomer method which returns the Customer object.
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
public Customer PopulateCustomer(IDataRecord dr)
{
    //Create Object For Customer Class
    Customer objCustomer = new Customer();
    //Get the single ContactTitle from DataReader
    if (dr["ContactTitle"] != DBNull.Value)
    {
        //set  the Value to Customer Property
        objCustomer.ContactTitle = (string)dr["ContactTitle"];
    }
    //Get the single ContactName from DataReader
    if (dr["ContactName"] != DBNull.Value)
    {
        //set  the Value to Customer Property
        objCustomer.ContactName = (string)dr["ContactName"];
    }
    //Get the single CompanyName from DataReader
    if (dr["CompanyName"] != DBNull.Value)
    {
        //set  the Value to Customer Property
        objCustomer.CompanyName = (string)dr["CompanyName"];
    }
    //Get the single City from DataReader
    if (dr["City"] != DBNull.Value)
    {
        //set  the Value to Customer Property
        objCustomer.City = (string)dr["City"];
    }

    return objCustomer;
}

<o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p><o:p>
<o:p>

4.<o:p> Multiple Customers :

If you Want to get Multiple Customer we need to create another one class name as CustomerCollection.Here we inherits the class CollectionBase.You can also use ArrayList or HashTables for Adding CustomerDetails.CollectionBase is the Base class for all Collections thats what we are implemented the CollectionBase Class.

Inside the CustomerCollection Class I wrote one method For getting MultipleCustomers .It returns the CustomerCollection Object.In this Method I am getting datas from DataReader
Object

CollectionBase works by storing any type of object inside private Arraylists, but exposing access to these private collections through methods that
only take a specific type

<o:p>

/// <summary>
/// Class For collection Of Customers
/// </summary>
public class CustomerCollection : CollectionBase
{
    /// <summary>
    /// Method For Adding Customer Values.
    /// </summary>
    /// <param name="Value"></param>
    /// <returns></returns>
    public int Add(Customer Value)
    {
        //List.Add() is from CollectionBase Class.
        return (List.Add(Value));
    }

    /// <summary>
    /// Method For Getting Multiple Values From Customer
    /// </summary>
    /// <returns></returns>
    public CustomerCollection GetMultipleCustomer()
    {
        IDataReader dr = null;
        CustomerCollection objCustomerColl;
        try
        {
            //set The Datas to Datareader
            EntityClass obj = new EntityClass();
            dr = obj.GetDatasFromDataBase();
            //Read the Multiple Rows
            objCustomerColl = new CustomerCollection();
            //Read the Values
            while (dr.Read())
            {
                //Add the values to CustomerCollection
                objCustomerColl.Add(obj.PopulateCustomer(dr));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return objCustomerColl;
    }
}


<o:p>

Conclusion:

I explained the basics only. There is lot more you can do with them. Its always a good idea to use Entity Classes instead of returning datasets and data Readers from the DataAccess Layer. DataSets might work if your application is small but when your application grows in size the use of datasets will create complexity

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
shanawazway7-Nov-11 21:02
shanawazway7-Nov-11 21:02 
QuestionRE: Good Article Pin
vivalt_san3-Aug-11 0:53
vivalt_san3-Aug-11 0:53 
GeneralGood work... Pin
Ra...aj15-Oct-07 23:38
Ra...aj15-Oct-07 23:38 
GeneralRe: Good work... Pin
NormDroid15-Oct-07 23:43
professionalNormDroid15-Oct-07 23:43 
GeneralRe: Good work... Pin
Ra...aj16-Oct-07 1:16
Ra...aj16-Oct-07 1:16 
GeneralRe: Good work... Pin
NormDroid16-Oct-07 1:24
professionalNormDroid16-Oct-07 1:24 
GeneralRe: Good work... Pin
BigTuna16-Oct-07 3:34
BigTuna16-Oct-07 3:34 
GeneralRe: Good work... Pin
NormDroid16-Oct-07 3:46
professionalNormDroid16-Oct-07 3:46 
GeneralRe: Good work... Pin
Dewey16-Oct-07 12:39
Dewey16-Oct-07 12:39 
GeneralRe: Good work... Pin
NormDroid16-Oct-07 20:37
professionalNormDroid16-Oct-07 20:37 
GeneralRe: Good work... [modified] Pin
bilo8117-Feb-09 1:47
bilo8117-Feb-09 1:47 
@Norm,
You should express your disagree about the quality of the article by rating it 1 and explain the reasons of your vote, but without insulting or saying that the article is "crap".
I think it's a matter of respect first of all and of good manner as well.
Regards

modified on Tuesday, February 17, 2009 8:01 AM

GeneralConclusion Pin
NormDroid15-Oct-07 23:23
professionalNormDroid15-Oct-07 23:23 
GeneralRe: Conclusion Pin
justindhas16-Oct-07 1:08
justindhas16-Oct-07 1:08 
GeneralRe: Conclusion Pin
NormDroid16-Oct-07 1:24
professionalNormDroid16-Oct-07 1:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.