将 nNHibernate 与发出的代码结合使用

发布于 2024-11-26 13:26:56 字数 409 浏览 1 评论 0原文

我正在开发一个软件,该软件将用作数据仓库的后端,其中将从 xml 文件读取事实定义,并动态创建相应的事实/维度表。

我已经设法使其工作,即代码创建表,如果可能的话更新表结构,否则删除表并创建一个新表,它可以将数据插入表中,我们也可以从客户端应用程序查询数据仓库。到目前为止,一切都很好。

现在我有两个问题

1)sql语句太多。问题:维护起来会是一场噩梦

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

我必须承认,我对 (n)Hibernate 或 Reflection.Emit 的使用不多。

但我想知道使用 Reflection.Emit 为我的表生成类然后使用 ActiveRecord/nHibernate 访问数据有多困难?这样我就不必直接处理数据库的脏活了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-12-03 13:26:56
class Fact
{
    public virtual int Id { get; set; }
    IDictionary Properties { get; set; }
}

模板

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

替换为生成的

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

构建

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

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

查询,

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);

创建/删除表

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);
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文