完全的模擬表達式非常困難,因為C#目前不支持自定義操作符,只支持操作符重載(希望C#能夠提供操作符自定義,如果有語言支持這個,也請告知),思路其實很簡單,就是建立自己的類型,並重載一些操作符,模擬這個的目的就是在實體模型下規避直接寫數據庫SQL語句,用類似於ESQL,Linq語法完成這個工作,提供開發一個一致的實體應用模型:
1)參數類,用於傳遞實際參數,最終轉換成ADO.Net的命令中需要的參數:
[csharp]
public class Parameter
{//沒有具體實現,這裡只需要模擬
}
2)表達式接口:
[csharp]
public interface IDbExpression
{
string Expression { get; }
Dictionary<string, Parameter> Parameters {get;}
}
3)表達式基類:主要是一些操作符號重載,一些無法用表達式的地方定義類似的函數
[csharp]
public class DbExpression : IDbExpression
{
private StringBuilder _sqlStatement;
private Dictionary<string, Parameter> _Parameters;
public StringBuilder SQL { get { return _sqlStatement; } }
public DbExpression()
{
_sqlStatement = new StringBuilder();
_Parameters = new Dictionary<string, Parameter>();
}
public static DbExpression operator >(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " > " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public static DbExpression operator <(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " < " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public static DbExpression operator >=(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " >= " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public static DbExpression operator <=(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " <= " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public static DbExpression operator ==(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " = " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public static DbExpression operator !=(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " != " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public static DbExpression operator &(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " and " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public static DbExpression operator |(DbExpression e1, DbExpression e2)
{
DbExpression theExp = new DbExpression();
theExp.SQL.AppendLine(e1.Expression + " or " + e2.Expression);
theExp.AddParams(e1);
theExp.AddParams(e2);
return theExp;
}
public DbExpression Parentheses(DbExpression e)
{
this.SQL.AppendLine("(" + e.Expression + ")");
this.AddParams(e);
return this;
}
public DbExpression Where(DbExpression e1)
{
this._sqlStatement.AppendLine(" WHERE " + e1.Expression);
this.AddParams(e1);
return this;
}
public DbExpression From(params DbExpression[] Tables)
{
this.SQL.AppendLine(" FROM ");
if (Tables != null)
{
int i = 0;
foreach (var item in Tables)
{
if (i == 0)
{
_sqlStatement.Append(" " + item.Expression);
}
else
{
_sqlStatement.Append("," + item.Expression);
}
this.AddParams(item);
}
}
return this;
}
public DbExpression Select(params DbExpression[] Fields)
{
_sqlStatement.Append(" SELECT ");
if (Fields != null)
{
int i = 0;
foreach (var item in Fields)
{
if (i == 0)
{
_sqlStatement.Append(" " + item.Expression);
}
else
{
_sqlStatement.Append("," + item.Expression);
}
this.AddParams(item);
}
}
else
{
_sqlStatement.Append(" * ");
}
return this;
}
public void AddParams(DbExpression e)
{
if (e.Parameters == null)
{
return;
}
foreach (var p in e._Parameters)
{
if(this._Parameters.ContainsKey(p.Key)==false)
{
this._Parameters.Add(p.Key, p.Value);
}
}
}
public void AddParams(IDbExpression e)
{
if (e.Parameters == null)
{
return;
}
foreach (var p in e.Parameters)
{
if (this._Parameters.ContainsKey(p.Key) == false)
{
this._Parameters.Add(p.Key, p.Value);
}
}
}
public virtual string Expression
{
get { return _sqlStatement.ToString(); }
}
public virtual Dictionary<string, Parameter> Parameters
{
get { return _Parameters; }
}
}
摘自 hawksoft