NetUML.DataEngine 數據引擎支持多數據庫,數據訪問引擎采用配置方式,類似ibatis.net底層原理,支持多數據庫連接方式。將來可支持數據庫讀寫分離,讀寫分離配置采用MVC路由機制。
源碼結構
1 <provider name="oracleManagedDataAccess"
2 description="Oracle, Microsoft provider V1.0.5000.0"
3 enabled="true"
4 default="false"
5 assemblyName="Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"
connectionClass="Oracle.ManagedDataAccess.Client.OracleConnection"
6 commandClass="Oracle.ManagedDataAccess.Client.OracleCommand"
7 parameterClass="Oracle.ManagedDataAccess.Client.OracleParameter"
8 parameterDbTypeClass="Oracle.ManagedDataAccess.Client.OracleDbType"
9 parameterDbTypeProperty="OracleType"
10 dataAdapterClass="Oracle.ManagedDataAccess.Client.OracleDataAdapter"
11 commandBuilderClass="Oracle.ManagedDataAccess.Client.OracleCommandBuilder"
12 usePositionalParameters="false"
13 useParameterPrefixInSql="true"
14 useParameterPrefixInParameter="false"
15 parameterPrefix=":"
16 allowMARS="false"
17 />
assemblyName:訪問數據庫的DLL程序集信息,此DLL文件也必須放在程序根目錄下。
App.Config 配置數據庫連接字符串
<add providerName="oracleManagedDataAccess" name="oracle"
connectionString="User ID=d;Password=d;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=ORCL)));"/>
providerName:采用哪個配置信息訪問數據庫,這裡采用oracleManagedDataAccess的配置。
在程序啟動的時候調用 NetUML.DataAccess.DbHelper.InitDBConfig();
1、新建表“Class1”
1 /** 創建表[Class1] **/ 2 CREATE TABLE [dbo].[Class1]( 3 [ID] [int] IDENTITY (1, 1) NOT NULL , 4 [aa] [nvarchar](50) NULL , 5 [bb] [datetime] NULL , 6 [cc] [bit] NULL , 7 [ee] [int] NULL , 8 CONSTRAINT [PK_Class1] PRIMARY KEY 9 ( 10 [ID] ASC 11 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 12 ) ON [PRIMARY] 13 GO
2、生成“ClassModel”
1 ///<summary> 2 ///Class1 3 ///<summary> 4 [Table("Class1")] 5 public class ClassModel: NetUML.DataEngine.ActiveEntity 6 { 7 8 /// <summary> 9 /// [ID] 10 /// </summary> 11 [Key(KeyType.Indentity)] 12 public int ID 13 { 14 get;set; 15 } 16 private string _aa; 17 /// <summary> 18 /// [aa] 19 /// </summary> 20 public string aa 21 { 22 set 23 { 24 if (_aa == value) return; 25 OnChanged("aa", _aa, value); 26 _aa = value; 27 } 28 get { return _aa;} 29 } 30 private DateTime? _bb; 31 /// <summary> 32 /// [bb] 33 /// </summary> 34 public DateTime? bb 35 { 36 set 37 { 38 if (_bb == value) return; 39 OnChanged("bb", _bb, value); 40 _bb = value; 41 } 42 get { return _bb;} 43 } 44 private bool? _cc; 45 /// <summary> 46 /// [cc] 47 /// </summary> 48 public bool? cc 49 { 50 set 51 { 52 if (_cc == value) return; 53 OnChanged("cc", _cc, value); 54 _cc = value; 55 } 56 get { return _cc;} 57 } 58 private int? _ee; 59 /// <summary> 60 /// [ee] 61 /// </summary> 62 public int? ee 63 { 64 set 65 { 66 if (_ee == value) return; 67 OnChanged("ee", _ee, value); 68 _ee = value; 69 } 70 get { return _ee;} 71 } 72 }
新建表和生成Model代碼都是用DataUML 軟件生成的,如下圖:
1 //添加數據 2 ClassModel model = new ClassModel(); 3 model.aa = "ss"; 4 model.bb = DateTime.Now; 5 model.Save(); 6 //修改數據 7 ClassModel model = new ClassModel(); 8 model.ID = 1; 9 model.aa = "bb"; 10 model.Update(); 11 // 刪除數據 12 ClassModel model = new ClassModel(); 13 model.ID = 1; 14 model.Delete();
app.config 數據庫配置信息如下:
<add providerName="oracleManagedDataAccess" name="oracle" connectionString="User ID=a;Password=a;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)));"/> <add providerName="sqlServer2.0" name="sqlconnect1" connectionString="Data Source=192.168.0.1;Initial Catalog=t;User Id=sa;Password=sa" /> <add providerName="sqlServer2.0" name="sqlconnect2" connectionString="Data Source=192.168.0.1;Initial Catalog=t;User Id=sa;Password=sa" />
Model的信息如下。
1 ///<summary> 2 ///采用“sqlconnect1” 這個數據庫連接字條串進行讀寫數據 3 ///<summary> 4 [Table("Class1", ConnectionName = "sqlconnect1")] 5 public class ClassModel: NetUML.DataEngine.ActiveEntity 6 { 7 [Key(KeyType.Indentity)] 8 public int ID 9 { 10 get;set; 11 } 12 }
當讀寫數據的時候,會采用
<add providerName="sqlServer2.0" name="sqlconnect1" connectionString="Data Source=192.168.0.1;Initial Catalog=t;User Id=sa;Password=sa" />
這條數據庫連接字符串。
1、增加讀寫分離配置、多數據庫操作配置,無需在類上面定義ConnectionName屬性,采用配置方式進行設置
2、增加面向對象的概念,類與類之間映射關系,以及級聯操作
3、增加更豐富的查詢操作
4、。。。。。。。。。。
源碼下載