NHibernate 配置文件的处理和使用多数据库的多层架构思路(第二部分)

二、数据访问层
这里演示了一个 一对多的例子:
两个表:事件表和日志表,事件表 (1: n) 日志表.
以下是两个实体类和对应的配置文件。我单独建了一个项目来存放它们,它们不算是数据访问层!数据访问层、业务逻辑层、显示层都要用它。

 

None.gif // -----------------------------------------------------------------------------------------
None.gif
//  模块编号:030101
None.gif
//  文件名: EventTypeInfo.cs
None.gif
//  描述: EventTypeInfo 实体类
None.gif
//  作者:ChenJie 
None.gif
//  编写日期:2007-4-27
None.gif
//  Copyright 2007
None.gif
// -----------------------------------------------------------------------------------------
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  Novelty.Model.LogRecordBlcok
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// <para>EventTypeInfo 类</para>
InBlock.gif    
/// <para>操作事件类型</para>
InBlock.gif    
/// <para><see cref="member"/></para>
InBlock.gif    
/// <remarks></remarks>
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class EventTypeInfo : IComparable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
内部成员变量#region 内部成员变量
InBlock.gif        
private int _eventId;
InBlock.gif        
private int _eventParentId;
InBlock.gif        
private string _eventName = string.Empty;
InBlock.gif        
private string _description = string.Empty;
InBlock.gif        
private IList _systemLogs;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认的构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public EventTypeInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
InBlock.gif        
///<param name="eventId">事件源编号</param>
InBlock.gif        
///<param name="eventParentId">事件源父编号</param>
InBlock.gif        
///<param name="eventName">事件源名称</param>
ExpandedSubBlockEnd.gif        
///<param name="description">事件描述</param>

InBlock.gif        public EventTypeInfo(int eventId, int eventParentId, string eventName, string description)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _eventId 
= eventId;
InBlock.gif            _eventParentId 
= eventParentId;
InBlock.gif            _eventName 
= eventName;
InBlock.gif            _description 
= description;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 事件源编号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual int EventId
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _eventId;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_eventId == value)
InBlock.gif                    
return;
InBlock.gif                _eventId 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 事件源父编号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual int EventParentId
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _eventParentId;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_eventParentId == value)
InBlock.gif                    
return;
InBlock.gif                _eventParentId 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 事件源名称
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual string EventName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _eventName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_eventName == value)
InBlock.gif                    
return;
InBlock.gif                _eventName 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 事件描述
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual string Description
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _description;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_description == value)
InBlock.gif                    
return;
InBlock.gif                _description 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 系统日志列表
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual IList SystemLogs
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_systemLogs == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _systemLogs 
= new ArrayList();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _systemLogs;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _systemLogs = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
重载方法#region 重载方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据关键字重新载 Equals 方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj">比较对象</param>
ExpandedSubBlockEnd.gif        
/// <returns>比较结果</returns>

InBlock.gif        public override bool Equals(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!(obj is EventTypeInfo))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new InvalidCastException("比较的对象不是 EventTypeInfo 的类型!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            EventTypeInfo eventTypeInfo 
= (EventTypeInfo)obj;
InBlock.gif            
return _eventId == eventTypeInfo.EventId;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得对象的 hash 值
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override int GetHashCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int hash = 100;
InBlock.gif            hash 
= 100 * _eventId.GetHashCode();
InBlock.gif            
return hash;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 实现 IComparable 接口的比较方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public int CompareTo(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!(obj is EventTypeInfo))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new InvalidCastException("比较的对象不是 EventTypeInfo 的类型!");
ExpandedSubBlockEnd.gif            }

InBlock.gif             EventTypeInfo eventTypeInfo 
= (EventTypeInfo)obj;
InBlock.gif             
return _eventId.CompareTo(eventTypeInfo.EventId);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

