Grove-based. NET application development Tip 2

Source: Internet
Author: User

Grove-based. NET application development tips
 

An example of how to handle a primary key for a database table if it is not auto-incrementing using Grove is described in the previous article.

7/13/423644. aspx.
Some common operations on objects when using Grove. It is nothing more than a data entity class for each entity class

Namespace jadesoft. Logistics. systeminfo
{
Using system;
Using Grove. Orm;
Using jadesoft. Common;
[Datatable ("sdepartment")]
Public class Department {
Guid _ OID;
String _ deptid = "";

// The OID attribute indicates that it is the oId column of the primary key column corresponding to the database, and the oId column does not automatically grow.
[Keyfield ("oid", keytype = uniqueidtype. otherdefinition)]
Public guid OID
{
Get {return this. _ OID ;}
Set
{
This. _ OID = value;
}
}
[Datafield ("deptid")]
Public String deptid
{
Get {
Return this. _ deptid;
}
Set {
This. _ deptid = value;
}
}

}
}

The basic framework of the Data Pipeline class is as follows:
// Process Department Data Access

Using system;
Using system. Data;
Using jadesoft. Logistics. systeminfo;
Using Grove. Orm;
Namespace jadesoft. Logistics. systeminfo. dal
{
/// <Summary>
/// Deptdataaccess abstract description.
/// </Summary>
Public class deptdataaccess
{

Public deptdataaccess ()
{
//
// Todo: add the constructor logic here
//
}

/// <Summary>
/// Add a department record
/// </Summary>
/// <Param name = "Dept"> Department object </param>
Public void addnewdept (Department Dept)
{
If (null = Dept)
{
Throw new argumentnullexception ("the object cannot be blank! ");
}
Try
{
Objectoperator objopt = new objectoperator ();
Objopt. insert (Dept );
}
Catch
{
Throw;
}
}

// Other data processing, such as Delete. Omitted
}
}

The key is. There are many tables in the database. Each table corresponds to such a Data Partition class, isn't it a little impractical. Can we use a public interface to implement all object pairs?

Data access rules. If you have such an idea, you can quickly turn it into an implementation. (On the way home by bus, I have been thinking about this problem. I got home from my company.

It takes an hour and a half to finish dinner after going home. In a few minutes, open the computer and start to write code ).

Here we can define an interface so that all entity classes can implement this interface.
Using system;

Namespace jadesoft. Common
{
/// <Summary>
/// Defines object class interfaces. All object classes in the system must comply with this standard.
/// </Summary>
Public interface ientity
{
// I set all primary keys to guid type,
System. guid OID
{
Get;
Set;
}

/// <Summary>
/// This attribute identifies whether the object has been persisted.
/// </Summary>
Entitystatus ispersist {Get; set ;}

/// <Summary>
/// This method provides a way to verify whether the data in the object is valid. This method is required for each data entity class. However, in this method, only

The row checks the validity of its own data.
/// The recommendation for verifying the validity of multiple instances is not included in this method and should be placed in the corresponding control class.
/// </Summary>
/// <Returns> check whether the result is valid </returns>
Bool validate ();
}

/// <Summary>
/// Indicates the current status of a data entity
/// </Summary>
Public Enum entitystatus
{
/// <Summary>
/// Indicates that it does not exist in the Database Table
/// </Summary>
New = 0,

/// <Summary>
/// Marked as needing to be updated to the database
/// </Summary>
Update = 1,

/// <Summary>
/// Marked as deleted from the database
/// </Summary>
Delete = 2,

/// <Summary>
/// The table and the database are unchanged.
/// </Summary>
Persisted = 3

}
}

