分析
要做一個基於數據庫的應用程序
圍繞這
一
ADO
為了提高性能
為了使用方便
public System
public System
public int SqlExecuteNonQuery(string sql
public System
public object SqlExecuteScalar(string sql
當然
DbHelper dbhelper = new DbHelper();
string sql =
using (DatabaseTrans trans = new DatabaseTrans(dbhelper))
{
try
{
dbhelper
dbhelper
trans
OutPut(
}
catch (Exception)
{
trans
OutPut(
}
}
二
先看如下的代碼
//
xxxCase xxxCase = new xxxCase();
xxxCase
xxxCase
xxxCase
xxxCase
xxxCase
xxxCase
xxxCase
xxxCase
xxxCase
EntityBase
//
xxxCase
xxxCase
xxxCase
EntityBase
//
xxxCase
xxxCase
EntityBase
//
WhereCondition condition = new WhereCondition(
xxxCase
new WhereCondition(xxxCase
DateTime
new WhereCondition(xxxCase
IList<xxxCase> list = EntityBase
new string[] {
foreach (xxxCase item in list)
{
Console
}
Console
上面的代碼是以面向對象(請忽略那些關於貧血模型的討論
性能也不會很差(請忽略具體語句是否能使用索引的討論
我們看一下EntityBase
private static DbHelper _db = new DbHelper();
public static void Insert(EntityBase entity) {
string sql = GetInsertSql(entity);
string[] parameters = GetParameters(entity
object[] parameterValues = GetParameterValuess(entity
_db
}
private static string GetInsertSql(EntityBase entity) {
int len = entity
StringBuilder sql = new StringBuilder();
sql
sql
for (int i =
if (i != len
sql
else
sql
}
sql
sql
for (int i =
if (i != len
sql
else
sql
}
sql
return sql
}
private static string[] GetParameters(IList<DbCommonClass<string
int len = items
List<string> parameters = new List<string>();
for (int i =
parameters
}
return parameters
}
private static object[] GetParameterValuess(List<DbCommonClass<string
int len = items
List<object> parameters = new List<object>();
for (int i =
parameters
}
return parameters
}
當然Select方法稍微復雜一些
三
ADO
為了脫離對特定數據庫的依賴
public class CodeModel
{
public string ClassName;
public string TableName;
public string Descript;
public string Namespace;
public string PkColName;
public List<CodeProperty> Properties;
}
public class CodeProperty
{
public string DbColName;
public int? DbLength;
public bool DbAllowNull
public SqlDbType DbType;
public string DbTypeStr;
public bool DbIsIdentity;
public bool DbIsPk;
public string Descript;
public string PropertyName;
public System
public string CSharpTypeStr;
public bool UiAllowEmpty;
public bool UiIsShowOn;
public long? UiMaxCheck;
public long? UiMinCheck;
public string UiRegxCheck;
}
得到元數據後
比如實體類的模板
using System;
using System
using WawaSoft
namespace $model
public class $model
$foreach
public const string $prop
$endforeach$
private static readonly List<string> _Cols = new List<string>();
static $model
{
$foreach
_Cols
$endforeach$
}
public $model
_tableName =
_PkName =
}
$foreach
private $prop
$endforeach$
$foreach
public $prop
get { return $prop
set {
$prop
AddInnerData(
}
}
$endforeach$
protected override IList<string> Cols
{
get { return _Cols; }
}
public override void ConvertToEntity(IEnumerable<DbCommonClass<string
foreach (DbCommonClass<string
switch (item
$foreach
case $prop
$prop
break;
$endforeach$
}
}
}
}
}
生成的實體類
using System;
using System
using WawaSoft
namespace Entities {
public class User : EntityBase {
public const string UserIdColName =
public const string UsernameColName =
public const string NameColName =
public const string PasswordColName =
public const string CreateTimeColName =
public const string IsAdminColName =
private static readonly List<string> _Cols = new List<string>();
static User() {
_Cols
_Cols
_Cols
_Cols
_Cols
_Cols
}
public User() {
_tableName =
_PkName =
}
private Nullable<Int
private String username;
private String name;
private String password;
private Nullable<DateTime> createtime;
private Nullable<Boolean> isadmin;
public Nullable<Int
get { return userid; }
set {
userid = value;
AddInnerData(
}
}
public String Username {
get { return username; }
set {
username = value;
AddInnerData(
}
}
public String Name {
get { return name; }
set {
name = value;
AddInnerData(
}
}
public String Password {
get { return password; }
set {
password = value;
AddInnerData(
}
}
public Nullable<DateTime> CreateTime {
get { return createtime; }
set {
createtime = value;
AddInnerData(
}
}
public Nullable<Boolean> IsAdmin {
get { return isadmin; }
set {
isadmin = value;
AddInnerData(
}
}
protected override IList<string> Cols {
get { return _Cols; }
}
public override void ConvertToEntity(IEnumerable<DbCommonClass<string
foreach (DbCommonClass<string
switch (item
case UserIdColName:
userid = (Nullable<Int
break;
case UsernameColName:
username = (String)item
break;
case NameColName:
name = (String)item
break;
case PasswordColName:
password = (String)item
break;
case CreateTimeColName:
if (item
createtime = (Nullable<DateTime>)item
break;
case IsAdminColName:
if (item
isadmin = (Nullable<Boolean>)item
break;
}
}
}
}
}
小結
解決了以上幾個問題
From:http://tw.wingwit.com/Article/program/net/201311/11415.html