使用nNHibernate与释放code [英] Using nNHibernate with Emitted Code

查看:224
本文介绍了使用nNHibernate与释放code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发,将作为后端的数据洁具的房子,在事实上将定义从一个XML文件和相应的事实来阅读软件/维度表会在运行中产生。

我设法使它工作即code创建表,更新表结构如果可能的话,否则删除表并作出新的,它可以将数据插入到表中,我们可以从我们的客户查询的数据仓库的应用程序也是如此。到目前为止好。

现在我有两个问题。

1)太多的SQL语句。问题是:这将是恶梦维持

2)太多的SQL语句。问题:我被要求支持意味着更多的SQL statments多个数据库

我必须承认,我没有太多的工作与任一(n)的休眠或Reflection.Emit的。

但我不知道它如何努力将生成类使用Reflection.Emit的我的表,然后使用ActiveRecord / NHibernate的访问数据?所以,我没有做直接处理数据库的肮脏的工作。

解决方案

 类事实
{
    公共虚拟INT标识{获取;组; }
    IDictionary的属性{获得;组; }
}
 

模板

 <休眠映射>
  <类名=事实>
    <动态组件名称=属性>
      <! - 占位符 - >
    &所述; /动态成分>
  < /类>
< /休眠映射>
 

替换<! - 占位符 - > 与生成

 <物业
  NAME =P1
  TYPE =INT/>
<物业
  NAME =P2
  TYPE =字符串/>
 

构建

  VAR DOC =新System.Xml.XmlDocument();
doc.LoadXml(generatedXml);

新NHibernate.Cfg.Configuration()
    .AddDocument(DOC)
...
    .BuildSessionFactory();
 

查询

  VAR的查询= session.CreateCriteria<事实>();

的foreach(在限制VAR限制)
{
    query.Add(Restrictions.Eq(restriction.Name,restriction.Value))
}

VAR事实= query.List<事实>();

SendBack(事实);
 

创建/删除表

  VAR方言= Dialect.GetDialect(config.Properties);
VAR defaultCatalog = PropertiesHelper.GetString(NHibernate.Cfg.Environment.DefaultCatalog,config.Properties,NULL);
VAR defaultSchema = PropertiesHelper.GetString(NHibernate.Cfg.Environment.DefaultSchema,config.Properties,NULL);

VAR createTableSql = config.GetClassMapping(typeof运算(FACT))Table.SqlCreateString(方言,config.BuildMapping(),defaultCatalog,defaultSchema)。
。VAR dropTableSql = config.GetClassMapping(typeof运算(INT))Table.SqlDropString(方言,defaultCatalog,defaultSchema);
 

I am developing a software that will serve as back-end for a data-ware house, in which fact definitions will be read from an xml file and corresponding fact/dimension tables will be created on the fly.

I have managed to make it work i.e. the code creates tables, updates table structure if possible otherwise drop table and make an new one, it can insert data into tables and we can query data-ware house from our client application as well. So far so good.

Now I have two problems

1) Too many sql statements. The problem: it will be night mare to maintain

2) Too many sql statements. The problem: I have been asked to support multiple databases that implies more sql statments.

I have to admit that I have not worked much with either of (n)Hibernate or Reflection.Emit.

But I was wondering how hard it would be to generate classes for my tables using Reflection.Emit and then use ActiveRecord/nHibernate to access data? So that I don't have to do the dirty work of dealing with a database directly.

解决方案

class Fact
{
    public virtual int Id { get; set; }
    IDictionary Properties { get; set; }
}

Template

<hibernate-mapping>
  <class name="Fact">
    <dynamic-component name="Properties">
      <!--placeholder -->
    </dynamic-component>
  </class>
</hibernate-mapping>

replace <!--placeholder --> with generated

<property
  name="P1"
  type="int" />
<property
  name="P2"
  type="string" />

Build

var doc = new System.Xml.XmlDocument();
doc.LoadXml(generatedXml);

new NHibernate.Cfg.Configuration()
    .AddDocument(doc)
...
    .BuildSessionFactory();

Query

var query = session.CreateCriteria<Fact>();

foreach (var restriction in restrictions)
{
    query.Add(Restrictions.Eq(restriction.Name, restriction.Value))
}

var facts = query.List<Fact>();

SendBack(facts);

creating/dropping the table

var dialect = Dialect.GetDialect(config.Properties);
var defaultCatalog = PropertiesHelper.GetString(NHibernate.Cfg.Environment.DefaultCatalog, config.Properties, null);
var defaultSchema = PropertiesHelper.GetString(NHibernate.Cfg.Environment.DefaultSchema, config.Properties, null);

var createTableSql = config.GetClassMapping(typeof(Fact)).Table.SqlCreateString(dialect, config.BuildMapping(), defaultCatalog, defaultSchema);
var dropTableSql = config.GetClassMapping(typeof(int)).Table.SqlDropString(dialect, defaultCatalog, defaultSchema);

这篇关于使用nNHibernate与释放code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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