探索NHibernate的映射 [英] Exploring nhibernate mapping
问题描述
一个代码生成器需要提取和生成的NHibernate映射的一些元数据,我想知道如何店NHibernate的关系。
有关多对一关系的另一面是如何存储(一对一一部分)
域模型:
公共类Person
{
公众详细{获取;集;}
}
公共类详细
{
公众人物{获取;集;}
}
遍历图:
地图对PersistentClass = _config.GetClassMapping(typeof运算(人));
属性道具= map.GetProperty(详细信息);
//如何找到关联的属性(即Detail.Person')
配置,后来 ISessionFactory
从它建立,都保持约持久化的信息。他们可能代表的实体或集合。
在这里我们可以看到,我们可以得到任何持久化类:
类的持留:
VAR的EntityType = typeof运算(TEntity)已;
变种厂= ... //获取你的ISessionFactory
VAR持留= factory.GetClassMetadata(的EntityType)为AbstractEntityPersister;
和我们还可以观察采集,持久
集合持留:
//抽象集合持留
VAR collectionPersister =工厂
.GetCollectionMetadata(角色名)作为AbstractCollectionPersister;
//这里,我们去:
VAR isManyToMany = collectionPersister.IsManyToMany;
VAR isOneToMany = collectionPersister.IsOneToMany;
所以,在一般情况下,什么被映射,它被表示为持留。希望它能帮助
扩展,找出一到一
根据以上的东西,对于显式搜索一到一
可能是这样的:
VAR持留= factory.GetClassMetadata(的EntityType)为AbstractEntityPersister;
VAR propertyNameList = persister.PropertyNames;
的foreach(在propertyNameList VAR propertyName的)
{
//很多信息是收藏,所以我们需要知道索引
//我们的财产$ B的$ b VAR指数= persister.GetPropertyIndex(propertyName的);
//索引,我们可以用映射类型
变种类型= persister.PropertyTypes [指数]工作;
//如果类型是一对一...我们可以观察到
如果(类型为NHibernate.Type.OneToOneType)
{
Console.Write(类型。 IsAssociationType);
Console.Write(type.IsAnyType);
Console.Write(type.IsMutable);
Console.Write(type.IsCollectionType);
Console.Write(type.IsComponentType);
Console.Write(type.ReturnedClass);
...
//许多其他有趣的和有用的信息可以透露
}
}
A code generator needs to extract and generate some metadata from nhibernate mappings, I am wondering how nhibernate store relations.
For many to one relation how the other side stored (one to one part)
Domain model:
public class Person
{
public Detail {get;set;}
}
public class Detail
{
public Person {get;set;}
}
Traversing map:
PersistentClass map = _config.GetClassMapping(typeof(Person));
Property prop = map.GetProperty("Detail");
// how to find Associated Property (I.E. 'Detail.Person')
Configuration, and later ISessionFactory
build from it, are keeping information about persisters. They could represent entities or collections.
Here we can see what we could get about any persisted class:
The class persister:
var entityType = typeof(TEntity);
var factory = ... // Get your ISessionFactory
var persister = factory.GetClassMetadata(entityType) as AbstractEntityPersister;
And we also can observe collection persisters
the collection persister:
// the Abstract collection persister
var collectionPersister = factory
.GetCollectionMetadata(roleName) as AbstractCollectionPersister;
// here we go:
var isManyToMany = collectionPersister.IsManyToMany;
var isOneToMany = collectionPersister.IsOneToMany;
So, in general, what is mapped, it is represented as persister. Hope it helps
EXTEND, find out one-to-one
Based on the stuff above, the explicit search for one-to-one
could be like:
var persister = factory.GetClassMetadata(entityType) as AbstractEntityPersister;
var propertyNameList = persister.PropertyNames;
foreach (var propertyName in propertyNameList)
{
// many info is in collections, so we need to know the index
// of our property
var index = persister.GetPropertyIndex(propertyName);
// with index, we can work with the mapped type
var type = persister.PropertyTypes[index];
// if the type is OneToOne... we can observe
if(type is NHibernate.Type.OneToOneType)
{
Console.Write(type.IsAssociationType);
Console.Write(type.IsAnyType);
Console.Write(type.IsMutable);
Console.Write(type.IsCollectionType);
Console.Write(type.IsComponentType);
Console.Write(type.ReturnedClass);
...
// many other interesting and useful info could be revealed
}
}
这篇关于探索NHibernate的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!