NHiernate中自定义Generator

我的这个自定义的Generator设置如下:

None.gif<generator class="HYLQ.Core.Domain.StreamGenerator, HYLQ.Core">
None.gif        
<param name="ObjectName">Child</param>
None.gif      
</generator>
这样我会为每个ObjectName都会产生一个顺序的流水号。
StreamGenerator的代码:
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Data.SqlClient;
None.gif
using System.Runtime.CompilerServices;
None.gif
using System.Collections.Generic;
None.gif
using System.Collections;
None.gif
using System.Text;
None.gif
using NHibernate.Id;
None.gif
using NHibernate.Engine;
None.gif
using NHibernate.SqlCommand;
None.gif
using NHibernate.SqlTypes;
None.gif
using NHibernate.Type;
None.gif
using NHibernate.Util;
None.gif
using NHibernate.Dialect;
None.gif
using NHibernate.Mapping;
None.gif
None.gif
namespace HYLQ.Core.Domain
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
class StreamGenerator : IPersistentIdentifierGenerator, IConfigurable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary></summary>
InBlock.gif        public const string ObjectNameTarget = "ObjectName";
InBlock.gif
InBlock.gif        
public const string procNameTarget = "ProcedureName";
InBlock.gif
InBlock.gif        
private string objectName;
InBlock.gif        
private string procName = "usp_GetID";
InBlock.gif        
private long next;
InBlock.gif
InBlock.gif        
private System.Type returnClass;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="type"></param>
InBlock.gif        
/// <param name="parms"></param>
ExpandedSubBlockEnd.gif        
/// <param name="d"></param>

InBlock.gif
InBlock.gif        
public void Configure(IType type, IDictionary parms, Dialect d)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            objectName 
= parms[ObjectNameTarget] as string;
InBlock.gif
InBlock.gif            
if (parms[procNameTarget] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                procName 
= parms[procNameTarget] as string;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            returnClass 
= type.ReturnedClass;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="session"></param>
InBlock.gif        
/// <param name="obj"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        [MethodImpl(MethodImplOptions.Synchronized )]
InBlock.gif        
public object Generate(ISessionImplementor session, object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (objectName != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                getNext(session);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return IdentifierGeneratorFactory.CreateNumber(next, returnClass);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void getNext(ISessionImplementor session)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            SqlConnection conn 
= (SqlConnection)session.Factory.OpenConnection();
InBlock.gif            SqlCommand qps 
= conn.CreateCommand();
InBlock.gif            qps.CommandText 
= procName;
InBlock.gif            qps.CommandType 
= CommandType.StoredProcedure;
InBlock.gif            qps.Parameters.Add(
new SqlParameter("@ObjectName", SqlDbType.VarChar,10));
InBlock.gif            qps.Parameters[
"@ObjectName"].Value = objectName;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                next 
= int.Parse(qps.ExecuteScalar().ToString());
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.Factory.CloseConnection( conn );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dialect"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public string[] SqlCreateStrings(Dialect dialect)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new string[0];
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dialect"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public string SqlDropString(Dialect dialect)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary></summary>
InBlock.gif        public object GeneratorKey()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return objectName;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

这里是调用一个存储过程来生成流水号的。
存储过程和表的定义如下:
None.gif--流水号
None.gif
create table Stream
None.gif(
None.gif    ObjectName        
varchar(10)        not null,        --对象名
None.gif
    MaxID            int                not null,        --流水号
None.gif
    primary key(ObjectName)
None.gif)
None.gif
go
None.gif
None.gif
--流水号发生器
None.gif
if exists(select * from sysobjects where type='p' and name='usp_GetID')
None.gif    
drop proc usp_GetID
None.gif
go
None.gif
create proc usp_GetID
None.gif(
None.gif    
@ObjectName        varchar(10)            --对象名
None.gif
)
None.gif
as
None.gif
begin
None.gif    
declare @MaxID int
None.gif    
SET NOCOUNT ON
None.gif
None.gif    
if exists(select * from Stream where ObjectName=@ObjectName)
None.gif    
begin
None.gif        
select @MaxID=MaxID from Stream where ObjectName=@ObjectName
None.gif        
update Stream set MaxID=MaxID+1 where ObjectName=@ObjectName
None.gif    
end
None.gif    
else
None.gif    
begin
None.gif        
set @MaxID=1
None.gif        
insert into Stream values(@ObjectName,2)
None.gif    
end
None.gif    
None.gif    
select @MaxID as MaxID
None.gif
end
None.gif
go

希望对大家能有所帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值