【发布时间】:2009-06-18 15:16:22
【问题描述】:
或者可能有更好的方法。
我正在为 NHibernate 构建一个动态查询构建器,我们不想将 HQL 直接放入应用程序中,我们希望它尽可能与 ORM 无关。目前看起来是这样的:
public override IEnumerable<T> SelectQuery(Dictionary<string, string> dictionary)
{
string t = Convert.ToString(typeof (T).Name);
string criteria = string.Empty;
foreach (KeyValuePair<string, string> item in dictionary)
{
if (criteria != string.Empty)
criteria += " and ";
criteria += item.Key + " = '" + item.Value + "'";
}
string query = " from " + t;
if (criteria != string.Empty)
query += " where " + criteria;
return FindByHql(query);
}
好的,很好,但是....这里有两个问题:
这个查询只处理“and”,我最初的想法是通过构建一个方法来动态构建字典,该字典采用属性名称、值和运算符“and”或“or”并构建字典以及一系列运算符。这听起来像是正确的做法吗?
好的,所以,这很好用,但是,当有一个整数时,它会因为单引号而失败。我认为最好的方法是让字典接受
<T.Property, string>,然后反映到 T.Property 以查找数据类型并采取相应的行为。我是不是把事情复杂化了?
谢谢。
【问题讨论】:
标签: c# .net nhibernate .net-3.5 orm