None.gif // -----------------------------------------------------------------------------------------
None.gif
//  模块编号:
None.gif
//  文件名: SystemLogInfo.cs
None.gif
//  描述: SystemLogInfo 实体类
None.gif
//  作者:ChenJie 
None.gif
//  编写日期:2007-5-11
None.gif
//  Copyright 2007
None.gif
// -----------------------------------------------------------------------------------------
None.gif
using  System;
None.gif
using  Novelty.Model.UserAccountBlcok;
None.gif
None.gif
namespace  Novelty.Model.LogRecordBlcok
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// <para>SystemLogInfo 类</para>
InBlock.gif    
/// <para>操作日志表</para>
InBlock.gif    
/// <para><see cref="member"/></para>
InBlock.gif    
/// <remarks></remarks>
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SystemLogInfo : IComparable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
内部成员变量#region 内部成员变量
InBlock.gif        
private int _systemLogId;
InBlock.gif        
private DateTime _operationTime;
InBlock.gif        
private string _description = string.Empty;
InBlock.gif        
private string _iPAddress = string.Empty;
InBlock.gif        
private decimal _userSerial;
InBlock.gif        
private EventTypeInfo _eventType;        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认的构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public SystemLogInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>        
InBlock.gif        
///<param name="systemLogId">日志编号</param>        
InBlock.gif        
///<param name="operationTime">操作时间</param>
InBlock.gif        
///<param name="description">事件描述</param>
InBlock.gif        
///<param name="iPAddress">计算机地址</param>
InBlock.gif        
///<param name="userSerial">用户序号</param>
ExpandedSubBlockEnd.gif        
///<param name="eventType">事件对象</param>        

InBlock.gif        public SystemLogInfo(int systemLogId, DateTime operationTime, string description,
InBlock.gif            
string iPAddress, decimal userSerial, EventTypeInfo eventType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _systemLogId 
= systemLogId;
InBlock.gif            _operationTime 
= operationTime;
InBlock.gif            _description 
= description;
InBlock.gif            _iPAddress 
= iPAddress;
InBlock.gif            _eventType 
= eventType;
InBlock.gif            _userSerial 
= userSerial;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 日志编号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual int SystemLogId
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _systemLogId;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_systemLogId == value)
InBlock.gif                    
return;
InBlock.gif                _systemLogId 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 操作时间
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual DateTime OperationTime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _operationTime;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_operationTime == value)
InBlock.gif                    
return;
InBlock.gif                _operationTime 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 事件描述
ExpandedSubBlockEnd.gif        
/// </summary>
InBlock.gif        public virtual string Description
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _description;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_description == value)
InBlock.gif                    
return;
InBlock.gif                _description 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 计算机地址
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual string IPAddress
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _iPAddress;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_iPAddress == value)
InBlock.gif                    
return;
InBlock.gif                _iPAddress 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用户序号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual decimal UserSerial
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _userSerial;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_userSerial == value)
InBlock.gif                    
return;
InBlock.gif                _userSerial 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 事件类型对象
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public virtual EventTypeInfo EventType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _eventType;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_eventType == value)
InBlock.gif                    
return;
InBlock.gif                _eventType 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
重载方法#region 重载方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据关键字重新载 Equals 方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj">比较对象</param>
ExpandedSubBlockEnd.gif        
/// <returns>比较结果</returns>

