Farseer.net lightweight open source framework intermediate: dynamic database access

Source: Internet
Author: User

Farseer.net lightweight open source framework intermediate: dynamic database access
When talking about access to dynamic databases, we have to say that how do we find and Access Database configurations for our entity classes? Let's review our Users class: 1 [DB (Name = "Members_User")] 2 public class Users: BaseModel <Users>; here we have a DB feature, name indicates the table Name. In the group, I am often asked if I support views? The view has the same SQL statement as the table query statement. So you only need to change the table name to the view name here. Let's take a look at the definition of DB: Copy code 1 namespace FS. ORM 2 {3 /// <summary> 4 // object class attribute mark 5 /// </summary> 6 [AttributeUsage (AttributeTargets. class)] 7 public sealed class DBAttribute: attribute 8 {9 // <summary> 10 // The default value is MSSqlServer 2005, and the first database configuration is 11 /// </summary> 12 public DBAttribute () 13 {14 DbIndex = 0; 15 DataType = DataBaseType. sqlServer; 16 DataVer = "2005"; 17} 18 19 /// <summary> 20 /// table name 21 /// </summary> 22 Public string Name {get; set;} 23 24 // <summary> 25 // set the database connection configuration (Dbconfig) index item 26 /// </summary> 27 public int DbIndex {get; set ;} 28 29 /// <summary> 30 // set the database connection string 31 /// </summary> 32 public string ConnStr {get; set ;} 33 34 // <summary> 35 // set the database type 36 /// </summary> 37 public DataBaseType ype {get; set ;} 38 39 // <summary> 40 // set database version 41 /// </summary> 42 public string DataVer {get; Set;} 43 44 // <summary> 45 // set the time when the database executes the T-SQL, in seconds, the default value is 30 seconds. 46 // </summary> 47 public int CommandTimeout {get; set ;} 48} 49} copy the code. All the information about the relationship between the table and the database is saved here. In the constructor, the default value is DbIndex = 0. This setting is the configuration of 0th indexes in DbConfigs. Now we know how to find the database configuration. Assume that ~ The/App_Data/Db. Config file contains two database configurations. Users corresponds to 2nd configurations. You can write the entity class as follows: 1 [DB (Name = "Members_User", DbIndex = 1)] 2 public class Users: baseModel <Users> Farrseer. net will go to the second DbIndex to retrieve the database configuration. Of course, the default value is 0, so we do not need to display the statement. How to dynamically change the DbIndex mentioned above for different databases is a fixed binding to the object class at runtime. However, sometimes our project may be a dynamic data source. For example, it is possible that each user is an independent database. Their table structure is the same. In this case, you must be able to support the dynamic (changed during runtime) mode. In the common BaseModel logic, Users. Data returns the Bean <Users> type. The Data of BaseCacheModel and BaseCateModel is of the CacheBean <Users> type. The two types are separated mainly because the operations of the cache logic are different, mainly because the cached data is stored in local variables. Data operates on databases. Therefore, CacheBean <Users> does not have any operation method to query databases. We need to dynamically change the database source through the Bean <Users> or CacheBean <Users> constructor. The default Data parameter is the DbIndex of the object class. Directly paste the code to see how Data instances: this is a common logic layer (BaseModel) data Replication code 1 // <summary> 2 // database persistence 3 // </summary> 4 public static Bean <TInfo> Data 5 {6 get 7 {8 var bean = new Bean <TInfo> (); 9 DataResult = bean. dataResult; 10 return bean; 11} 12} copy the code to copy code 1 /// <summary> 2 /// default use 3 /// </summary> 4 /// <param name = "tableName"> table name </param> 5 public Bean (string tableName = "") 6 7 // 8 /// <summary> 9 // compatible with Qyn. F Acloud Project 10 /// </summary> 11 /// <param name = "dbIndex"> Database Configuration index </param> 12 /// <param name = "tableName"> table name </param> 13 public Bean (int dbIndex, string tableName = "") 14 15 // 16 /// <summary> 17 // compatible with Qyn. factory Project 18 /// </summary> 19 /// <param name = "dbType"> Database Type </param> 20 /// <param name = "connetionString"> connection string </param> 21 /// <param name = "commandTimeout"> timeout value for command execution </param> 22 /// <param name = "tableName"> Table name </param> 23 public Bean (DataBaseType dbType, string connetionString, int commandTimeout, string tableName = "") copy the Code. This is BaseCacheModel (Cache logic) and BaseCateModel (classification logic) data: copy code 1 // <summary> 2 // database persistence 3 // </summary> 4 public static CacheBean <TInfo> Data 5 {6 get 7 {8 var bean = new CacheBean <TInfo> (); 9 DataResult = bean. dataResult; 10 return bean; 11} 12} 1 // <summary> 2 // 3 by default // </s Ummary> 4 // <param name = "tableName"> table name </param> 5 public CacheBean (string tableName = "") 6 7 /// <summary> 8 // compatible with Qyn. factory Project 9 // </summary> 10 // <param name = "dbType"> Database Type </param> 11 /// <param name = "connetionString"> connection string </param> 12 // <param name = "commandTimeout"> timeout value for command execution </param> 13 /// <param name = "tableName"> table name </param> 14 public CacheBean (DataBaseType dbType, string connetionString, in T commandTimeout, string tableName = "") copy the code. After reading their constructor, I believe everyone knows how to use it: Users. data. xxxx and Bean <Users>. xxxx is the same result. So sometimes we write code like this: Copy code 1 Bean <Users> bean = Users. data; 2 bean. where (......) 3 4 // You can also: 5 6 beans <Users> Bean = new bean <Users> (); 7 beans. where (......) when the code is changed dynamically, you may think like this: 1 var bean = new Bean <Users> (DataBaseType. sqlServer, "User ID = sa; Password = 123456; Pooling = true; Data Source = .; initial Catalog = Farseer; ", 30); in this way, the bean is not accessed by the database source defined by Users. How is it easy? This chapter is about this.

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.