For the development of the database middle tier (think ORM and semi-ORM framework)

Source: Internet
Author: User
Tags commit datetime generator rollback

I csdn in the back of the post, feeling sometimes need to tidy up.
The database is frequently manipulated in the project, many of which not only duplicate labor but also increase maintenance costs.
The framework requires the ability to fully use ORM or some of the functions in the framework, to be able to adapt to the object-oriented operation of the database can also adapt to high performance requirements. Finishing the effects you need to achieve

http://community.csdn.net/expert/topicview3.asp?id=5349356
To get out of this tedious work, you can design tools for ORM or use the tools that are already ripe to automate many operations, and the amount of code is reduced.
As an example,
Assume that the user table operation
public class UserInfo
{
public int userid{get; set;}
public string username{get; set;}
public string password{get; set;}
}

Manipulate user table, object is userinfo entity
Here is the fragment
Idatabase database = new SQLDatabase (new Sqlconfing (Server,database,userid,password));
Idataprovider Provider = database. Getdataprovider (TableName);
Provider. Insert (New UserInfo ("sa", "123"));
The entity has been inserted into the database after the above operation.
If the database field differs from the entity class, you can label it, as
public class UserInfo
{
[DataColumn ("user_id")]
public int userid{get; set;}
[DataColumn ("user_name")]
public string username{get; set;}
public string password{get; set;}
}
Stored Procedures
public class UserInfo
{
[DataColumn ("user_id")]
[Dataparameter ("@User_Id")]
public int userid{get; set;}
[DataColumn ("user_name")]
public string username{get; set;}
public string password{get; set;}
}
The above is only if the "@User_ID" parameter differs from the property, the above setting

If you need to manipulate multiple tables, you need a transaction example
public class UserInfo
{
public int userid{get; set;}
public string username{get; set;}
public string password{get; set;}
}

public class Userdetail
{
public int detailid{get; set;}
public int userid{get; set;}
Public DateTime regtime{get; set;}
}
Idatabase database = new SQLDatabase (new Sqlconfing (Server,database,userid,password));
Try
{
Database. Transactionbegin ();
Idataprovider Userprovider = database. Getdataprovider ("User");
UserInfo UserInfo = new UserInfo ("sa", "123");
Userdetail userdetail = new Userdetail (DateTime.Now);
Userprovider.insert (UserInfo);
Userdetail.userid = Userinfo.userid;
Detailprovider = database. Getdataprovider ("Userdetail");
Detailprovider.insert (Userdetail);
Database.commit ();
} catch (Exception ex)
{
Write log
if (database! = NULL)
Database. Rollback ();
}
The above is through the practice, from the cumbersome database operation freed up is the trend.
You can also extend more label descriptions to solve the relationship between multiple tables ORM Automation processing.

http://topic.csdn.net/t/20061119/21/5169703.html
There's a lot of work to be done,
1. First define a standard (interface) to complete all database operations.
2. Information collectors, which can collect entity and database information.
3. The remaining is the realization, for information collection can be varied, different databases can achieve different information collection.

Assume that the user table operation
public class UserInfo
{
public int userid{get; set;}
public string username{get; set;}
public string password{get; set;}
}

Manipulate user table, object is userinfo entity
Here is the fragment
Idatabase database = new SQLDatabase (new Sqlconfing (Server,database,userid,password));
Idataprovider Provider = database. Getdataprovider (TableName);
Provider. Insert (New UserInfo ("sa", "123"));
The entity has been inserted into the database after the above operation.
If the database field differs from the entity class, you can label it, as
public class UserInfo
{
[DataColumn ("user_id")]
public int userid{get; set;}
[DataColumn ("user_name")]
public string username{get; set;}
public string password{get; set;}
}
Stored Procedures
public class UserInfo
{
[DataColumn ("user_id")]
[Dataparameter ("@User_Id")]
public int userid{get; set;}
[DataColumn ("user_name")]
public string username{get; set;}
public string password{get; set;}
}
The above is only if the "@User_ID" parameter differs from the property, the above setting

If you need to manipulate multiple tables, you need a transaction example
public class UserInfo
{
public int userid{get; set;}
public string username{get; set;}
public string password{get; set;}
}

public class Userdetail
{
public int detailid{get; set;}
public int userid{get; set;}
Public DateTime regtime{get; set;}
}
Idatabase database = new SQLDatabase (new Sqlconfing (Server,database,userid,password));
Try
{
Database. Transactionbegin ();
Idataprovider Userprovider = database. Getdataprovider ("User");
UserInfo UserInfo = new UserInfo ("sa", "123");
Userdetail userdetail = new Userdetail (DateTime.Now);
Userprovider.insert (UserInfo);
Userdetail.userid = Userinfo.userid;
Detailprovider = database. Getdataprovider ("Userdetail");
Detailprovider.insert (Userdetail);
Database.commit ();
} catch (Exception ex)
{
Write log
if (database! = NULL)
Database. Rollback ();
}

This can also be used to structure the processing of assignment information.
For queries that can be completed independently of the module, you can collect the entity information (including values and associated information), generate SQL from the SQL generator, and feed the execution results back into the entity.

Not all occasions are used ORM, if you use ORM in query I dare not imagine, "frame" I think is semi-finished, I write and is fully ORM framework, If you combine the functionality you need, you can complete the ORM. When querying and calculating in large numbers, I'm going to give up, but I can use some of the templates in the framework to simplify the query operation, and when customizing the query, you can generate SQL to the SQL generator, because sometimes I don't need a database reflection object, just a table result.
http://community.csdn.net/expert/topicview3.asp?id=5461087
Consider decoupling, because ORM is sometimes necessary when different needs are met, but whether the performance-demanding framework satisfies the automatic ORM and semi-orm.
Cases
public class User
{
[Relation (Typeof (userdetail), Relationtype.onetoone)]
Public Userdetail detail{get; set;}
.....
}

public class Userdetail
{
....
}

User user = New User (..., new userdetail (..));
Isqlsimpleinsertbuilder Sqlinsertbuilder = new Sqlserversimpleinsertbuildersqlbuidler ();
String Sqlinsert = sqlinsertbuilder.parse (user);
Database. Execute (Sqlinsert);

Isqlsimpleselectbuilder Sqlselectbuilder = new Sqlserversimpleselectbuildersqlbuilder ();
String sqlselect = Sqlselectbuilder.parse (Conditionstype.custom, "User.userid=userdetial.userid and UserDetial.Age >30 ");
DataSet ds = database. Select (Sqlselect);
If you want to ORM, in the above schema layer to map the results, for the sqlbuilder there is a local dissatisfaction can be completely reconstructed.

Isqlcomplexbuilder sqlbuilder=new Sqlservercomplexsqlbuidler ();
StringCollection sqls=sqlbuilder.insertparse (user);

Oracle Database
Idatabase database = new Oracledatabase (oracleconfig);
User user = New User (..., new userdetail (..));
Idataprovider Userprovider = database. Getdataprovider (user);
Userprovider.insert (user);
User and userdetail the appropriate information into Oracle.
In the future if you encounter a database that you do not know, you can implement according to the framework, unable to determine their own framework (less interface, write more abstract class, small function Class) to a certain time frame slowly floating up.

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.