NHibernate动态映射 [英] NHibernate dynamic mapping

查看:107
本文介绍了NHibernate动态映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些方法来动态地映射数据库表类在我的应用程序中使用nhibernate(或者如果一些其他的ORM作品,然后让我知道)。我对nhibernate相当陌生,过去我使用过实体框架。



我的应用程序大部分将使用静态结构和流畅的nhibernate来映射它们。然而,有多个数据库表需要创建并映射到每个安装站点的对象。这些都将作为一个基础结构(编号,名称等),但他们将有额外的领域取决于他们正在捕获的数据的类型。从一些阅读中,我发现我可以使用xml中的dynamic-component映射来使用IDictionary Attributes属性添加字段。这是第一步,看起来比较直接。 Ref(http://ayende.com/blog/3942/nhibernate-mapping-dynamic-component)



第二步是我挣扎的地方。我将需要定义表格并根据客户的需要来映射它们。如上所述,每个表将具有一组静态属性和一些动态属性。他们还需要引用一个静态的Location类,如下所示:

Location(STATIC)(id,coordinates)
---- -DynamicTable1(DYNAMIC)(id,Name,location_id,DynamicAttribute1,DynamicAttribute2 ........)
----- DynamicTable2(DYNAMIC)(id,Name,location_id,DynamicAttributeA,DynamicAttributeB ....)



我们将需要能够创建/映射尽可能多的这些DynamicTables作为客户端需要。 DynamicTable1,DynamicTable2等在大多数客户端网站的某些方面很可能会有所不同。有没有办法在这个nhibernate?数据库中表的创建/管理将在其他地方进行管理,我只是需要一些方法来将其映射到我的ORM中。



有点背景>
此应用程序将用于存储地质数据。由于地质数据本质上是不同的,地质学家正在使用不同的方法,寻找不同的元素(黄金,煤炭等),所以存储这些信息的数据结构需要非常灵活。

解决方案

看一下新的按代码映射 NH 3.2的功能。在运行时应该很容易创建新的表格定义。与Fluent相比,您不需要编写映射类,您只需在for循环中添加新类即可:

  //使用SQL或SMO或其他任何方式查找数据库中的所有动态表$ var $ dynamicTables = GetDynamicTables(); 

//映射所有动态表
foreach(var table in dynamicTables)
{
mapper.Class< MyGenericEntity>(ca =>
{
//使用实体名来区分映射
ca.EntityName(table.Name);
ca.Id(x => x.Id,map =>
$ b map.Column(Id);
map.Generator(Generators.HighLow,gmap => gmap.Params(new {max_low = 100}));
} );
//地图属性,使用什么是必需的:if's,for's ...
ca.Property(x => x.Something,map => map.Length(150)) ;
});





$ b

使用实体名称可以在不同的表格中存储和载入实体,即使它们映射为相同的实体类。这就像使用NHibernate输入鸭子。。 p>

相信我,这并不容易。如果你对每个NH专家都有很大的挑战感兴趣,那么就去做吧。如果你只是想让它工作,你应该选择一个更经典的方法:创建一个静态数据库模型,它能够以通用的方式存储动态数据(比如:名称值对)。

I am looking for some way to dynamically map database tables classes in my application using nhibernate (or if some other ORM works then let me know). I am fairly new to nhibernate, I used entity frameworks in the past though.

Most of my application will be using a static structures and fluent nhibernate to map them.

However there are multiple database tables that will be needed to be created and mapped to objects at each install site. These will all have as a base structure (id,name etc) however they will have additional fields depending on the type of data they are capturing. From some reading I found that I can use the "dynamic-component" mapping in xml to add fields using an IDictionary Attributes property. This is the first step and seems relatively straight forward. Ref (http://ayende.com/blog/3942/nhibernate-mapping-dynamic-component)

The second step is where I am struggling. I will need to define tables and map them depending on the client’s need. As stated above each of the tables will have a set of static properties, and some dynamic ones. They will also need to reference a static "Location"Class as shown below

Location (STATIC) (id,coordinates)
-----DynamicTable1 (DYNAMIC) (id,Name,location_id, DynamicAttribute1, DynamicAttribute2........)
-----DynamicTable2 (DYNAMIC) (id,Name,location_id, DynamicAttributeA, DynamicAttributeB....)

We will need to be able to create / map as many of these DynamicTables as the client needs. DynamicTable1, DynamicTable2 etc will most likely be different in some ways for most client sites. Is there any way in nhibernate to achieve this? The creating / management of the tables in the Database will be managed elsewhere, I just need some way to get this to map in my ORM.

A bit of background
This application will be used to store geological data. As geological data is inherently different depending on where it is, and geologist are using different methods and looking for different elements (gold, coal etc), the data structure to store this information needs to be extremely flexible.

解决方案

Take a look at the new Mapping By Code functionality of NH 3.2. It should make it easy to create new table definitions at runtime. In contrast to Fluent, you don't need to write a mapping class, you just can add new classes in for loops:

// lookup all dynamic tables in the database using SQL or SMO or whatever
var dynamicTables = GetDynamicTables();

// map all dynamic tables
foreach(var table in dynamicTables)
{
  mapper.Class<MyGenericEntity>(ca =>
  {
      // use an entity name to distinguish the mappings.
      ca.EntityName(table.Name);
      ca.Id(x => x.Id, map =>
      {
          map.Column("Id");
          map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
      });
      // map properties, using what ever is required: if's, for's ...
      ca.Property(x => x.Something, map => map.Length(150));
  });
}

Using the entity name you can store and load the entities to and from different tables, even if they are mapped as the same entity class. It is like Duck Typing With NHibernate..

Believe me, it won't be easy. If you are interested in a big challenge which impresses every NH expert, just go for it. If you just want to get it working you should choose a more classic way: create a static database model which is able to store dynamic data in a generic way (say: name value pairs).

这篇关于NHibernate动态映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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