Or possibly there is a better way.
I am building a dynamic query builder for NHibernate, we don't want to put HQL directly into the application, we want it as ORM agnostic as possible. It looks like this currently:
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);
}
ok, great, however.... there are two things in here that pose a problem:
This query only handles "and," my initial thought is to pass is to build a method to dynamically build the dictionary that takes the property name, value, and an operator "and" or "or" and builds the dictionary along with an array of operators. Does that sound like the right thing to do?
Ok, so, this works GREAT, however, when there is an integer it fails because of the single quotes. What I think would be the BEST way is have the dictionary accept
<T.Property, string>
and then reflect into T.Property to find the datatype and behave accordingly. Am I over complicating this?
Thank you.