Farseer.net lightweight open source framework intermediate: DbFactory data Factory

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

Farseer.net lightweight open source framework intermediate: DbFactory data Factory
Of course, there are also some binding techniques, such as binding enumeration and List <Users> to DorpDownList, CheckBoxList, RadioButtonList, and displaying Chinese in the next article for further explanation. In this article, we will mainly explain a factory pattern class used: DbFactory. International Convention. Let's take a look at the methods of this class: Full name space, located at: FS. core. bean copy code 1 namespace FS. core. bean 2 {3 // <summary> 4 // database factory mode 5 // </summary> 6 public class DbFactory 7 {8 // <summary> 9/ // create database SQL to generate 10 /// </summary> 11 /// <typeparam name = "TInfo"> entity class </typeparam> 12 /// <param name = "dbType"> Database Type </param> 13 // <param name = "tableName"> table name </param> 14 internal static DbBuilder <TInfo> CreateDbBuilder <TInfo> (string tableN Ame = "") where TInfo: ModelInfo, new (); 15 16 /// <summary> 17 // create a database Expression Tree parser 18 /// </summary> 19 /// <typeparam name = "TInfo"> entity class </typeparam> 20 // <param name = "dbType"> Database Type </param> 21 /// <param name = "tableName"> table name </param> 22 internal static DbVisit <TInfo> CreateDbVisit <TInfo> () where TInfo: ModelInfo, new (); 23 24 /// <summary> 25 /// create and modify a Database Class 26 /// </summary> 27 /// <param name = "dbType"> Database Type </ p Aram> 28 // <param name = "connetionString"> connection string </param> 29 // <param name = "tableName"> name of the table to be operated </param> 30 public static DbOperate CreateDbOperate (DataBaseType dbType, string connetionString, string tableName); 31 32 // <summary> 33 // create a database provider 34 /// </summary> 35 public static DbProvider CreateDbProvider <TInfo> () where TInfo: ModelInfo, new (); 36 37 // <summary> 38 // create a database provider 39 /// </summary> 40 /// <Param name = "dbType"> Database Type </param> 41 public static DbProvider CreateDbProvider <TInfo> (DataBaseType dbType ); 42 43 // <summary> 44 // database creation operation 45 // </summary> 46 // <typeparam name = "TInfo"> entity class </typeparam> 47 // <param name = "tranLevel"> enable transaction level </param> 48 public static DbExecutor CreateDbExecutor <TInfo> (IsolationLevel tranLevel = IsolationLevel. serializable) where TInfo: ModelInfo, new (); 49 50 // <su Mmary> 51 // database creation operation 52 /// </summary> 53 // <typeparam name = "TInfo"> entity class </typeparam> 54 // <param name = "tranLevel"> enable transaction level </param> 55 public static DbExecutor CreateDbExecutor (int dbIndex = 0, isolationLevel tranLevel = IsolationLevel. unspecified ); 56 57 58 // <summary> 59 // create a database connection string 60 // </summary> 61 // <param name = "dbInfo"> Database Configuration </param> 62 public static string CreateConnString (int dbIndex = 0 ); 63 64 // <summary> 65 // create a database connection string 66 // </summary> 67 // <param name = "dataType"> Database Type </ param> 68 // <param name = "userID"> account </param> 69 // <param name = "passWord"> passWord </param> 70 /// <param name = "server"> server address </param> 71 // <param name = "catalog"> table name </param> 72 /// <param name = "dataVer"> database version </param> 73 // <param name = "connectTimeout"> connection timeout </param> 74 // <param name = "poolMinSize"> Minimum number of connection pools </param> 75 /// <Param name = "poolMaxSize"> maximum number of connection pools </param> 76 // <param name = "port"> port </param> 77 public static string CreateConnString (DataBaseType dataType, string userID, string passWord, string server, string catalog, string dataVer, int connectTimeout = 60, int poolMinSize = 16, int poolMaxSize = 100, string port = ""); 78} 79} copying the code is actually mainly a method call, which is very simple. For a special article, we mainly consider that this class is still quite common. So I wrote this article to help you better understand it. The above methods are roughly divided into six types: CreateDbBuilderCreateDbVisitCreateDbOperateCreateDbProviderCreateDbExecutorCreateConnString. There are some overloaded versions. The Heavy Load version mainly refers to the parameters configured for the database. You can select a database by writing it manually, using entity class ing, or configuring the database. When talking about database configuration, you may be wondering how my entity is associated with database configuration (DbConfig? What if I have multiple databases? Please rest assured that these frameworks have been implemented. We will discuss this issue in detail later. Let's explain the functions of these six methods one by one. Because the methods are relatively simple. You can explain their respective functions. CreateDbBuilder creates DbBuilder. DbBuilder has corresponding Derived classes for different database types, such as SqlserverBuilder. The purpose of using the factory model is, of course, to operate normally without modifying the code when switching the database. We have mentioned many Insert, Update, Delete, ToList, and other methods. In fact, they ultimately aim to convert them into SQL statements and then submit them to the database. Therefore, DbBuilder is generated to generate real SQL statements. For example, copy code 1 public override string ToTable (int pageSize, int pageIndex) 2 {3 if (Map. classInfo. dataVer = "2000") {return base. toTable (pageIndex, pageSize);} 4 if (pageIndex = 1) {return base. toTable (pageSize);} 5 6 if (SortString. length = 0) {SortString. appendFormat ("order by {0} ASC", Map. indexName);} 7 return string. format (8 "SELECT {0} FROM (SELECT {0}, ROW_NUMBER () OVER ({1}) as Row FR OM {2} {3}) a WHERE Row BETWEEN {4} AND {5}; ", 9 GetFields (), SortString, TableName, WhereString, (pageIndex-1) * pageSize + 1, pageIndex * pageSize); 10} copy the code above, which is taken from the method in SqlserverDbBuilder. The rewrite method is used. Let's take a look at the methods in DbBuilder (SqlserverDbBuilder inherits from DbBuilder): Copy code 1 public virtual string ToTable (int pageSize, int pageIndex) 2 {3 // No paging 4 if (pageIndex = 1) {return ToTable (pageSize);} 5 if (SortString. length = 0) {SortString. appendFormat ("order by {0} ASC", Map. indexName);} 6 7 var sort2 = SortString. toString (). replace ("DESC", "[reverse]"). replace ("ASC", "DESC "). replace ("[inverted]", "ASC"); 8 9 ret Urn string. format ("select top {1} {0} FROM (select top {2} {0} FROM {3} {4} {5}) a {6 };", 10 GetFields (), pageSize, pageSize * pageIndex, TableName, WhereString, SortString, sort2); 11} the copy code is a virtual method. We use rowrow_number () the paging function is provided. Therefore, we do not need the implementation in DbBuilder. Here, we understand the role of DbBuilder, which is used to produce SQL. CreateDbVisit: Create CreateDbVisit. CreateDbVisit has corresponding Derived classes for different database types, such as SqlserverVisit. We have always used Lambda expressions as conditions. For example, Where (o => o. ID = 1) or: Select (o => new {o. ID, o. userName}); then DbVisit is used to parse these processes. The parsing process uses the Expression tree: Expression <Func <entity class, bool> parses these Lbmbda Expression trees into all the SQL statements recognized by the database. In other words, the Expression tree Expression CreateDbOperate is used to create CreateDbOperate. CreateDbOperate has corresponding Derived classes for different database types, such as SqlserverOperate. This class is used to perform physical operations on the database. For example, creating a table, creating a field, deleting a field, and modifying a field. Of course, we usually use SQL Server Management Studio to maintain the database, but it is not excluded from some projects. We need to make it possible for customers to customize physical fields. (Of course, some of them use JSON structures and so on. I will not explain them here.) This method is rarely used. You can understand it. I know there is such a method. When you need to use it, you can view the methods contained in the returned class in the source code. CreateDbProvider: Create CreateDbProvider. CreateDbProvider has corresponding Derived classes for different database types, such as SqlserverDbProvider. What is DbProvider? Let's take a look at how DbProvider works. For example, each database type contains its own Provider. This Provider mainly provides the unique features of the database. For example, in Sqlserver. The table name and field name are protected by [] brackets. To avoid defining some names as SQL Server-specific keywords, it can be understood as an escape character. MySql uses two single quotes. CreateDbExecutor: Create CreateDbExecutor. The final execution of the framework is to pass the generated SQL in this class. Therefore, you can use the SQL statements you pass in to execute the desired results. In the previous two chapters, this method is used for transactions and SQL Execution. I will not explain it here. CreateConnString: CreateConnString. Create a connection string for the database. We know that different databases and different database versions have different connnectionstrings. This method is designed to create connection strings for different databases. Viewed ~ /App_Data/Db. Config should know that, for a configuration file, it uses not a connection string, but: Copy code 1 <? Xml version = "1.0"?> 2 <DbConfig xmlns: xsi =" http://www.w3.org/2001/XMLSchema -Instance "xmlns: xsd =" http://www.w3.org/2001/XMLSchema "> 3 <DbList> 4 <DbInfo> 5 <Server>. </Server> 6 <UserID> sa </UserID> 7 <PassWord> 123456 </PassWord> 8 <DataType> SqlServer </DataType> 9 <DataVer> 2005 </DataVer> 10 <Catalog> Farseer </Catalog> 11 <PoolMinSize> 16 </PoolMinSize> 12 <PoolMaxSize> 100 </PoolMaxSize> 13 <ConnectTimeout> 30 </ConnectTimeout> 14 <CommandTimeout> 60 </CommandTimeout> 15 </DbInfo> 16 </DbList> 17 </DbConfig> copy the code to configure the database. The framework uses this method to generate database connection strings.

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.