本原理

/* This code assumes an IEntity interface that identifies your persistent types. */
/// <summary>
/// This static class provides common extension methods for <see cref="IEntity"/> types.
/// </summary>
public static class EntityExtensions {
/// <summary>
/// Gets the real, underlying Entity-type - as opposed to the standard GetType() method,
/// this method takes into account the possibility that the object may in fact be an
/// NHibernate Proxy object, and not a real object. This method will return the real
/// Entity-type, doing a full initialization if necessary.
/// </summary>
public static Type GetEntityType(this IEntity entity ) {
if(entity is INHibernateProxy)
{ var lazyInitialiser = ((INHibernateProxy)entity).HibernateLazyInitializer;
var type = lazyInitialiser.PersistentClass;
if( type.IsAbstract || type.GetNestedTypes().Length > )
return Unproxy ( entity ).GetType ();
else // we don't need to "unbox" the Proxy-object to get the type
return lazyInitialiser.PersistentClass ;
}
return entity.GetType ();
}
/// <summary>
/// Based on the real, underlying Entity-type, this method returns true if the specified
/// type matches (or is assignable from) the specified Type.
/// </summary>
public static bool Is < TEntity >( this IEntity entity ) where TEntity : class , IEntity
{
var entityType = entity.GetEntityType ();
var type = typeof ( TEntity );
return entityType == type || type.IsAssignableFrom ( entityType );
} /// <summary>
/// In some cases, you may need the actual object, not just the type - for example, if
/// you're going to cast to a type deeper in the hierarchy, you may need to Unproxy
/// the object first.
/// </summary>
public static TEntity Unproxy < TEntity >( this TEntity entity ) where TEntity : class , IEntity
{
return entity is INHibernateProxy ? (TEntity)Service.Session.GetSessionImplementation ().PersistenceContext.Unproxy(entity ):entity ;
}
}

nhibernate GetType的更多相关文章

  1. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

  2. 配置NHibernate将枚举保存为Oracle数据库中的字符串

    假设有这样一个枚举: /// <summary> /// 字典项类型 /// </summary> public enum DicItemType { [EnumDescrip ...

  3. 5、ASP.NET MVC入门到精通——NHibernate代码映射

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一篇NHibernate学习笔记—使用 NHibernate构建一个ASP.NET MVC应用程序 使用的是xml进行orm映射,那么这一 ...

  4. 测试 ClownFish、CYQ、Entity Framework、Moon、MySoft、NHibernate、PDF、XCode数据访问组件性能

    下期预告: 由于很多园友反馈,有的组件不应该缺席.测试复杂度不够.测试还缺乏一定的公平. 因此考虑在下一个版本中,确保在更加公平的前提下进行更高复杂度的测试 . 同时将分为2组测试,纯SQL组件及纯O ...

  5. How to implement a custom type for NHibernate property

    http://blog.miraclespain.com/archive/2008/Mar-18.html <?xml version="1.0" encoding=&quo ...

  6. csharp:Nhibernate Procedure with CreateSQLQuery and GetNamedQuery

    <?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly=&quo ...

  7. NHibernate实例化类部分属性

    NHibernate 为习惯SQL的开发者提供了接口,将查询的结果转变为持久化对象.虽然该方法不是很提倡. GetCurrentSession().CreateSQLQuery(sql) 参数sql就 ...

  8. MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)

    MyGeneration的几个NHibernate模板功能已经很强,但还是存在些问题.例如:Guid主键支持不佳,代码不易修改,不支持中文注释等等.所以我决定自己来改写此模版.我把一部分通用的函数提取 ...

  9. Nhibernate cookbook 3.0-翻译

    /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-ts ...

随机推荐

  1. Django跳转

    跳转问题 如果我现在停留在文章的详情页,用户未登陆,如果你要评论,或者点赞就应该回到登陆页面登陆 如果登陆成功了,就要返回到当初跳转过来的页面 第一种通过前后端传送数据 $('.hit').click ...

  2. c++ 先序构建二叉树

    二叉树首先要解决构建问题,才能考虑后续的遍历,这里贴出通过先序构建二叉树,同时包含四种二叉树的遍历方法(先序,中序,后序,逐层) 第一.定义BinaryTreeNode 类 #include < ...

  3. java.lang.ArrayIndexOutOfBoundsException异常分析及解决

    这是一个非常常见的异常,从名字上看是数组下标越界错误,解决方法就是查看为什么下标越界. 下面是一个错误示例: Exception in thread "main" java.lan ...

  4. phpstorm 不能提示代码tp 3.2 $this->display等 解决办法

    phpstorm->file->Setting->Directorires 里把 ThinkPHP/Model 目录设置为 Excluded ,保存.

  5. OD 实验(六) - 对一个程序的破解

    程序: 双击进行安装,安装过程默认就好了 安装完成 运行程序 输入 User 为 sch01ar,点击 Create 点击 Log In 这里显示为未注册版本 看看未注册版本有哪些功能被限制 这里会显 ...

  6. GridControl 添加全选列

    这里通过List对象绑定GridControl,且不用在GirdControl界面中添加任何列,实现CheckBox列的方法 1.列表中出现CheckBox列 非常简单,在绑定的List实体中,增加一 ...

  7. apache DOCUMENT_ROOT

    问题描述:本地页面错误,+1上正常 本地及+1apache配置 <VirtualHost *:> ServerAdmin webmaster@dummy-host.example.com ...

  8. python+selenium+Firefox+pycharm版本匹配

    window(2018-05-29)最新 python:3.6.1    地址https://www.python.org/downloads/release/python-361/ selenium ...

  9. python学习整理

    Python-copy()与deepcopy()区别 —–我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在.所以改变原有被复制对象不会对已经复制出来的新对象产生影响. ...

  10. Django学习---cookie和session

    cookie 客户端浏览器上的一个文件,以键值对的形式存储,如{“user”:“dacehgnzi”} 入门:实现一个简单的登录功能 views.py: user_info = { '}, '}, } ...