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

希望對大家能有所幫助!

發佈了7 篇原創文章 · 獲贊 0 · 訪問量 3009
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章