首页 > 其他 > 详细

How to implement a custom type for NHibernate property

时间:2014-03-05 18:34:55      阅读:447      评论:0      收藏:0      [点我收藏+]

http://blog.miraclespain.com/archive/2008/Mar-18.html

 

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="SoftInn.Domain" >

  <class name="SoftInn.Domain.Archives.Archive" table="archive" lazy="false">   
    <!--Mapped classes must declare the primary key column of the database table. 
    Most classes will also have a property holding the unique identifier of an instance. 
    The <id> element defines the mapping from that property to the primary key column.-->
    <id name="Id" column="archive_id" type="Int64">
      <!--"column" is The name of the primary key column-->
      <generator class="SoftInn.Services.Internal.NHibernate.IdGenerator, SoftInn.Services">
        <param name="table">nextinstanceid</param>
        <param name="column">next</param>
        <param name="max_lo">9</param>
      </generator>
    </id>
    
    <version name="LastUpdateNumber" column="last_update_number" type="Int32" unsaved-value="-1"/>
    <component name="OwnerId" class="SoftInn.Domain.ObjectId">
      <property name="ClassId" column= "node_type" type="Int32"/>
      <property name="InstanceId" column= "node_id" type="Int64"/>
    </component>

    <property name="User" type="SoftInn.Services.Internal.NHibernate.UserType, SoftInn.Services" not-null="false">
      <column name="siuser_id"/>
    </property>    
    <property name="FullName" column= "full_name" type="String"/>    
    <property name="Name" column= "archive_name" type="String"/>
    <property name="NodeNamePath" column= "node_name_path" type="String"/>
    <property name="Size" column="archive_size" type="Int64"/>
    <property name="ZipSize" column="archive_physical_zip_size" type="Int64"/>    
    <property name="ContainsReports" column="contains_reports" type="YesNo"/>
    <property name="ContainsPermissions" column="contains_permissions" type="YesNo"/>
    <property name="FilteredByDateRange" column="is_filteredby_data_range" type="YesNo"/>
    <property name="StartDate" column= "start_datetime" type="SoftInn.Services.Internal.NHibernate.DateTimeType, SoftInn.Services"/>
    <property name="EndDate" column= "end_datetime" type="SoftInn.Services.Internal.NHibernate.DateTimeType, SoftInn.Services"/>
    <component name="PermissionOwnerId" class="SoftInn.Domain.ObjectId">
      <property name="ClassId" column= "permissionowner_type" type="Int32"/>
      <property name="InstanceId" column= "permissionowner_id" type="Int64"/>
    </component>
    <property name="PermissionOwnerName" column= "permissionowner_name" type="String"/>
    <property name="CreateDate" column= "create_datetime" type="SoftInn.Services.Internal.NHibernate.DateTimeType, SoftInn.Services"/>
    <property name="Status" column= "status" type="String"/>    
  </class>

