views:

48

answers:

1

What is the best way to run raw SQL against NHibernate, then push it into List using AutoMapper?

Example:

public virtual List<T> GetList<T>(string mainsql)
{
    IQuery q = GetSession().CreateSQLQuery(mainsql);
    IEnumerable srcAllRows = q.List();
    List<T> targetAllRows = new List<T>();
    *** do AutoMapper thing to get srcAllRow to targetAllRows ***
    return targetAllRows;
}
A: 

I did a lot of stuff like this, but with my mapping technology (ValueInjecter)

There is a DAL (console app) sample in the download I am doing this without NHibernate so it's like this:

        public static IEnumerable<T> GetAll<T>(string cs) where T : new()
        {
            using (var conn = new SqlConnection(cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "select * from " + TableConvention.Resolve(typeof(T));
                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new T();
                            o.InjectFrom<ReaderInjection>(dr);
                            yield return o;
                        }
                    }
                }
            }
        }

with NHibernate I know there something like

criteriaSelect.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(YourEntity)));

which should do the mapping for you

Omu