是否有可能为nhibernate返回一个查询作为一个IDictionary而不是一个实体类? [英] Is it possible for nhibernate to return a query as an IDictionary instead of an entity class?

查看:114
本文介绍了是否有可能为nhibernate返回一个查询作为一个IDictionary而不是一个实体类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体Person:

  public class Person 
{
public virtual int Id {得到;组; }
公共虚拟字符串名字{get;组; }
公共虚拟字符串MiddleName {get;组; }
public virtual string LastName {get;组; }

$ / code $ / pre

映射:

  public class PersonMap 
{
public PersonMap()
{
Table(TABLE_NAME);
Id(x => x.Id);
Map(x => x.FirstName).Not.Nullable();
Map(x => x.LastName).Not.Nullable();
Map(x => x.MiddleName).Not.Nullable();




$ b $ p
$有一些stui我希望Nhibernate返回字典,而不是实体:

  IDictionary< string,string> person = session.Get(id); // ???? 
字符串firstName = person [FirstName];

这可能不需要添加不同的映射吗?

解决方案

您将需要定义您自己的ResultTransformer实现,使其以您需要的方式工作。以下是您可以根据需要调整的参考实现。完全没有错误检查等;所以请谨慎使用;)

  using System; 
使用System.Collections;
使用NHibernate;
使用NHibernate.Properties;
使用NHibernate.Transform;



public class DictionaryResultTransformer:IResultTransformer
{

Dictionary DictionaryResultTransformer()
{

}

#region IResultTransformer成员
$ b $公共IList TransformList(IList集合)
{
return collection;


public object TransformTuple(object [] tuple,string [] aliases)
{
var result = new Dictionary< string,object>();
for(int i = 0; i< aliases.Length; i ++)
{
result [aliases [i]] = tuple [i];
}
返回结果;
}

#endregion
}


I have an entity Person:

public class Person
{
   public virtual int Id {get; set; }
   public virtual string FirstName { get; set; }
   public virtual string MiddleName { get; set; }
   public virtual string LastName { get; set; }
}

with the mappings:

public class PersonMap
{
   public PersonMap()
   {
       Table(TABLE_NAME); 
       Id( x => x.Id);
       Map(x => x.FirstName).Not.Nullable();
       Map(x => x.LastName).Not.Nullable();
       Map(x => x.MiddleName).Not.Nullable();
    }
}

There are some stuations where I would like Nhibernate to return a dictionary instead of the entity:

IDictionary<string,string> person = session.Get(id);//????
string firstName = person["FirstName"];

Is this possible to without adding a different mapping?

解决方案

You will need to define your own ResultTransformer implementation to have this working the way you need it. Below is a reference implementation that you can tweak as needed. There is a complete lack of error-checking, etc; so use with caution ;)

using System;
using System.Collections;
using NHibernate;
using NHibernate.Properties;
using NHibernate.Transform;


[Serializable]
public class DictionaryResultTransformer : IResultTransformer
{

        public DictionaryResultTransformer()
        {

        }

        #region IResultTransformer Members

        public IList TransformList(IList collection)
        {
                return collection;
        }

        public object TransformTuple(object[] tuple, string[] aliases)
        {
          var result = new Dictionary<string,object>();
          for (int i = 0; i < aliases.Length; i++)
          {
            result[aliases[i]] = tuple[i];                         
          }
          return result;
        }

        #endregion
}

这篇关于是否有可能为nhibernate返回一个查询作为一个IDictionary而不是一个实体类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