简体   繁体   English

NHibernate的通用实体

[英]Generic Entity for NHibernate

I am trying to write a sort of generic data warehouse using NHibernate but I have run into a couple of issues. 我正在尝试使用NHibernate编写一种通用数据仓库,但是遇到了两个问题。

I have a class defined as such: 我有一个这样定义的类:

public class Entity
{
    public Entity() { }
    public Entity (string schemaName) 
    {
        this.SchemaName = schemaName;
    }

    [DataMember]
    public string SchemaName { get; set; }

    [DataMember]
    public Guid Id {
        get { return this.Attributes.ContainsKey("id") ? (Guid)this.Attributes["id"] : Guid.Empty; }  
        set { this.Attributes ["id"] = value; }  
    }

    [DataMember]
    public Dictionary<string, object> Attributes = new Dictionary<string, object>();
}

What i'd like to do is use the Attribute dictionary as the columns for a table and the SchemaName as the table name in SQL. 我想做的是在SQL中使用Attribute字典作为表的列,将SchemaName作为表名。 I have a couple of wrapper functions to insert/retrieve/delete but it can only do ~170/second. 我有几个包装器函数可以插入/检索/删除,但它只能执行〜170 /秒。 I would like to improve this by using native NHibernate methods. 我想通过使用本机NHibernate方法来改善这一点。 I could technically generate a class in memory based on the column information and feed that to NHibernate but I don't think that is the best option. 从技术上讲,我可以根据列信息在内存中生成一个类,并将其提供给NHibernate,但我认为这不是最好的选择。

How would I accomplish this? 我将如何完成? Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

Currently I have something that generates the queries based on information passed in and then execute it by using CreateSqlQuery on the NHibernate session object. 目前,我有一些可以根据传入的信息生成查询,然后通过在NHibernate会话对象上使用CreateSqlQuery来执行查询。

So the answer to this question comes directly from NHibernates documentation, which I found in this post: How to save a dynamic object in nhibernate 因此,此问题的答案直接来自NHibernates文档,我在本文中找到了该文档: 如何在nhibernate中保存动态对象

The solution comes from Dynamic Models and in which you need to configure the session to handle this. 该解决方案来自动态模型,您需要在其中配置会话以进行处理。

Entity representation modes can also be set on a per ISession basis: 实体表示模式也可以在每个ISession的基础上设置:

using (ISession dynamicSession = pocoSession.GetSession(EntityMode.Map))
{
    // Create a customer
    var frank = new Dictionary<string, object>();
    frank["name"] = "Frank";
    dynamicSession.Save("Customer", frank);
    ...
}
// Continue on pocoSession

The other alternative is to setup the session factory to do this for you; 另一种选择是设置会话工厂来为您执行此操作。 however, the documentation is unclear ... or I was too lazy to find it ... on how to achieve this. 但是,文档尚不清楚...或者我懒得找到它...如何实现这一目标。

Thank you @Radim Köhler for posting the documentation for this. 感谢@RadimKöhler发布了有关此文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM