Is it possible to do something like this:
Code:
User usr = new User();
usr.FirstName = "J%";
usr.LastName = "Gore";
IList<User> users = session.Find(usr) as IList<User>;
If not, how do I retrieve the table <=> object mappings (to be able to to the bindings myself)?[/code]
Edit 1:Ok. I've found the CreateCriteria command =)
I know that the method is very limited. But here is my quick and dirty implementation.
Code:
public class Manager
{
public IList<T> GeneralFind<T>(T searchPattern)
{
ISession session = NHibernateHelper.GetCurrentSession();
ICriteria criteria = session.CreateCriteria(typeof(T));
// Reflect all properties to get search params
PropertyInfo[] properties = searchPattern.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
object value = property.GetValue(searchPattern, null);
// Strings can use Like
if (value.GetType() == typeof(string))
{
string valueStr = value as string;
if (valueStr.IndexOf("%") >= 0)
criteria.Add(Expression.Like(property.Name, value));
else
criteria.Add(Expression.Eq(property.Name, value));
}
else
criteria.Add(Expression.Eq(property.Name, value));
}
return criteria.List<T>();
}
}
//And to fetch data:
User usr = new User();
usr.FirstName = "J%";
usr.LastName = "Gore";
Manager mgr = new Manager();
IList<User> users = mgr.GeneralFind<User>(usr);
I made it general by using reflection.