How to ignore property mapping using agreement matching

Is there a way to avoid mapping a property to NHibernate 3.2 by matching code conventions? By default, all properties are displayed.

+4
source share
3 answers

As far as I know, two options:

1) Extend the terms of the ConventionModelMapper and SimpleModelInspector to extend IsPersistentProperty to suit your needs.

2) Use IsPersistentProperty as follows:

... mapper.IsPersistentProperty((memberInfo, declared) => IsPersistentProperty(mapper.ModelInspector, memberInfo, declared, "YourPropertyName")); ... public static bool IsPersistentProperty(IModelInspector modelInspector, MemberInfo member, bool declared, string propertyName) { return (declared ||(member is PropertyInfo) && !IsReadOnlyProperty(member)) && !member.Name.Equals(propertyName); } private static bool IsReadOnlyProperty(MemberInfo subject) { const BindingFlags defaultBinding = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly; var property = subject as PropertyInfo; if (property == null) { return false; } if (CanReadCantWriteInsideType(property) || CanReadCantWriteInBaseType(property)) { return !PropertyToField.DefaultStrategies.Values.Any(s => subject.DeclaringType.GetField(s.GetFieldName(property.Name), defaultBinding) != null) || IsAutoproperty(property); } return false; } private static bool IsAutoproperty(PropertyInfo property) { return property.ReflectedType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).Any(pi => pi.Name == string.Concat("<", property.Name, ">k__BackingField")); } private static bool CanReadCantWriteInsideType(PropertyInfo property) { return !property.CanWrite && property.CanRead && property.DeclaringType == property.ReflectedType; } private static bool CanReadCantWriteInBaseType(PropertyInfo property) { if (property.DeclaringType == property.ReflectedType) { return false; } var rfprop = property.DeclaringType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).SingleOrDefault(pi => pi.Name == property.Name); return rfprop != null && !rfprop.CanWrite && rfprop.CanRead; } 
+2
source

2). As an alternative to copying and pasting the default implementation of IsPersistentProperty, you can reuse it with reflection:

 var mapper = new ConventionModelMapper(); var field = mapper.ModelInspector.GetType() .GetField( "isPersistentProperty", BindingFlags.NonPublic | BindingFlags.Instance ); var ispp = (Func<MemberInfo, bool, bool>)field.GetValue( mapper.ModelInspector ); mapper.IsPersistentProperty( ( mi, b ) => ispp( mi, b ) && ( /*any conditions here*/ mi.Name != "SomeFiledName" ) ); 

Conditions can be moved to a separate method or class. A surface shell based on expressions can be performed on it.

+2
source

Duplicate: Ignore column using code matching in HNibernate

you can use the following:

 mapper.IsPersistentProperty((mi, declared) => { if (mi.DeclaringType == typeof (YourType) && mi.Name == "PropertyNameToIgnore") return false; return true; }); 
+1
source

All Articles