Entity Classes
Introduction:
In the last article we saw the
differences between the dataset and typed datasets. Typed datasets are more
flexible than using dataset. In this article I will explain the entity classes
which is far superior than datasets and typed datasets.
Why using Entity Classes:
The problem using the typed datasets was
that when the systems grows so does the complicity of using typed datasets.
DataSets are also slow when passed between layers of application. As you might
already know that dataset is a collection of many tables and hence consumes
more memory.
Entity classes provides more flexibility
since they access the data using special methods defined in the class library.
You can also use different patterns when using Entity Classes.
There are different ways for dealing with
entity classes. The most advance is by the use of OR/Mappers which are third
party components. In this article I will just explain the basics of entity
classes and how you can retrieve data using them.
Let's first make a Person class which
contains private field and those fields are accessed through properties.
public
class
Person
{
private
int _personId
= 0;
private
string _email =
null;
private
DateTime _dateCreated;
private
string _password =
null;
public
int PersonID
{
get
{
return
_personId;
}
}
public
string
Email
{
get
{
return
_email;
}
set
{
_email = value;
}
}
public
DateTime DateCreated
{
get
{
return
_dateCreated;
}
set
{
_dateCreated = value;
}
}
public
string
Password
{
get
{
return
_password;
}
set
{
_password = value;
}
}
|
All the properties have get and set assessors except
the PersonID property which is read only. Now we will make the GetSingleUser
class which will return us a single User. You should implement this
functionality by sending some key to the database and searching for a single
user. I have just moved the data reader forward and returned the first row.
| // You should send the
key as a parameter to get the Single user
public
Person GetSingleUser()
{
Database db =
DatabaseFactory.CreateDatabase();
DBCommandWrapper selectCommandWrapper =
db.GetSqlStringCommandWrapper("SELECT PersonID,Email,Password,DateCreated FROM
Person");
IDataReader dr =
db.ExecuteReader(selectCommandWrapper);
// moving the data reader forward
dr.Read();
return
PopulateUser(dr);
}
|
As you can read my comments about the method above.
You should send some parameter to the GetSingleUser method and select the User
on the basis of those parameter. The method calls the PopulateUser method which
returns the Person object.
public
Person PopulateUser(IDataRecord dr)
{
Person person = new
Person();
if(dr["Email"]
!= DBNull.Value)
{
person.Email = ( string)
dr["Email"];
}
return
person;
}
|
The PopulateUser method simply checks that if the
"Email" field is not null and if its not null adds it to the person object and
returns the person object to the calling program.
Now let's see how we can return collection of objects
each representing a different user. First we need to create a
collection that can hold multiple instances of the Person class. For
this we will create a collection which inherits from CollectionBase. You
can also use ArrayList or HashTables but since CollectionBase is the
base class for all the collections that's why we are implementing it.
public class
PersonCollection : CollectionBase
{
public
Person this[int
index]
{
get
{ return
(Person) List[index]; }
set
{ List[index] = value;
}
}
public
int
Add(Person value)
{
return
(List.Add(value));
}
} |
In the above code we defined certain
properties which are exposed by our PersonCollection class. Now let's
put instances of Person class into the collection and return the
collection to the user.
public
PersonCollection GetPersons()
{
Database db =
DatabaseFactory.CreateDatabase();
DBCommandWrapper selectCommandWrapper =
db.GetSqlStringCommandWrapper("SELECT
PersonID,Email,Password,DateCreated FROM Person");
IDataReader dr =
db.ExecuteReader(selectCommandWrapper);
PersonCollection personCollection =
new
PersonCollection();
while(dr.Read())
{
personCollection.Add(PopulateUser(dr));
}
return
personCollection;
} |
Now we have already seen the
implementation of the PopulateUser method which returns the Person
object. We are simply adding all the returned objects to the
PersonCollection and finally returning the collection. You might have
noticed that I am using Enterprise Library for Data Access you can use
any method you want.
Binding the collection to the
presentation layer is also very simple. You can simply use the property
name to bind the columns with any control on the grid.
In this article I explained the basics
of entity classes. There is lot more you can do with them. Its always a
good idea to use Entity Classes instead of returning datasets and
datareaders from the database layer. DataSets whether not typed or
strongly typed might work if you application is small but when your
application grows in size the use of datasets will create complexity.
Please also see the sample code
attached with this article.
Attachments:
Sample Files: EntityClassDbperson.zip