代码之家  ›  专栏  ›  技术社区  ›  mindplay.dk

使用自定义C#属性选择Fluent约定

  •  1
  • mindplay.dk  · 技术社区  · 14 年前

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

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

    我对NHibernate还不熟悉(而且很流利,这方面我也很熟悉)——这种方法可行吗?

    谢谢!

    1 回复  |  直到 14 年前
        1
  •  2
  •   Kenny Eliasson    14 年前

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

    在应用约定时,只需检查是否存在您的属性和ur good:)

    编辑:

    添加一些代码示例

        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();
            }
    }