I have created a generic function for nHibernate to convert List to Bean which works fine however I need a way to do conversion in a generic way without calling AddScalar(as commented in my code) to my query that is assume I have over 50 model pojo and want to use same generic method to pull list of records how can I achieve this. This is my generic Method that I want to use to any model and pass associated query.
- public static IList<T> getRawQuery<T>(string query)
- {
- IList<T> ls = new List<T>();
- ISession session = MyHibernateUtil.openSession();
- try
- {
- ls = session.CreateSQLQuery(@query)
- .AddScalar("total", NHibernate.NHibernateUtil.Int32)
- .AddScalar("product_name", NHibernate.NHibernateUtil.String)
- .AddScalar("barcode", NHibernate.NHibernateUtil.String)
- .SetResultTransformer(Transformers.AliasToBean(typeof(T)))
- .List<T>();
-
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- }
- finally
- {
- MyHibernateUtil.CloseSession(session);
- }
- return ls;
- }
and here is my one of my model
- public class SalesModel
- {
- public virtual String barcode { get; set; }
- public virtual String product_name { get; set; }
- public virtual int total { get; set; }
- }