When using Entity Framework, it helps us to process the data processing layer. At the business layer, we also have some common operations on entities, for example, for adding, deleting, and modifying entities, most of these three operations are the same. The following code is a common method entity at the business processing layer. If there is a separate processing, the child business processing class can inherit the business parent class to implement its own functions.
The following defines the business parent class. There are three public methods: Add, Remove, Mofify, and a private method, GetEntitySetName. This function is used to Add a common object class, the AddObject square of the database object is called. The first parameter of the AddObject requires an object set name. The name can be obtained through the GetEntitySetName method. This method requires an object to be added as a parameter.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data. Objects. DataClasses;
Using System. Data. Mapping;
Using System. Data. Metadata. Edm;
Using System. Data. EntityClient;
Using System. Reflection;
Using System. Collections;
Class BllEntity
{
// Database object
Protected TSMS_DBEntities tsms = new TSMS_DBEntities ();
/// <Summary>
/// Obtain the name of the object and data table from the ing File
/// </Summary>
/// <Param name = "entityname"> entity name </param>
/// <Returns> data table name </returns>
String GetEntitySetName (EntityObject entity)
{
Type entitytype = entity. GetType (); // obtain the object Type
String entityname = entitytype. Name; // obtain the Object Name
// Construct the connection object
EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder (System. Configuration. ConfigurationManager. ConnectionStrings ["TSMS_DBEntities"]. ConnectionString );
String [] arrstr = ecsb. Metadata. Split ('|'); // obtain the ing File
EdmItemCollection e = new EdmItemCollection (arrstr [0]); // obtain the CSDL
StoreItemCollection s = new StoreItemCollection (arrstr [1]); // get SSDL
StorageMappingItemCollection smt = new StorageMappingItemCollection (e, s, arrstr [2]); // obtain MSL
// Obtain the ing between the combined object
Var entities = smt [0]. GetType (). GetProperty ("EntitySetMaps", BindingFlags. NonPublic | BindingFlags. Instance). GetValue (smt [0], null );
Foreach (var entityvalue in (IList) entities)
{
// Obtain the StorageSetMapping type through reflection.
Assembly ass = Assembly. Load ("System. Data. Entity, Version = 3.5.0.0, culture = Neutral, PublicKeyToken = b77a5c561934e089 ");
Type type = ass. GetType ("System. Data. Mapping. StorageSetMapping ");
EntitySet es = (EntitySet) type. GetField ("m_extent", BindingFlags. NonPublic | BindingFlags. Instance). GetValue (entityvalue );
If (es. ElementType. Name = entityname)
{
Return es. Name;
}
}
Return null;
}
/// <Summary>
/// Add object
/// </Summary>
/// <Param name = "entity"> entity </param>
/// <Returns> </returns>
Public bool Add (EntityObject entity)
{
Try
{
String entitysetname = GetEntitySetName (entity );
If (entitysetname! = Null)
{
Tsms. AddObject (entitysetname, entity );
Tsms. SaveChanges ();
Return true;
}
Else
{
Throw new BllException ("An error occurred while obtaining the database entity table! ");
}
}
Catch (Exception exc)
{
Throw exc;
}
}
/// <Summary>
/// Delete an object
/// </Summary>
/// <Param name = "entity"> entity </param>
/// <Returns> </returns>
Public bool Remove (EntityObject entity)
{
Try
{
Tsms. DeleteObject (tsms. GetObjectByKey (entity. EntityKey ));
Tsms. SaveChanges ();
Return true;
}
Catch (Exception exc)
{
Throw exc;
}
}
/// <Summary>
/// Modify an object
/// </Summary>
/// <Param name = "entity"> Object </param>
/// <Returns> </returns>
Public bool Modify (EntityObject entity)
{
Try
{
Object oldentity = tsms. GetObjectByKey (entity. EntityKey );
EntityValueTransform. Transform (EntityObject) oldentity, entity );
Tsms. SaveChanges ();
Return true;
}
Catch (Exception exc)
{
Throw exc;
}
}
Because the query results are strange, the query is not put into this class. The personalized function is implemented in the subclass as follows:
Class BllGrade: BllEntity
{
/// <Summary>
/// Obtain the score View list
/// </Summary>
/// <Returns> score Entity List </returns>
Public List <V_Grades> GetGrades ()
{
Return tsms. V_Grades.ToList ();
}
/// <Summary>
/// Obtain the score by number
/// </Summary>
/// <Param name = "ID"> Unique ID </param>
/// <Returns> score entity </returns>
Public Grade GetGradeByID (int ID)
{
Return tsms. Grades. SingleOrDefault (grade => grade. ID = ID );
}
}
Author: "Gui suwei"