Created
May 23, 2012 05:26
-
-
Save dbones/2773407 to your computer and use it in GitHub Desktop.
Basic Hilo Id Generator class,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Text; | |
namespace TestEtl.Infrastructure | |
{ | |
public interface IIdGenerator | |
{ | |
object GenerateKey(string table); | |
} | |
public class GuidIdGenerator : IIdGenerator | |
{ | |
public object GenerateKey(string table) | |
{ | |
return Guid.NewGuid(); | |
} | |
} | |
public class HiloIdGenerator : IIdGenerator | |
{ | |
private readonly string _connectionName; | |
public string TableName { get; set; } | |
public string HiColumn { get; set; } | |
public int MaxLow { get; set; } | |
public string Where { get; set; } | |
private readonly IDictionary<string, TableHilo> _hilos; | |
public HiloIdGenerator(string connectionName) | |
{ | |
_connectionName = connectionName; | |
TableName = "Hilo"; | |
HiColumn = "NextHiValue"; | |
MaxLow = 1000; | |
Where = "where Entity = '{0}'"; | |
_hilos = new Dictionary<string, TableHilo>(); | |
} | |
public object GenerateKey(string table) | |
{ | |
if (!_hilos.ContainsKey(table)) | |
{ | |
var hilo = new TableHilo {Name = table}; | |
_hilos.Add(table, hilo); | |
} | |
var tableHilo = _hilos[table]; | |
if (!tableHilo.CurrentHi.HasValue || tableHilo.NextId > MaxLow) | |
{ | |
tableHilo.CurrentHi = GetHi(table); | |
tableHilo.NextId = 1; | |
} | |
int id = tableHilo.NextId + (tableHilo.CurrentHi.Value * MaxLow); | |
tableHilo.NextId++; | |
return id; | |
} | |
protected virtual int GetHi(string table) | |
{ | |
using (var conn = CreateConnection(_connectionName)) | |
{ | |
var cmd = conn.CreateCommand(); | |
cmd.CommandText = SqlCmdText(table); | |
var idParameter = cmd.CreateParameter(); | |
idParameter.ParameterName = IdParamName(); | |
idParameter.Direction = ParameterDirection.Output; | |
idParameter.DbType = DbType.Int32; | |
cmd.Parameters.Add(idParameter); | |
conn.Open(); | |
cmd.ExecuteNonQuery(); | |
conn.Close(); | |
return (int)idParameter.Value; | |
} | |
} | |
private string SqlCmdText(string table) | |
{ | |
string where = string.IsNullOrEmpty(Where) | |
? "" | |
: string.Format(Where, table); | |
StringBuilder cmd = new StringBuilder(); | |
cmd.AppendFormat("SELECT {3} = {0} FROM {1} {2};", HiColumn, TableName, where, IdParamName()); | |
cmd.AppendFormat("\r\nUPDATE {0} SET {1} = {3} + 1 {2};", TableName, HiColumn, where, IdParamName()); | |
return cmd.ToString(); | |
} | |
protected virtual string IdParamName() | |
{ | |
return "@Id"; | |
} | |
protected virtual IDbConnection CreateConnection(string connectionName) | |
{ | |
return new SqlConnection(ConfigurationManager.ConnectionStrings[connectionName].ConnectionString); | |
} | |
public class TableHilo | |
{ | |
public string Name { get; set; } | |
public int NextId { get; set; } | |
public int? CurrentHi { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
simple version of a Hilo Class, feel free to use (its only had minimal testing)