使用泛型列表进行 CRUD(创建、读取、更新、删除)操作






4.88/5 (22投票s)
使用泛型列表进行 CRUD(创建、读取、更新、删除)操作
引言
DataTable 结合 DataSet 是与数据库交互进行 CRUD(创建、读取、更新和删除)操作的常见选择。诚然,DataTable 对开发者友好,并提供很棒的功能,但同时它也很庞大、资源密集且非面向对象。因此,我们可以将 Generic List LIST<T>
视为 DataTable 的一个入门级替代方案,并进一步尝试创建一个框架来提供上述 CRUD 功能。
以下是使用上述框架开发的屏幕截图。

泛型列表对于创建相似对象的集合非常有用。Generic List<T>
,其中 T
是业务对象/业务实体/等,可以进一步进行强类型化。
例如,List<Employee>
将只允许添加 Employee
实例到列表中。
在以下框架中,我们添加了一个自定义包装器,位于 List<T>
之上,以提高可读性。
public class EntityCollection<T> : List<T>{}
EntityCollection<T>
,其中 T
引用业务实体/对象。以下是将多个实体插入集合的方法。
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 (……));
实现搜索功能
EntityCollection
的自定义方法“FindByExpression
”提供了根据作为参数传递的表达式过滤数据的能力。表达式应采用“Property=Value
”格式提供。
EntityCollection<userbo> userCollection = UserData(); // All users
userCollection = userCollection.FindByExpression(“Name=Jhon”);
EntityCollection<cusbo> objCustCollection = CustomerData();// All Customers
objCustCollection = objCustCollection.FindByExpression(“CustId=3451”);
让我们看看方法“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;
}
getPropertyInfo()
方法返回查找表达式属性的元数据以及要搜索的值,并将它们包装在一个类中。
之后,我们使用泛型 LIST
的 FindAll
方法和 lambda 表达式来查找所有匹配项。
实现排序功能
EntityCollection
的自定义方法“SortByExpression
”提供了根据作为参数传递的表达式对数据进行排序的能力。表达式应采用“Property Desc/Asc”格式提供。
EntityCollection<userbo> userCollection = UserData(); // All users
userCollection = userCollection.SortByExpression (“Name”); // Asc order
EntityCollection<cusbo> objCustCollection = CustomerData(); // All Customers
objCustCollection = objCustCollection. SortByExpression (“CustId Desc”);
让我们了解“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;
}
我们利用泛型 LIST<t>
的 OrderBy
/OrderByDescending
方法进行排序。
实现删除功能
EntityCollection
的自定义方法“DeleteByExpression
”提供了根据匹配项从集合中删除实体的能力。表达式应采用“Property=Value
”格式提供。
EntityCollection<userbo> userCollection = UserData(); // All users
userCollection = userCollection.FindByExpression(“Name=Jhon”);
让我们了解“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;
}
我们使用泛型 LIST
的 FindAll
方法并消除匹配项。
历史
- 2010年5月27日:初始发布