Then let the object class implement this interface
Namespace jadesoft. Logistics. systeminfo
{
Using system;
Using Grove. Orm;
Using jadesoft. Common;
[Datatable ("sdepartment")]
Public class Department: jadesoft. Common. ientity
{
Guid _ OID;
String _ deptid = "";

[Keyfield ("oid", keytype = uniqueidtype. otherdefinition)]
Public guid OID
{
Get {return this. _ OID ;}
Set
{
This. _ OID = value;
This. _ entitystatus = entitystatus. Update; // data change, which must be saved to the database
}
}
[Datafield ("deptid", size = 30)]
Public String deptid
{
Get {
Return this. _ deptid;
}
Set {
This. _ deptid = value;
This. _ entitystatus = entitystatus. Update; // data change, which must be saved to the database
}
}

 
# Region ientity Member

Public bool validate ()
{
If (this. _ deptid. Length = 0)
{
Return false;
}
If (this. _ deptname. Length = 0)
{
Return false;
}
Return true;

}
// Object status
Private entitystatus _ entitystatus = entitystatus. New;
Public jadesoft. Common. entitystatus ispersist
{
Get
{
// Todo: Add department. ispersist getter implementation
Return _ entitystatus;
}
Set
{
_ Entitystatus = value;
}
}

# Endregion


}
}

Below I can write a public database category class for accessing data of all entities.

Using system;
Using Grove. Orm;
Using system. Data;
Using jadesoft. Common;
Using system. collections;
Namespace jadesoft. Logistics. systeminfo. dal
{
/// <Summary>
/// Common processing of general object. Add, delete, modify, and query Functions
/// </Summary>
Public class commondataaccess
{
Objectoperator objopt = new objectoperator ();
Public commondataaccess ()
{
//
// Todo: add the constructor logic here
//
}


Public void updateobject (ientity entity)
{
If (entity = NULL)
{
Throw new argumentnullexception ("the object cannot be blank! ");
}
Try
{
// The object is generated.
If (entity. ispersist = entitystatus. New)
{
Objopt. insert (entity );
Entity. ispersist = entitystatus. persisted; // object status after update
}
Else if (entity. ispersist = entitystatus. Update)
{
Objopt. Update (entity );
}
Else if (entity. ispersist = entitystatus. delete)
{
Objopt. Remove (entity );
}
}
Catch (exception ex)
{
Throw ex;
}
}

Public ientity retrieve (type, guid OID)
{
Return (ientity) objopt. Retrieve (type, OID );
}

/// <Summary>
/// Query all objects based on the desired type.
/// </Summary>
/// <Param name = "type"> required type, business entity </param>
/// <Returns> Object array </returns>
Public arraylist retrieves (type)
{
Objectquery oquery = new objectquery (type );
Return objopt. getobjectset (oquery );
}
/// <Summary>
/// Query all objects based on the desired type.
/// </Summary>
/// <Param name = "type"> required type, business entity </param>
/// <Param name = "filterexpression"> the filter expression is the same as the WHERE clause in SQL. For example, this. Username = 'greystar' </param>
/// <Returns> Object array </returns>
Public arraylist retrieves (type, string filterexpression)
{
Objectquery oquery = new objectquery (type, filterexpression );
Return objopt. getobjectset (oquery );
}
/// <Summary>
/// Query all objects based on the desired type.
/// </Summary>
/// <Param name = "type"> required type, business entity </param>
/// <Param name = "filterexpression"> the filter expression is the same as the WHERE clause in SQL. For example, this. Username = 'greystar' </param>
/// <Returns> DataSet object </returns> <
Public dataset retrievedataset (type, string filterexpression)
{
Objectquery oquery = new objectquery (type, filterexpression );
Return (Dataset) objopt. getobjectsource (oquery );
}

/// <Summary>
/// Query all objects based on the desired type.
/// </Summary>
/// <Param name = "type"> required type, business entity </param>
/// <Returns> DataSet object </returns>
Public dataset retrievedataset (type)
{
Objectquery oquery = new objectquery (type );
Return (Dataset) objopt. getobjectsource (oquery );
}
}
}

Next, let's try it. Can this be done. Can an objectoperator object identify the original type of an ientity object during data operations.
We can test it with code.
Commondataaccess CDA = new commondataaccess ();
Department DPT = new Department ();
DPT. OID = system. guid. newguid ();
DPT. deptid = "greystar ";

CDA. updateobject (DPT); // Save the object
 
The code runs normally. The database operation is successful. In this case, I only need to implement the ientity interface for each object class. You don't need to write any data access code.
Is this idea good.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.