【问题标题】:Using custom C# attributes to select Fluent conventions使用自定义 C# 属性选择 Fluent 约定
【发布时间】:2010-07-13 20:54:07
【问题描述】:

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

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

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

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

它是理智的吗? :-)

谢谢!

【问题讨论】:

    标签: nhibernate fluent-nhibernate fluent custom-attributes


    【解决方案1】:

    是的!我实际上做了类似的事情,但改用了标记接口(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();
            }
    }
    

    【讨论】:

    • 你知道吗? ... instance.EntityType 会给我正在映射的实体类型 - 是否有一个属性可以给我 EntityMapType?
    猜你喜欢
    • 2019-11-02
    • 2012-10-27
    • 2014-12-27
    • 2017-04-21
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多