zoukankan      html  css  js  c++  java
  • NHiernate中自定义Generator

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

    <generator class="HYLQ.Core.Domain.StreamGenerator, HYLQ.Core">
            
    <param name="ObjectName">Child</param>
          
    </generator>
    这样我会为每个ObjectName都会产生一个顺序的流水号。
    StreamGenerator的代码:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.CompilerServices;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;
    using NHibernate.Id;
    using NHibernate.Engine;
    using NHibernate.SqlCommand;
    using NHibernate.SqlTypes;
    using NHibernate.Type;
    using NHibernate.Util;
    using NHibernate.Dialect;
    using NHibernate.Mapping;

    namespace HYLQ.Core.Domain
    {
        
    class StreamGenerator : IPersistentIdentifierGenerator, IConfigurable
        
    {


            
    /// <summary></summary>
            public const string ObjectNameTarget = "ObjectName";

            
    public const string procNameTarget = "ProcedureName";

            
    private string objectName;
            
    private string procName = "usp_GetID";
            
    private long next;

            
    private System.Type returnClass;

            
    /// <summary>
            
    ///
            
    /// </summary>
            
    /// <param name="type"></param>
            
    /// <param name="parms"></param>
            
    /// <param name="d"></param>


            
    public void Configure(IType type, IDictionary parms, Dialect d)
            
    {
                objectName 
    = parms[ObjectNameTarget] as string;

                
    if (parms[procNameTarget] != null)
                
    {
                    procName 
    = parms[procNameTarget] as string;
                }


                returnClass 
    = type.ReturnedClass;
            }


            
    /// <summary>
            
    ///
            
    /// </summary>
            
    /// <param name="session"></param>
            
    /// <param name="obj"></param>
            
    /// <returns></returns>

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

                
    return IdentifierGeneratorFactory.CreateNumber(next, returnClass);
            }


            
    private void getNext(ISessionImplementor session)
            
    {

                SqlConnection conn 
    = (SqlConnection)session.Factory.OpenConnection();
                SqlCommand qps 
    = conn.CreateCommand();
                qps.CommandText 
    = procName;
                qps.CommandType 
    = CommandType.StoredProcedure;
                qps.Parameters.Add(
    new SqlParameter("@ObjectName", SqlDbType.VarChar,10));
                qps.Parameters[
    "@ObjectName"].Value = objectName;

                
    try
                
    {
                    next 
    = int.Parse(qps.ExecuteScalar().ToString());
                    
                }

                
    catch 
                
    {
                    
    throw;
                }

                
    finally
                
    {
                    session.Factory.CloseConnection( conn );
                }

            }


            
    /// <summary>
            
    ///
            
    /// </summary>
            
    /// <param name="dialect"></param>
            
    /// <returns></returns>

            public string[] SqlCreateStrings(Dialect dialect)
            
    {
                
    return new string[0];
            }


            
    /// <summary>
            
    ///
            
    /// </summary>
            
    /// <param name="dialect"></param>
            
    /// <returns></returns>

            public string SqlDropString(Dialect dialect)
            
    {
                
    return null;
            }


            
    /// <summary></summary>
            public object GeneratorKey()
            
    {
                
    return objectName;
            }

        }

    }

    这里是调用一个存储过程来生成流水号的。
    存储过程和表的定义如下:
    --流水号
    create table Stream
    (
        ObjectName        
    varchar(10)        not null,        --对象名
        MaxID            int                not null,        --流水号
        primary key(ObjectName)
    )
    go

    --流水号发生器
    if exists(select * from sysobjects where type='p' and name='usp_GetID')
        
    drop proc usp_GetID
    go
    create proc usp_GetID
    (
        
    @ObjectName        varchar(10)            --对象名
    )
    as
    begin
        
    declare @MaxID int
        
    SET NOCOUNT ON

        
    if exists(select * from Stream where ObjectName=@ObjectName)
        
    begin
            
    select @MaxID=MaxID from Stream where ObjectName=@ObjectName
            
    update Stream set MaxID=MaxID+1 where ObjectName=@ObjectName
        
    end
        
    else
        
    begin
            
    set @MaxID=1
            
    insert into Stream values(@ObjectName,2)
        
    end
        
        
    select @MaxID as MaxID
    end
    go

    希望对大家能有所帮助!

  • 相关阅读:
    JavaWeb项目自动部署,持续集成
    hbase系列
    传输视频的带宽如何计算?传输4K视频需要多少带宽?
    TSINGSEE青犀视频通过Webrtc编译android版本找不到gzip模块如何处理?
    TSINGSEE青犀视频webrtc相关内容编译如何在c++端编码出H264?
    视频组网/网络穿透工具EasyNTS如何永久删除其中某个设备?
    音视频流媒体平台的开发,开源EasyDarwin为什么如此受欢迎?
    基于音视频的云会议为什么会迎来发展的大爆发?
    EasyRTC的Web开发过程中如何创建新的空分支?
    C# 会话,进程,线程,线程安全
  • 原文地址:https://www.cnblogs.com/maplye/p/442322.html
Copyright © 2011-2022 走看看