1

假设您有一组适用于特定映射组的 Fluent 约定,但并非适用于所有映射组。

我的想法是,我将创建可以应用于 Fluent *Map 类的自定义 C# 属性 - 并编写通过检查 *Map 类来确定是否应用自定义属性的约定。

这样,我可以选择一组约定并将它们应用于各种映射,只需使用自定义属性标记它们 - [UseShortNamingConvention] 等。

我是 NHibernate 的新手(以及 Fluent 和 C#)——这种方法可行吗?

它是理智的吗?:-)

谢谢!

4

1 回答 1

2

是的!我实际上做了类似的事情,但使用了标记接口(INotCacheable、IComponent),但标记接口或属性应该没有太大区别。

应用您的约定时,只需检查您的属性是否存在并且您的好:)

编辑:

添加一些代码示例

    public class MyMappingConventions : IReferenceConvention, IClassConvention
    {
        public void Apply(IOneToManyCollectionInstance instance)
        {
            instance.Key.Column(instance.EntityType.Name + "ID");
            instance.LazyLoad();
            instance.Inverse();
            instance.Cascade.SaveUpdate();

            if ((typeof(INotCacheable).IsAssignableFrom(instance.Relationship.Class.GetUnderlyingSystemType())))
                return;

            instance.Cache.ReadWrite();
        }

        public void Apply(IClassInstance instance)
        {
            instance.Table(instance.EntityType.Name + "s");
            //If inheriting from IIMutable make it readonly
            if ((typeof(IImmutable).IsAssignableFrom(instance.EntityType)))
                instance.ReadOnly();

            //If not cacheable
            if ((typeof(INotCacheable).IsAssignableFrom(instance.EntityType)))
                return;

            instance.Cache.ReadWrite();
        }
}
于 2010-07-13T21:02:48.527 回答