InBlock.gif        public override bool Equals(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!(obj is SystemLogInfo))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new InvalidCastException("比较的对象不是 SystemLogInfo 的类型!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            SystemLogInfo systemLogInfo 
= (SystemLogInfo)obj;
InBlock.gif            
return _systemLogId == systemLogInfo.SystemLogId;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得对象的 hash 值
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override int GetHashCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int hash = 100;
InBlock.gif            hash 
= 100 * _systemLogId.GetHashCode();
InBlock.gif            
return hash;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 实现 IComparable 接口的比较方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public int CompareTo(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!(obj is SystemLogInfo))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new InvalidCastException("比较的对象不是 SystemLogInfo 的类型!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            SystemLogInfo systemLogInfo 
= (SystemLogInfo)obj;
InBlock.gif            
return _systemLogId.CompareTo(systemLogInfo.SystemLogId);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

EventTypeInfo.hbm.xml:

None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2" >
None.gif    
< class  name ="Novelty.Model.LogRecordBlcok.EventTypeInfo, Novelty.Model"  table ="EventType" >
None.gif        
< id  name ="EventId"  column ="EventId"  type ="Int32"  unsaved-value ="null" >
None.gif            
< generator  class ="assigned"   />
None.gif        
</ id >
None.gif        
< property  name ="EventParentId"  type ="Int32" >
None.gif            
< column  name ="EventParentId"  length ="4"  sql-type ="int"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="EventName"  type ="string" >
None.gif            
< column  name ="EventName"  length ="32"  sql-type ="nvarchar"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="Description"  type ="string" >
None.gif            
< column  name ="Description"  length ="128"  sql-type ="nvarchar"  not-null ="false" />
None.gif        
</ property >
None.gif        
< bag  name ="SystemLogs"  inverse ="true"  lazy ="true"  cascade ="all-delete-orphan" >
None.gif            
< key  column ="EventId" />
None.gif            
< one-to-many  class ="Novelty.Model.LogRecordBlcok.SystemLogInfo, Novelty.Model" />
None.gif        
</ bag >
None.gif    
</ class >
None.gif
</ hibernate-mapping >
None.gif

SystemLogInfo.hbm.xml:

None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2" >
None.gif    
< class  name ="Novelty.Model.LogRecordBlcok.SystemLogInfo, Novelty.Model"  table ="SystemLog" >
None.gif        
< id  name ="SystemLogId"  column ="SystemLogId"  type ="Int32"  unsaved-value ="null" >
None.gif            
< generator  class ="assigned"   />
None.gif        
</ id >
None.gif        
< property  name ="OperationTime"  type ="DateTime" >
None.gif            
< column  name ="OperationTime"  length ="8"  sql-type ="datetime"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="Description"  type ="string" >
None.gif            
< column  name ="Description"  length ="128"  sql-type ="nvarchar"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="IPAddress"  type ="string" >
None.gif            
< column  name ="IPAddress"  length ="15"  sql-type ="nvarchar"  not-null ="false" />
None.gif        
</ property >         
None.gif        
< property  name ="UserSerial"  type ="Decimal" >
None.gif            
< column  name ="UserSerial"  length ="5"  sql-type ="decimal"  not-null ="true" />
None.gif        
</ property >         
None.gif        
< many-to-one  name ="EventType"  class ="Novelty.Model.LogRecordBlcok.EventTypeInfo, Novelty.Model" >
None.gif            
< column  name ="EventId"  length ="4"  sql-type ="int"  not-null ="true"  index ="OperationEvent_FK" />
None.gif        
</ many-to-one >         
None.gif    
</ class >
None.gif
</ hibernate-mapping >
None.gif

        上面的代码太长了,你省略它们也不太影响思路。(为什么不早说?:) )

        我们就举一个数据层访问类,它是操作 SystemLog 表的!

        关于下面的代码,我就解释一下 Insert  方法,它是整个数据层中思路最重要的部分!
        由于EventTypeInfo 和 SystemLogInfo 和 是一对多的关系!所以我们在插入 SystemLogInfo 对象前,先创建EventTypeInfo 对象:
        先获得EventTypeInfo 的实体通用操作对象:
EntityControl<EventTypeInfo> eventTypeEntityControl = CommonDatabaseOperation<EventTypeInfo>.Instance.GetEntityControl(DEFAULT_DATABASE_IDENTIFIER);
        然后通过EventTypeInfo 的实体通用操作对象来获得EventTypeInfo 对象:
EventTypeInfo eventType = eventTypeEntityControl.GetEntity(eventId);
        在这里我们就不需要强制转换对象了,因为使用了范型类!当然,这里还不足以体现它的优越性!
        由于我们声明了全局变量:
private readonly EntityControl<SystemLogInfo> entityControl;
        然后在构造函数中创建它:
entityControl = CommonDatabaseOperation<SystemLogInfo>.Instance.GetEntityControl("SystemDatabase");
        然后在该类的各个方法中都可以使用属于SystemLogInfo 的实体通用操作对象,其中在获得对象或是对象列表时均不需要强制转换对象。
        这个数据访问层类只写了对应实体通用操作类中的一些方法,还应该包含其他方法!文件中没有给出例子,但是我还是举两个例说一下。
        (1)比如批量删除记录,怎么办?听说NH的作者说要么不用NH,要么用SQL!已经用NH,那就只有SQL来进行批量删除记录。(可能是执行SQL 的代码太简单,或者担心破坏面向对象操作的思想,或者其他,我不知道!)但是我们来进行处理SQL,特别是带条件的SQL。因为条件各不相同,所以为什么我不把执行SQL的操作方法抽象出来放在实体通用操作类中,而是在各自需要的数据访问层类写各自的。
我在底层的SessionFactory类中放了一个方法:
public Configuration GetConfiguration(string assemblyName, string nhibernateConfigName, string connectionString);
 
        通过它可以获得 ISessionFactoryImplementor 类的对象,然后再获得连接 IDbConnection 接口对象,然后再获得cmd,将相应的参数值赋上,然后再进行 SQL 操作。
        具体参考底层的 EntityControl 类中方法:
public IList<T> GetEntitiesByExecuteSQL(string sql)
        它给出了执行 SQL 语句的思路。

        (2)多条件查询!
        这个就是完全和 NH相关,还是因为条件各不相同,我无法在实体通用操作类中完成!
        这个可以参考底层的 EntityControl 类中方法:
public IList<T> GetEntities(string propertyName, object value)
        还补充一类情况:
string hql = "FROM SystemLogInfo WHERE UserSerial = :UserSerial ";
IQuery q = session.CreateQuery(hql);
q.SetDemical("UserSerial ", userSerial );
q.SetMaxResults(number);
return q.List();
        这个例子中的session底层的SessionFactory类的方法
public ISession OpenSession(string assemblyName, string nhibernateConfigName, string connectionString)
 中获得。
        这类查询还有很多,这里只是简单举例,目的在于说明数据访问层里面究竟包含一些什么东西。
        《面向对象的NHibernate数据查询语言-HQL》这篇文章关于查询讲的非常好,可供我们好好学习!

None.gif // -----------------------------------------------------------------------------------------
None.gif
//  模块编号:
None.gif
//  文件名: SystemLog.cs
None.gif
//  描述: SystemLog 数据层访问类
None.gif
//  作者:ChenJie 
None.gif
//  编写日期:2007-5-11
None.gif
//  Copyright 2007
None.gif
// -----------------------------------------------------------------------------------------
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Data;
None.gif
using  Novelty.Model.LogRecordBlcok;
None.gif
using  Novelty.CustomSystem;
None.gif
using  Novelty.CustomSystem.NHibernateOperation;
None.gif
using  Novelty.CustomSystem.NHibernateOperation.NHibernateModule;
None.gif
None.gif
namespace  Novelty.CustomSystem.LogStrategy.LogRecordBlcok
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SystemLog表的数据层访问类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    internal class SystemLog
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
常量#region 常量
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认的数据库标识符
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private const string DEFAULT_DATABASE_IDENTIFIER = "SystemDatabase";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// HQL 查询语句
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private const string HQL_MAX_SYSTEM_LOG_ID = "SELECT MAX _MAX;(systemLog.SystemLogId) FROM SystemLogInfo AS systemLog";
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有变量#region 私有变量
InBlock.gif        
private readonly EntityControl<SystemLogInfo> entityControl;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认的构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public SystemLog()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            entityControl 
= CommonDatabaseOperation<SystemLogInfo>.Instance.GetEntityControl("SystemDatabase");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
接口#region 接口
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将日志写入到数据库中
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="eventId">事件关编号</param>
InBlock.gif        
/// <param name="userSerial">用户序号</param>        
InBlock.gif        
/// <param name="clientAddress">客户地址</param>   
ExpandedSubBlockEnd.gif        
/// <param name="description">日志描述</param>

InBlock.gif        public void Insert(int eventId, decimal userSerial, string clientAddress, string description)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                EntityControl
<EventTypeInfo> eventTypeEntityControl = CommonDatabaseOperation<EventTypeInfo>.Instance.GetEntityControl(DEFAULT_DATABASE_IDENTIFIER);
InBlock.gif                EventTypeInfo eventType 
= eventTypeEntityControl.GetEntity(eventId);
InBlock.gif                
int systemLogId = entityControl.GetDataFieldValue<int>(HQL_MAX_SYSTEM_LOG_ID, 0+ 1;
InBlock.gif                SystemLogInfo systemLogInfo 
=
InBlock.gif                    
new SystemLogInfo(systemLogId, DateTime.Now, description, clientAddress, userSerial, eventType);
InBlock.gif                entityControl.AddEntity(systemLogInfo);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//记录日志, 抛出异常, 不包装异常 
InBlock.gif
                ExceptionFacade.LogAndThrowAndNoWrapPolicy(exception);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得 SystemLogInfo 对象
InBlock.gif        
/// </summary>
InBlock.gif        
///<param name="systemLogId">日志编号</param>
ExpandedSubBlockEnd.gif        
/// <returns> SystemLogInfo 对象</returns>

InBlock.gif        public SystemLogInfo GetSystemLogInfo(int systemLogId)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return entityControl.GetEntity(systemLogId);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得 SystemLogInfo 对象的列表
InBlock.gif        
/// </summary>        
ExpandedSubBlockEnd.gif        
/// <returns>SystemLogInfo 对象列表</returns>

InBlock.gif        public IList<SystemLogInfo> GetSystemLogInfos()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return entityControl.GetEntities();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得 SystemLogInfo 对象的数据集
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>SystemLogInfo 对象的数据集</returns>

InBlock.gif        public DataSet GetAll()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return entityControl.ConvertToDataSet(entityControl.GetEntities());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 更新 SystemLogInfo 对象
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="systemLogInfo">SystemLogInfo 对象</param>

InBlock.gif        public void Update(SystemLogInfo systemLogInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                entityControl.UpdateEntity(systemLogInfo, systemLogInfo.SystemLogId);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//记录日志, 抛出异常, 不包装异常 
InBlock.gif
                ExceptionFacade.LogAndThrowAndNoWrapPolicy(exception);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除满足条件的的 SystemLogInfo 对象
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="userSerial">用户序号</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回删除的记录数目数目</returns>

InBlock.gif        public int Delete(decimal userSerial)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int count = 0;
InBlock.gif
InBlock.gif            
return count;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除 SystemLogInfo 对象
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="systemLogInfo">SystemLogInfo 对象</param>

InBlock.gif        public void Delete(SystemLogInfo systemLogInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                entityControl.DeleteEntity(systemLogInfo);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//记录日志, 抛出异常, 不包装异常 
InBlock.gif
                ExceptionFacade.LogAndThrowAndNoWrapPolicy(exception);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 

NHibernate 配置文件的处理和使用多数据库的多层架构思路(第一部分)
http://www.cnblogs.com/scucj/archive/2007/05/15/747688.html
NHibernate 配置文件的处理和使用多数据库的多层架构思路(第三部分)
http://www.cnblogs.com/scucj/archive/2007/05/15/747698.html

转载于:https://www.cnblogs.com/scucj/archive/2007/05/15/747695.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值