流利的nHibernate,Hi-Lo表与实体每行使用约定 [英] Fluent nHibernate, Hi-Lo table with entity-per-row using a convention

查看:120
本文介绍了流利的nHibernate,Hi-Lo表与实体每行使用约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来指定一个表来使用Hi-Lo值,每个实体都有一个按行的条目,通过约定(同时还有nHibernate为你创建表结构)?我想复制Phil Haydon在这里的博客,但不必手动管理表格。就目前而言,只有在表中已经为表格中的TableKey创建了相应的条目后,才能将每行表格代码移植到自己的约定中。



<或者,这是可能的通过XML映射?



如果一切都失败了,是唯一的其他适当的选择使用自定义生成器,la 这篇文章

解决方案

Fabio Maulo b
$ b

通过代码示例映射:

  mapper.BeforeMapClass + =(mi,type,map)=> 
map.Id(idmap => idmap.Generator(Generators.HighLow,
gmap => gmap.Params(new
{
table =NextHighValues,
column =NextHigh,
max_lo = 100,
where = string.Format(
EntityName ='{0}',type.Name.ToLowerInvariant())
})));

对于FluentNHibernate,您可以这样做:

  public class PrimaryKeyConvention:IIdConvention 
{
public void Apply(IIdentityInstance instance)
{
var type = instance.EntityType.Name ;
instance.Column(type +Id);
instance.GeneratedBy.HiLo(type,NextHigh,100,
x => x.AddParam(where,String.Format(EntityName ='{0}'),type ));
}
}

另外,Fabio解释了如何使用 IAuxiliaryDatabaseObject 创建Hi-Lo脚本。
$ b

  private static IAuxiliaryDatabaseObject CreateHighLowScript 
IModelInspector inspector,IEnumerable< Type>实体)
{
var script = new StringBuilder(3072);
script.AppendLine(DELETE FROM NextHighValues;); $ b $ (
ALTER TABLE NextHighValues ADD EntityName VARCHAR(128)NOT NULL;);
script.AppendLine(
CREATE NONCLUSTERED INDEX IdxNextHighValuesEntity ON NextHighValues
+ (EntityName ASC););
script.AppendLine(GO);

foreach(entity.Where中的var实体(x => inspector.IsRootEntity(x)) )
{
script.App endLine(string.Format(
INSERT INTO [NextHighValues](EntityName,NextHigh)VALUES('{0}',1);,
entity.Name.ToLowerInvariant()));


返回新SimpleAuxiliaryDatabaseObject(
script.ToString(),null,new HashedSet< string> {
typeof(MsSql2005Dialect).FullName,typeof(MsSql2008Dialect) .FullName
});

$ / code


$您可以像这样使用它:

  configuration.AddAuxiliaryDatabaseObject(CreateHighLowScript(
modelInspector,Assembly.GetExecutingAssembly()。GetExportedTypes()));


Is there a way to specify a table to use for Hi-Lo values, with each entity having a per-row entry, via a convention (while still having nHibernate create the table structure for you)? I would like to replicate what Phil Haydon blogged about here, but without having to manually manage the table. As it stands, migrating his row-per-table code to its own convention will work only if you've already created the appropriate entries for 'TableKey' in the table already.

Alternatively, is this possible via the XML mappings?

And if all else fails, is the only other appropriate option to use a custom generator, a la this post?

解决方案

Fabio Maulo talked about this in one of his mapping-by-code posts.

Mapping by code example:

mapper.BeforeMapClass += (mi, type, map) =>
    map.Id(idmap => idmap.Generator(Generators.HighLow,
        gmap => gmap.Params(new
        {
            table = "NextHighValues",
            column = "NextHigh",
            max_lo = 100,
            where = string.Format(
                "EntityName = '{0}'", type.Name.ToLowerInvariant())
        })));

For FluentNHibernate, you could do something like:

public class PrimaryKeyConvention : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        var type = instance.EntityType.Name;
        instance.Column(type + "Id");
        instance.GeneratedBy.HiLo(type, "NextHigh", "100", 
            x => x.AddParam("where", String.Format("EntityName = '{0}'", type));
    }
}

Also, Fabio explained how you could use IAuxiliaryDatabaseObject to create Hi-Lo script.

private static IAuxiliaryDatabaseObject CreateHighLowScript(
    IModelInspector inspector, IEnumerable<Type> entities)
{
    var script = new StringBuilder(3072);
    script.AppendLine("DELETE FROM NextHighValues;");
    script.AppendLine(
        "ALTER TABLE NextHighValues ADD EntityName VARCHAR(128) NOT NULL;");
    script.AppendLine(
        "CREATE NONCLUSTERED INDEX IdxNextHighValuesEntity ON NextHighValues " 
        + "(EntityName ASC);");
    script.AppendLine("GO");

    foreach (var entity in entities.Where(x => inspector.IsRootEntity(x)))
    {
        script.AppendLine(string.Format(
         "INSERT INTO [NextHighValues] (EntityName, NextHigh) VALUES ('{0}',1);",
         entity.Name.ToLowerInvariant()));
    }

    return new SimpleAuxiliaryDatabaseObject(
        script.ToString(), null, new HashedSet<string> {
           typeof(MsSql2005Dialect).FullName, typeof(MsSql2008Dialect).FullName 
        });
}

You would use it like this:

configuration.AddAuxiliaryDatabaseObject(CreateHighLowScript(
    modelInspector, Assembly.GetExecutingAssembly().GetExportedTypes()));

这篇关于流利的nHibernate,Hi-Lo表与实体每行使用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