</hibernate-mapping>
bubuko.com,布布扣

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace SoftInn.Services.Internal.NHibernate {
 
    /// <summary>
    /// Used to retrieve and store generic User object relationships via an "InstanceId" column
    /// </summary>
    public class UserType : BaseEntityType {
 
        /// <see cref="SoftInn.Services.Internal.NHibernate.BaseEntityType.ReturnedClass"/>
        public override Type ReturnedClass {
            get { return typeof(User); }
        }
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using NHibernate;
using NHibernate.Engine;
using NHibernate.Type;
using NHibernate.UserTypes;
using SoftInn.Domain;
using SoftInn.Platform.Log;
 
namespace SoftInn.Services.Internal.NHibernate {
 
    /// <summary>
    /// Abstract base class for classes used to retrieve and store
    /// generic Persistent object relationships via an "InstanceId" column.
    /// </summary>
    public abstract class BaseEntityType : ICompositeUserType {
 
        #region ICompositeUserType Members
 
        /// <summary>See base class documentation</summary>
        public string[] PropertyNames {
            get { return new String[] { "InstanceId" }; }
        }
 
        /// <summary>See base class documentation</summary>
        public IType[] PropertyTypes {
            get {
                return new IType[] { NHibernateUtil.Int64 };
            }
        }
 
        /// <summary>See base class documentation</summary>
        public abstract Type ReturnedClass {
            get;
        }
 
        /// <summary>See base class documentation</summary>
        public object Assemble(object cached, ISessionImplementor session, object owner) {
            return DeepCopy(cached);
        }
 
        /// <summary>See base class documentation</summary>
        public object DeepCopy(object value) {
            return value;
        }
 
        /// <summary>See base class documentation</summary>
        public object Disassemble(object value, ISessionImplementor session) {
            return DeepCopy(value);
        }
 
        /// <summary>See base class documentation</summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        public object GetPropertyValue(object component, int property) {
            if (component == null) {
                throw new ArgumentNullException("component");
            }
 
            if (property == 0) {
                return ((Entity)component).ObjectId.InstanceId;
            } else {
                return null;
            }
        }
 
        /// <summary>See base class documentation</summary>
        public bool IsMutable {
            get { return true; }
        }
 
        /// <summary>See base class documentation</summary>
        public object NullSafeGet(System.Data.IDataReader dr, string[] names, ISessionImplementor session, object owner) {
            object instanceIdObj = NHibernateUtil.Int64.NullSafeGet(dr, names[0], session, owner);
 
            if (instanceIdObj == null ) {
                // Null instanceId means a null entity reference.
                return null;
            }
 
            Int64 instanceId = (Int64)instanceIdObj;
 
            // Construct an ObjectId for this instanceId.
            ObjectId entityId = new ObjectId(ReturnedClass, instanceId);
 
            try {
                // Find and return the corresponding Entity object.
                return ServiceDirectory.PersistServices.FindById(entityId, true);
 
            } catch (SoftInn.Services.Exceptions.ObjectNotFoundException) {
                // The referenced entity no longer exists, but we still have a reference to it by ID.
                LogManager.LogError(ServicesLogMessages.LOG_CONTEXT,
                    ServicesLogMessages.PERSIST_MISSING_OBJECT,
                    null, entityId.ToString(), GetType().FullName + ".NullSafeGet");
                //throw;
                return null;
            }
        }
 
        /// <summary>See base class documentation</summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2233:OperationsShouldNotOverflow", MessageId = "index+1")]
        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index, ISessionImplementor session) {
 
            Entity entity = (value == null) ? null : (Entity)value;
 
            if (entity != null && entity.ObjectId != null) {
                // Entity with an ObjectId... store the instance id and class id.
                Int64 instanceId = entity.ObjectId.InstanceId;
                NHibernateUtil.Int64.NullSafeSet(cmd, instanceId, index, session);
 
            } else {
                // Null entity... store null instance id and class id.
                NHibernateUtil.Int64.NullSafeSet(cmd, null, index, session);
            }
        }
 
        /// <summary>See base class documentation</summary>
        public void SetPropertyValue(object component, int property, object value) {
        }
 
        #endregion
 
        /// <summary>See base class documentation</summary>
        public new bool Equals(object obj1, object obj2) {
            if (obj1 == null) {
                return obj2 == null;
            } else {
                return obj1.Equals(obj2);
            }
        }
 
        /// <summary>See base class documentation</summary>
        public int GetHashCode(object obj1) {
            if (obj1 != null) {
                return obj1.GetHashCode();
            }
            else {
                return this.GetHashCode();
            }
        }
 
        /// <summary>See base class documentation</summary>
        public object Replace(object original, object target, ISessionImplementor session, object owner) {
            return original;
        }
 
    }
}

  

How to implement a custom type for NHibernate property,布布扣,bubuko.com

How to implement a custom type for NHibernate property

原文:http://www.cnblogs.com/malaikuangren/p/3581917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!