CRUD (Create, Read, Update, Delete) Operation With Generic List






4.88/5 (22 votes)
CRUD (Create, Read, Update, Delete) Operation With Generic List
Introduction
Data Table in conjunction with dataset is a very common option for CRUD (Create, Read, Update and Delete) operation towards database interaction.This is true that data table is developer friendly and provides cool features but at the same time this is heavy, resource hungry and not object oriented. As a result, we can think of Generic List LIST<T>
as an introductory alternative of datatable and moving forward we will try to create a framework to provide the above CRUD functionality.
See below for the screen developed with the above framework.
A generic list is useful for creating a collection of similar objects. Generic List<T>
where T
is the Business Object / Business Entity / etc., which can be further strongly typed.
For example, List<Employee>
will allow only Employee
instance to be added to the list.
In the following framework, we have added a custom wrapper on the top of List<T>
for better readability.
public class EntityCollection<T> : List<T>{}
EntityCollection<T>
where T
refers to Business Entity/Object. Following is the way to insert as many entities into collection.
EntityCollection<userbo> objUserCollection = new EntityCollection<userbo>();
objUserCollection.Add(new UserBO (……));
objUserCollection.Add(new UserBO (……));
EntityCollection<cusbo> objCustCollection = new EntityCollection<cusbo>();
objUserCollection.Add(new CusBO (……));
objUserCollection.Add(new CusBO (……));
Implementing Search Feature
“FindByExpression
” custom method of EntityCollection
provides facility to filter data depending upon the expression passed as parameter. Expression should be given in “Property=Value
” format.
EntityCollection<userbo> userCollection = UserData(); // All users
userCollection = userCollection.FindByExpression(“Name=Jhon”);
EntityCollection<cusbo> objCustCollection = CustomerData();// All Customers
objCustCollection = objCustCollection.FindByExpression(“CustId=3451”);
Let’s see the underlying logic of method “FindByExpression
”:
public static EntityCollection<t> FindByExpression<t>
(this EntityCollection<t> EntityCollection,string FindExpression)
{
Expression objExpression = GetPropertyInfo(EntityCollection, FindExpression, '=');
// Find all matches -- Lambda Expression
IEnumerable<t> objEntityList = EntityCollection.FindAll
(objC => objExpression.PropertyInfo.GetValue(objC, null).ToString()
== objExpression.ExpressionPropertyVal);
EntityCollection<t> objEntities = new EntityCollection<t>();
// Create a new entity collection with matched item
foreach (var objEntity in objEntityList) objEntities.Add(objEntity);
return objEntities;
}
The getPropertyInfo()
method returns metadata of find expression property and the value to be searched wrapped together in a class.
Afterwards, we are using FindAll
method of generic LIST
with lambda expression to find all the matches.
Implementing Sort Features
“SortByExpression
” custom method of EntityCollection
provides the facility to sort data depending upon the expression passed as parameter. Expression should be given in “Property Desc/Asc” format.
EntityCollection<userbo> userCollection = UserData(); // All users
userCollection = userCollection.SortByExpression (“Name”); // Asc order
EntityCollection<cusbo> objCustCollection = CustomerData(); // All Customers
objCustCollection = objCustCollection. SortByExpression (“CustId Desc”);
Let’s understand the logic of “SortByExpression
”:
public static EntityCollection<t> SortByExpression
<t>(this EntityCollection<t> EntityCollection,string SortExpression)
{
bool blnDescending = false;
IEnumerable objEntityList;
Expression objExpression = GetPropertyInfo(EntityCollection, SortExpression, ' ');
blnDescending = SortExpression.ToUpper().Contains("DESC");
// Sort in Desc order
if (blnDescending) objEntityList =
EntityCollection.OrderByDescending
(objX =>objExpression.PropertyInfo.GetValue(objX, null));
// Sort in ASC order
else objEntityList = EntityCollection.OrderBy
(objX => objExpression.PropertyInfo.GetValue(objX, null));
EntityCollection<t> objEntities = new EntityCollection<t>();
// Create a new entity collection with sorted items
foreach (var objEntity in objEntityList) objEntities.Add(objEntity);
return objEntities;
}
We are taking help of OrderBy
/OrderByDescending
method of generic LIST<t>
sort order.
Implementing Delete Feature
“DeleteByExpression
” custom method of EntityCollection
provides the facility to remove entity from collection depending upon the matches. Expression should be given in “Property=Value
” format.
EntityCollection<userbo> userCollection = UserData(); // All users
userCollection = userCollection.FindByExpression(“Name=Jhon”);
Let’s understand the logic of “DeleteByExpression
”:
public static EntityCollection<t> DeleteByExpression<t>
(this EntityCollection<t> EntityCollection,string FindExpression)
{
Expression objExpression = GetPropertyInfo(EntityCollection, FindExpression, '=');
// Find all matches -- Lamda Expression
IEnumerable<t> objEntityList = EntityCollection.FindAll
(objC =>objExpression.PropertyInfo.GetValue(objC, null).ToString()
== objExpression.ExpressionPropertyVal);
// Remove the matches from entity list
foreach (var objEntity in objEntityList) EntityCollection.Remove(objEntity);
return EntityCollection;
}
We are using FindAll
method of generic LIST
and eliminating the matches.
History
- 27th May, 2010: Initial post