本原理

/* 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. python set集合 以及 深浅拷贝

    set集合 特点: 无序, 不重复, 元素必须可哈希(不可变) 作用: 去重复 本身是可变的数据类型. 有增删改查操作. frozenset()冻结的集合. 不可变的. 可hash的 深浅拷贝() 1 ...

  2. EFCodeFirst使用Nuget更新数据库

    在MVC开发中,习惯于使用EF作为数据库操作,相对于传统的Ado.Net的数据库操作方式,EF大大的节省了我们手写SQL语句的时间,即便是传统的使用代码生成的方式.EF操作数据库目前分为两种大的方式. ...

  3. ngui自适应

    增加UIROOT using UnityEngine; namespace Com.Xyz.UI { [ExecuteInEditMode] [RequireComponent(typeof(UIRo ...

  4. 编译hostapd时,出现错误:/usr/bin/ld: cannot find -lnl

    book@ubuntu:/work/project/wifi/04.hostapd/hostapd-2.0/hostapd$ make /usr/bin/ld: cannot find -lnl co ...

  5. 大白话系列之C#委托与事件讲解大结局

    声明:本系列非原创,因为太精彩才转载,如有侵权请通知删除,原文:http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1698795.html 今天 ...

  6. OD 实验(二) - 绕过序列号验证

    需要破解的程序 输入用户名和序列号,点击 Check,程序会进行校验 用 OD 打开程序 按快捷键 Ctrl+F9 跟随表达式 GetDlgItemTextA 点击 ok 在这里调用了 GetDlgI ...

  7. Linux关闭iptables以及selinux

    1.查看iptables状态 /etc/init.d/iptables status # 方法1 service iptables status # 方法2 2.重启后永久生效 chkconfig i ...

  8. 在线浏览office 文件

    http://blog.csdn.net/binyao02123202/article/details/20051683 [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有 ...

  9. leetcode553

    public class Solution { public string OptimalDivision(int[] nums) { ) { return ""; } ) { ] ...

  10. 「小程序JAVA实战」小程序注册与后端联调(35)

    转自:https://idig8.com/2018/09/01/xiaochengxujavashizhanxiaochengxuzhuceyuhouduanliandiao35/ 小程序的后端spr ...