Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CollectionBinder - extract method NeedBackref

This method returns true for of one-to-many non-inverse collecitions with not-nullable keys
  • Loading branch information...
commit 5278180efdd100f3af961ac11299eb4df0027bdb 1 parent 82ad2c9
@hazzik hazzik authored
Showing with 28 additions and 24 deletions.
  1. +28 −24 src/NHibernate/Cfg/XmlHbmBinding/CollectionBinder.cs
View
52 src/NHibernate/Cfg/XmlHbmBinding/CollectionBinder.cs
@@ -157,7 +157,7 @@ private void BindPrimitiveArray(HbmPrimitiveArray arrayMapping, PrimitiveArray m
if (!string.IsNullOrEmpty(collectionMapping.PersisterQualifiedName))
{
model.CollectionPersisterClass = ClassForNameChecked(collectionMapping.PersisterQualifiedName, mappings,
- "could not instantiate collection persister class: {0}");
+ "could not instantiate collection persister class: {0}");
}
if(!string.IsNullOrEmpty(collectionMapping.CollectionType))
@@ -192,8 +192,8 @@ private void BindPrimitiveArray(HbmPrimitiveArray arrayMapping, PrimitiveArray m
{
//TABLE
string tableName = !string.IsNullOrEmpty(collectionMapping.Table)
- ? mappings.NamingStrategy.TableName(collectionMapping.Table)
- : mappings.NamingStrategy.PropertyToTableName(className, path);
+ ? mappings.NamingStrategy.TableName(collectionMapping.Table)
+ : mappings.NamingStrategy.PropertyToTableName(className, path);
string schema = string.IsNullOrEmpty(collectionMapping.Schema) ? mappings.SchemaName : collectionMapping.Schema;
string catalog = string.IsNullOrEmpty(collectionMapping.Catalog) ? mappings.CatalogName : collectionMapping.Catalog;
@@ -233,7 +233,7 @@ private void BindPrimitiveArray(HbmPrimitiveArray arrayMapping, PrimitiveArray m
isGeneric = collectionType.IsGenericType;
}
- model.IsGeneric = isGeneric ?? false;
+ model.IsGeneric = isGeneric.GetValueOrDefault();
if (model.IsGeneric)
{
@@ -506,8 +506,7 @@ private static void PostCollectionSecondPass(Mapping.Collection collection)
}
}
- private void BindListSecondPass(HbmList listMapping, List model,
- IDictionary<string, PersistentClass> persistentClasses, IDictionary<string, MetaAttribute> inheritedMetas)
+ private void BindListSecondPass(HbmList listMapping, List model, IDictionary<string, PersistentClass> persistentClasses, IDictionary<string, MetaAttribute> inheritedMetas)
{
BindCollectionSecondPass(listMapping, model, persistentClasses, inheritedMetas);
@@ -515,10 +514,10 @@ private static void PostCollectionSecondPass(Mapping.Collection collection)
BindCollectionIndex(listMapping, model);
if (listMapping.ListIndex != null && !string.IsNullOrEmpty(listMapping.ListIndex.@base))
{
- model.BaseIndex = Convert.ToInt32(listMapping.ListIndex.@base);
+ model.BaseIndex = Convert.ToInt32(listMapping.ListIndex.@base);
}
- if (model.IsOneToMany && !model.Key.IsNullable && !model.IsInverse)
+ if (NeedBackref(model))
{
string entityName = ((OneToMany)model.Element).ReferencedEntityName;
PersistentClass referenced = mappings.GetClass(entityName);
@@ -566,14 +565,14 @@ private void BindCollectionIndex(IIndexedCollectionMapping listMapping, IndexedC
{
iv = new SimpleValue(model.CollectionTable);
new ValuePropertyBinder(iv, Mappings).BindSimpleValue(listMapping.ListIndex,
- IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
+ IndexedCollection.DefaultIndexColumnName, model.IsOneToMany);
}
else if (listMapping.Index != null)
{
iv = new SimpleValue(model.CollectionTable);
listMapping.Index.type = NHibernateUtil.Int32.Name;
new ValuePropertyBinder(iv, Mappings).BindSimpleValue(listMapping.Index, IndexedCollection.DefaultIndexColumnName,
- model.IsOneToMany);
+ model.IsOneToMany);
}
if (iv != null)
{
@@ -634,7 +633,7 @@ private void BindOneToMany(HbmOneToMany oneToManyMapping, OneToMany model)
{
var value = new SimpleValue(model.CollectionTable);
new ValuePropertyBinder(value, Mappings).BindSimpleValue(indexMapping, IndexedCollection.DefaultIndexColumnName,
- model.IsOneToMany);
+ model.IsOneToMany);
model.Index = value;
if (string.IsNullOrEmpty(model.Index.TypeName))
throw new MappingException("map index element must specify a type: " + model.Role);
@@ -680,7 +679,7 @@ private void BindOneToMany(HbmOneToMany oneToManyMapping, OneToMany model)
}
bool indexIsFormula = model.Index.ColumnIterator.Any(x=> x.IsFormula);
- if (model.IsOneToMany && !model.Key.IsNullable && !model.IsInverse && !indexIsFormula)
+ if (NeedBackref(model) && !indexIsFormula)
{
string entityName = ((OneToMany)model.Element).ReferencedEntityName;
PersistentClass referenced = mappings.GetClass(entityName);
@@ -790,12 +789,12 @@ private void BindIndexManyToMany(HbmIndexManyToMany indexManyToManyMapping, Many
BindCache(collectionMapping.Cache, model);
- if (model.IsOneToMany && !model.IsInverse && !model.Key.IsNullable)
+ if (NeedBackref(model))
{
// for non-inverse one-to-many, with a not-null fk, add a backref!
string entityName = ((OneToMany)model.Element).ReferencedEntityName;
PersistentClass referenced = mappings.GetClass(entityName);
- Backref prop = new Backref();
+ var prop = new Backref();
prop.Name = '_' + model.OwnerEntityName + "." + collectionMapping.Name + "Backref";
prop.IsUpdateable = false;
prop.IsSelectable = false;
@@ -811,7 +810,7 @@ private void BindManyToMany(HbmManyToMany manyToManyMapping, Mapping.Collection
var manyToMany = new ManyToOne(model.CollectionTable);
model.Element = manyToMany;
new ValuePropertyBinder(manyToMany, Mappings).BindSimpleValue(manyToManyMapping,
- Mapping.Collection.DefaultElementColumnName, false);
+ Mapping.Collection.DefaultElementColumnName, false);
InitOuterJoinFetchSetting(manyToManyMapping, manyToMany);
var restrictedLaziness = manyToManyMapping.lazySpecified ? manyToManyMapping.lazy : (HbmRestrictedLaziness?) null;
InitLaziness(restrictedLaziness, manyToMany, true);
@@ -847,11 +846,11 @@ private void BindManyToAny(HbmManyToAny manyToAnyMapping, Mapping.Collection mod
new TypeBinder(any, Mappings).Bind(manyToAnyMapping.idtype);
BindAnyMeta(manyToAnyMapping, any);
new ColumnsBinder(any, Mappings).Bind(manyToAnyMapping.Columns, true,
- () =>
- new HbmColumn
- {
- name = mappings.NamingStrategy.PropertyToColumnName(manyToAnyMapping.column1)
- });
+ () =>
+ new HbmColumn
+ {
+ name = mappings.NamingStrategy.PropertyToColumnName(manyToAnyMapping.column1)
+ });
}
private void BindElement(HbmElement elementMapping, Mapping.Collection model)
@@ -901,7 +900,7 @@ private void BindKey(HbmKey keyMapping, Mapping.Collection model)
keyValue = (IKeyValue) model.Owner.GetProperty(propRef).Value;
}
var key = new DependantValue(model.CollectionTable, keyValue)
- {IsCascadeDeleteEnabled = keyMapping.ondelete == HbmOndelete.Cascade};
+ {IsCascadeDeleteEnabled = keyMapping.ondelete == HbmOndelete.Cascade};
new ValuePropertyBinder(key, Mappings).BindSimpleValue(keyMapping, Mapping.Collection.DefaultKeyColumnName, model.IsOneToMany);
@@ -909,8 +908,8 @@ private void BindKey(HbmKey keyMapping, Mapping.Collection model)
throw new MappingException("illegal use of an array as an identifier (arrays don't reimplement Equals)");
model.Key = key;
- key.SetNullable(keyMapping.IsNullable.HasValue ? keyMapping.IsNullable.Value : true);
- key.SetUpdateable(keyMapping.IsUpdatable.HasValue ? keyMapping.IsUpdatable.Value : true);
+ key.SetNullable(!keyMapping.IsNullable.HasValue || keyMapping.IsNullable.Value);
+ key.SetUpdateable(!keyMapping.IsUpdatable.HasValue || keyMapping.IsUpdatable.Value);
BindForeignKey(keyMapping.foreignkey, key);
}
@@ -929,7 +928,7 @@ private void BindManyToManySubelements(HbmManyToMany manyToManyMapping, Mapping.
var hasFilters = filters != null && filters.Length > 0;
if ((hasFilters || whereCondition != null) && collection.FetchMode == FetchMode.Join
- && collection.Element.FetchMode != FetchMode.Join)
+ && collection.Element.FetchMode != FetchMode.Join)
{
throw new MappingException(
string.Format(
@@ -947,5 +946,10 @@ private System.Type GetPropertyType(System.Type containingType, string propertyN
return containingType == null ? null : ReflectHelper.ReflectedPropertyClass(containingType, propertyName, access);
}
+
+ private static bool NeedBackref(Mapping.Collection model)
+ {
+ return model.IsOneToMany && !model.Key.IsNullable && !model.IsInverse;
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.