nhibernate GetType

本原理

/* 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 >0 )
                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 ;
        }
     }
时间: 2024-10-06 00:50:19

nhibernate GetType的相关文章

NHibernate动态添加表

NHibernate动态添加表 设置和动态扩展表差不多,添加了一个模板hbm.xml文件,用于创建动态hbm.xml,HibernateUtil无改动. MappingManger添加了两个方法 1 public static void UpdateClassMapping(DynamicTestModel dynamicModel) 2 { 3 var session = HibernateUtil.Instance.CurrentSession; 4 var fileName = "hbm_

NHibernate.3.0.Cookbook第一章第五节Setting up a base entity class

Setting up a base entity class设置一个实体类的基类 在这节中,我将给你展示怎么样去为我们的实体类设置一个通用的基类. 准备工作 完成前面三节的任务 如何去做 1.在Entity.cs中,为我们的Entity类输入如下代码: public abstract class Entity<TId>{public virtual TId Id { get; protected set; }public override bool Equals(object obj){ret

NHibernate代码映射

上一篇NHibernate学习笔记—使用 NHibernate构建一个ASP.NET MVC应用程序 使用的是xml进行orm映射,那么这一篇就来讲下代码映射. 新建一个抽象的数据化持久基类AbstractNHibernateDao.cs /// <summary> /// 数据持久化基本 /// </summary> /// <typeparam name="T">要持久化的数据类型</typeparam> /// <typepa

csharp:Nhibernate Procedure with CreateSQLQuery

<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly="Domain" namespace="Domain" xmlns="urn:nhibernate-mapping-2.2"> <class name="DuCardType" table="DuCardType&q

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

假设有这样一个枚举: /// <summary> /// 字典项类型 /// </summary> public enum DicItemType { [EnumDescription("程序使用")] Program = 0, [EnumDescription("用户自定义")] Custom = 1 } NHibernate默认是映射为数据库中的数字类型,也就是0或者1.当我们使用数据库管理工具(例如PLSql/Developer)直接浏

spring.net 集成nhibernate配置文件(这里暴露了GetCurrentSession 对于 CurrentSession unbond thread这里给出了解决方法)

我这里主要分成了两个xml来进行spring.net管理实际情况中可自己根据需要进行分类 Dao2.xml <?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database" xmlns:tx=&quo

NHibernate源码分析

NHibernate源码分析之开篇: 计划和安排 只从使用NHibernate以来,请被其强大的功能和使用的简洁所吸引. 为了进一步研究NHibernate,决定分析其源代码,如有感兴趣者,欢迎一起研究. 这里列出了将要分析的部分: 1.        NHibernate配置和持久对象映射文件 2.        NHibernate架构分析(uml图) 3.        NHibernate源码分析之一: 配置信息 4.        NHibernate源码分析之一(续): 对象映射 5

NHibernate &amp; INotifyPropertyChanged

One of the things that make NHibernate easy to use is that it fully support the POCO model. But one of the things that most people do not consider is that since NHibernate did the hard work of opening up the seams to allow external persistence concer

Spring.NET教程(十九)整合NHibernate和ASP.NET MVC(基础篇)

今天带给大家的就是期待以久的ASP.net MVC与Spring.NET和NHibernate的组合,视图打造.NET版的SSH(Spring-Struts-Hibernate).是不是听到名字都很兴奋?我认为目前的ASP.NET MVC比Struts在某些功能上要好用的多,而且代码量要少,这就是我一直热衷于ASP.NET MVC的原因. 我们接着昨天的例子学习.昨天我们成功测试了带事务的业务层.接下来就是将业务层的对象注入到Controller中.我们先在Controller中写好要注入的属性