NHibernate 如何手动添加扩展?

NHibernate How to add extensions manually?

我想使用 this method 从我的数据库动态获取数据。如该主题中所述,我应该向我的 nhibernate 配置添加一个扩展。

有人能告诉我如何添加该扩展吗?

提前致谢。

public static class NhTransformers
{
    public static readonly IResultTransformer ExpandoObject;

    static NhTransformers()
    {
        ExpandoObject = new ExpandoObjectResultSetTransformer();
    }

    private class ExpandoObjectResultSetTransformer : IResultTransformer
    {
        public IList TransformList(IList collection)
        {
            return collection;
        }

        public object TransformTuple(object[] tuple, string[] aliases)
        {
            var expando = new ExpandoObject();
            var dictionary = (IDictionary<string, object>)expando;
            for (int i = 0; i < tuple.Length; i++)
            {
                string alias = aliases[i];
                if (alias != null)
                {
                    dictionary[alias] = tuple[i];
                }
            }
            return expando;
        }
    }
}

public static class NHibernateExtensions
{
    public static IList<dynamic> DynamicList(this IQuery query)
    {
        return query.SetResultTransformer(NhTransformers.ExpandoObject)
                .List<dynamic>();
    }
}

-----------------------------------
USE CASE
-----------------------------------

var results = this.session.CreateSQLQuery("select Id, Title, Body from [Posts]")
              .DynamicList(); // Secret sauce!
// results are now dynamic!
Console.WriteLine(results[0].Id);
Console.WriteLine(results[0].Name);
// rock on!

最后,我只创建了 NhTransformers class 并且这样做了;

var query1 = " select * from mySQLView";

var result1 = this.session.CreateSQLQuery(query1).SetResultTransformer(NhTransformers.ExpandoObject)
                .List<dynamic>();

foreach (var obj in result1)
{
   // some stuff...
}