Moon. ORM
I. Introduction
This article focuses on the discussion of Moon. ORM technology and its usage instructions. If you have any other questions, please leave a message. This article will analyze Moon. ORM from the actual situation (Technical Group:
216965349
)
Entity FrameworkNot long ago, Microsoft opened EF5.0 open source, and obtained its code and found that it could not be compiled. It roughly looked at its Code. Its elegance and convenience are Orthodox, but its actual combat nature remains to be improved.
1) Linq cannot generate the ideal SQL for us, and its own performance may be compromised. This is the main problem EF has to face. There is no perfect thing but a balance point-no silver bullet
2) linq cannot solve queries with high complexity. Microsoft attempted to ignore the existence of the database at the beginning, but this is absolutely impossible.
3) multi-database and multi-data source problems are difficult to solve;
4) Only. net 4.5 is supported and cannot be run on windows 2003.
EF has many advantages. the advantages and disadvantages of Linq seem to have become one of EF's advantages and disadvantages. microsoft wants to use Linq to let us think about and persist their data in a data-oriented way. but we can think about the records in the database, isn't it an object? I think this is an obvious advantage for the elegance and convenience of a user, and then the important role that anonymous objects play in it.
...
Ii. features and advantages of Moon. ORM
---- All wisdom is simple, but not known. It is also the main feature of Moon. ORM: the greatest truths to simplicity.
1. high performance is Moon. one of the advantages of ORM is also one of the main purposes of my architecture. As I said before, it was designed to compensate for performance problems encountered in the project. it can be said that the entire framework data processing adopts the pure ADO. NET is encapsulated and combined with EMIT to quickly generate entities (of course, you can also use 4.0 of the Code Generator to complete pure ADO. NET ). I have to admit the elegance brought by the linq and lambda statements, but at the same time, we need to acknowledge the limitations of linq. some people may say that they can make up for it by means. For example, some people write articles to Improve the Performance of linq, but we need to acknowledge the two facts that they can be optimized only after they identify the linq system. That is to say, the nature of linq determines performance loss. again, linq is not a silver bullet, because it is almost impossible for the responsible scenario to do so. What's more, the SQL statements generated by linq are not necessarily what you really want. (Note: I am not a competitor to linq, but to tell the truth, as I once said: in actual development, there is no silver bullet, and there is only a balance point. It is enough for an architecture that can meet the needs to solve the actual situation) in addition, I do not need to write another framework to create something similar to nhib.pdf or an Entity Framework. I always think that we need to develop a unique feature and advantage.
2. ease of use. I think Moon. ORM should be used to know this. It is easy to configure, SMART awareness, and code generator assistance. Moon can be used for SQL.
3. supports multiple databases and multiple data sources. moon. ORM is your best choice. for example, if your system uses MSSQL by default and you want to use MYSQL at the same time, you only need to instantiate an engine. DBFactory. getEntity (Pjy_AdminRoleTable.RoleID.BiggerThan (0), new MYSQL ("connection string"); of course, you can make the engine global.
4. Syntax sugar function. The result of my personal use is about 70% of my actual needs.
5. native support for. NET 2.0.
6. database transformation problems, if you find that one day your database needs to be changed from mysql to mssql, you only need to change your configuration file. (Of course, you need to pay attention to the SQL syntax difference, if you are using native SQL for operations ).
Iii. maintenance problems.
Someone once said, 'No source code, so I can't use'. I can't beat Microsoft, but we can change to another location: Do you know the bug in. net framework 3.5 sp1? Is there any bug message in Microsoft's library? Moon. ORM Standard Edition, free of charge (including API documentation) and group technical support. for enterprise users, I will provide specialized services and technical support as well as a more elegant and powerful Enterprise Edition Moon. orm code generator tools and technical training materials.
Iv. Comparison of similar products.
Http://www.cnblogs.com/humble/archive/2012/05/20/2510756.html
V. Absolute performance advantages
After Version 3.9, the query performance has been improved to the ultimate performance of pure SQL queries.
Vi. Technical Guidance (the majority of cases are provided for reference. For details, refer to the API Documentation)
I. query operations.
1. query a record (an entity)
Administrator newUser = DBFactory. GetEntity (AdministratorTable. ID. Equal (userID). And (AdministratorTable
. Age. Equal (12 )));
2. query multiple records (multiple entities)
Var list = DBFactory. GetEntities (AdministratorTable. ID. BiggerThan (9 ));
3. nested query (codeID acts as the ID of the foreign key pointing to the Administrator)
Var list = DBFactory. GetEntities (AdministratorTable. ID
. Equal (AttachmentTable. codeID. SelectWhere (AttachmentTable. fileName. Equal ("ee.txt "))));
4. For complex multi-table queries, use the code generator to copy your SQL statement, and then it helps you generate entities.
NewOBJ newUser = DBFactory. GetEntity (SQL statement );
5. Single Data Query.
String userName = DBFactory. GetOneField <string> (AdministratorTable. UserName, AdministratorTable. ID. BiggerThan (0 ));
(Note that an exception may be reported. For example, the data found is DBNull ).
6. query the number of records.
Long count = DBFactory. GetCount (AdministratorTable. UserName, AdministratorTable. ID. BiggerThan (0 ));
7. Return other formats. You can refer to the API to view the database
(Call methods such as DBFactory. DefaultDB. GetDataSet ("SQL statement");) DB ultdb is of the type DB, and its API can be found in the help documentation;
8. query a single field. var userName = DBFactory. getOneField <string> (UserTable. userName, UserTable. age. equal (12 ). and (UserTable. tel. equal ("qsmy ")));
9. query the specified field object.
Var user = DBFactory. GetEntity (UserTable. Age + UserTable. UserName, UserTable. ID. Equal (12 ));
10. The complex query function. Moon provides three solutions.
1. The code generator generates an entity and puts your SQL statement (no matter how complicated) into the code generator. It will automatically generate a strongly typed entity for you, as described in article 4th.
2. intelligent entities, without code generators, are as follows:
Var list = DBFactory. GetIntelligentEntities ("select * from [User]");
Console. WriteLine (list [index] ["username"]. To <string> (); (Note that all fields are in lower case)
Var user = DBFactory. GetIntelligentEntity ("select * from [User] where age> 3 ");
Console. WriteLine (user ["username"]. To <string> ());
3. dynamically compile entities without the need for code generators (Moon5.0 will meet you)
Var list = DBFactory. GetAutoEntities ("a fairly complex SQL query statement", "Custom class name ");
For (var a in list ){
Int count = a. Count;
String country = a. Country;
}
2. Add data. Administrator admin = new Administrator ();
Admin. UserName = "Qin ShiChuan" + DateTime. Now;
Admin. Password = "qsmy ";
// Enable transaction operations
Admin. StartTransaction (true );
// Return the primary key value
Int userID = Convert. ToInt32 (DBFactory. Add (admin); 3. data deletion operation. // return the number of deleted items.
Long count = DBFactory. DeleteWhen (AdministratorTable. ID. BiggerThan (0); 4. data modification operation. Administrator admin = new Administrator ();
Admin. UserName = "Qin ShiChuan" + DateTime. Now;
Admin. Password = "qsmy ";
Admin. SetOnlyMark (AdministratorTable. ID. BiggerThan (0 ));
// Enable transaction operations
Admin. StartTransaction (true );
// Return the primary key value
DBFactory. Update (admin );
7. Moon. ORM design prerequisites that need to be understood
1. For database design, each table must have a primary key;
2. The primary key design scheme determined by the business logic is incorrect, so the primary key cannot be restrained by the business, because the business is changed. Moon. ORM needs to be established independently of the business.
Because the primary key design MOON selects guid or auto-increment (we recommend using auto-increment ).
8. Instructions for use
Instructions for use: Code Generator and instructions for use http://www.cnblogs.com/humble/archive/2012/05/20/2510756.html
1. Download the Code Generator (download the latest version above)
2. modify the configuration file of the Code Generator, as shown in. (For mysql, see section 6.Instructions for use)
3. Run the code generator to generate an object