From: Paul H. <pha...@us...> - 2005-03-21 11:31:47
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1083/nhibernate/src/NHibernate/Cfg Modified Files: AssemblyHbmOrderer.cs Binder.cs Mappings.cs Log Message: NH-185 - Default naming strategy for Classes NH-180 - Add default Namespace/Assembly to hibernate-mapping Index: Mappings.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Mappings.cs,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** Mappings.cs 1 Mar 2005 16:24:44 -0000 1.12 --- Mappings.cs 21 Mar 2005 11:31:35 -0000 1.13 *************** *** 26,29 **** --- 26,31 ---- private IList propertyReferences; private string defaultAccess; + private string @namespace; + private string assembly; private IDictionary caches; private INamingStrategy namingStrategy; *************** *** 139,142 **** --- 141,162 ---- /// <summary> + /// The default namespace for persistent classes + /// </summary> + public string Namespace + { + get { return @namespace; } + set { @namespace = value; } + } + + /// <summary> + /// The default assembly for persistent classes + /// </summary> + public string Assembly + { + get { return assembly; } + set { assembly = value; } + } + + /// <summary> /// /// </summary> Index: AssemblyHbmOrderer.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/AssemblyHbmOrderer.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AssemblyHbmOrderer.cs 21 Feb 2005 19:16:52 -0000 1.1 --- AssemblyHbmOrderer.cs 21 Mar 2005 11:31:35 -0000 1.2 *************** *** 5,8 **** --- 5,9 ---- using System.Reflection; using System.Xml; + using NHibernate.Util; namespace NHibernate.Cfg *************** *** 84,88 **** while( xmlReader.Read() ) { ! if( xmlReader.NodeType!=XmlNodeType.Element ) { continue; --- 85,89 ---- while( xmlReader.Read() ) { ! if( xmlReader.NodeType != XmlNodeType.Element ) { continue; *************** *** 92,96 **** { xmlReader.MoveToAttribute("name"); ! string className = xmlReader.Value; ClassEntry ce = new ClassEntry( null, className, fileName ); _classes.Add(ce); --- 93,97 ---- { xmlReader.MoveToAttribute("name"); ! string className = StringHelper.GetClassname( xmlReader.Value ); ClassEntry ce = new ClassEntry( null, className, fileName ); _classes.Add(ce); *************** *** 99,107 **** { xmlReader.MoveToAttribute("name"); ! string className = xmlReader.Value; if( xmlReader.MoveToAttribute("extends") ) { containsExtends = true; ! string baseClassName = xmlReader.Value; ClassEntry ce = new ClassEntry( baseClassName, className, fileName ); _classes.Add(ce); --- 100,108 ---- { xmlReader.MoveToAttribute("name"); ! string className = StringHelper.GetClassname( xmlReader.Value ); if( xmlReader.MoveToAttribute("extends") ) { containsExtends = true; ! string baseClassName = StringHelper.GetClassname( xmlReader.Value ); ClassEntry ce = new ClassEntry( baseClassName, className, fileName ); _classes.Add(ce); *************** *** 169,173 **** // base class was found - insert at the index // immediately following it ! if( sce.ClassName==ce.BaseClassName ) { insertIndex = i + 1; --- 170,174 ---- // base class was found - insert at the index // immediately following it ! if( sce.ClassName == ce.BaseClassName ) { insertIndex = i + 1; Index: Binder.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Binder.cs,v retrieving revision 1.41 retrieving revision 1.42 diff -C2 -d -r1.41 -r1.42 *** Binder.cs 14 Mar 2005 14:35:54 -0000 1.41 --- Binder.cs 21 Mar 2005 11:31:35 -0000 1.42 *************** *** 22,28 **** internal static Dialect.Dialect dialect; public static void BindClass(XmlNode node, PersistentClass model, Mappings mapping) { ! string className = node.Attributes["name"] == null ? null : node.Attributes["name"].Value; //CLASS --- 22,75 ---- internal static Dialect.Dialect dialect; + /// <summary> + /// Converts a partial class name into a fully qualified one + /// </summary> + /// <param name="className"></param> + /// <param name="mapping"></param> + /// <returns></returns> + public static string FullClassName( string className, Mappings mapping ) + { + if ( className == null ) + { + return null; + } + + // TODO: Make this stronger, assumes stuff about presence of culture, public key etc + // Split off the assembly reference if it's present + string[] parts = className.Split( ',' ); + string name = parts[ 0 ]; + if ( name.IndexOf( '.' ) == -1 && mapping.Namespace != null ) + { + // No namespace - use the default + name = mapping.Namespace + "." + name; + } + + string assm; + if ( parts.Length == 1 ) + { + if ( mapping.Assembly == null ) + { + // No default, let it try and find it automatically + assm = ""; + } + else + { + // No assembly - use the default + assm = ", " + mapping.Assembly; + } + } + else + { + // Use the assembly reference provided + assm = ", " + parts[ 1 ]; + } + + // Rebuild the name + return name + assm; + } + public static void BindClass(XmlNode node, PersistentClass model, Mappings mapping) { ! string className = node.Attributes["name"] == null ? null : FullClassName( node.Attributes["name"].Value, mapping ); //CLASS *************** *** 70,74 **** //IMPORT ! // we automattically want to add an import of the Assembly Qualified Name (includes version, // culture, public-key) to the className supplied in the hbm.xml file. The most common use-case // will have it contain the "FullClassname, AssemblyName", it might contain version, culture, --- 117,121 ---- //IMPORT ! // we automatically want to add an import of the Assembly Qualified Name (includes version, // culture, public-key) to the className supplied in the hbm.xml file. The most common use-case // will have it contain the "FullClassname, AssemblyName", it might contain version, culture, *************** *** 531,535 **** try { ! model.CollectionPersisterClass = ReflectHelper.ClassForName( persisterNode.Value ); } catch (Exception) --- 578,583 ---- try { ! string persisterName = FullClassName( persisterNode.Value, mappings ); ! model.CollectionPersisterClass = ReflectHelper.ClassForName( persisterName ); } catch (Exception) *************** *** 554,558 **** XmlAttribute tableNode = node.Attributes["table"]; string tableName; ! if ( tableNode!=null) { tableName = mappings.NamingStrategy.TableName( tableNode.Value ); --- 602,606 ---- XmlAttribute tableNode = node.Attributes["table"]; string tableName; ! if ( tableNode != null ) { tableName = mappings.NamingStrategy.TableName( tableNode.Value ); *************** *** 586,599 **** { model.IsSorted = true; ! string comparatorClassName = sortedAtt.Value; if ( !comparatorClassName.Equals("natural") ) { try { ! model.Comparer = (IComparer) Activator.CreateInstance( ReflectHelper.ClassForName(comparatorClassName) ); } catch (Exception) { ! throw new MappingException("could not instantiate comparer class: " + className); } } --- 634,647 ---- { model.IsSorted = true; ! string comparatorClassName = FullClassName( sortedAtt.Value, mappings ); if ( !comparatorClassName.Equals("natural") ) { try { ! model.Comparer = (IComparer) Activator.CreateInstance( ReflectHelper.ClassForName( comparatorClassName ) ); } catch (Exception) { ! throw new MappingException( "could not instantiate comparer class: " + comparatorClassName ); } } *************** *** 827,831 **** try { ! model.ComponentClass = ReflectHelper.ClassForName( classNode.Value ) ; } catch (Exception e) --- 875,879 ---- try { ! model.ComponentClass = ReflectHelper.ClassForName( FullClassName( classNode.Value, mappings ) ) ; } catch (Exception e) *************** *** 1312,1315 **** --- 1360,1367 ---- XmlAttribute aiNode = hmNode.Attributes["auto-import"]; model.IsAutoImport = (aiNode==null) ? true : "true".Equals( aiNode.Value ); + XmlAttribute nsNode = hmNode.Attributes["namespace"]; + model.Namespace = (nsNode==null) ? null : nsNode.Value ; + XmlAttribute assemblyNode = hmNode.Attributes["assembly"]; + model.Assembly = (assemblyNode==null) ? null : assemblyNode.Value ; nsmgr = new XmlNamespaceManager(doc.NameTable); *************** *** 1378,1385 **** foreach(XmlNode n in hmNode.SelectNodes(nsPrefix + ":import", nsmgr) ) { ! string className = n.Attributes["class"].Value; ! XmlAttribute renameNode = n.Attributes["rename"]; ! string rename = (renameNode==null) ? StringHelper.GetClassname(className) : renameNode.Value; ! log.Debug("Import: " + rename + " -> " + className); model.AddImport(className, rename); } --- 1430,1437 ---- foreach(XmlNode n in hmNode.SelectNodes(nsPrefix + ":import", nsmgr) ) { ! string className = FullClassName( n.Attributes["class"].Value, model ); ! XmlAttribute renameNode = n.Attributes[ "rename" ]; ! string rename = (renameNode == null) ? StringHelper.GetClassname( className ) : renameNode.Value; ! log.Debug( "Import: " + rename + " -> " + className ); model.AddImport(className, rename); } *************** *** 1414,1423 **** private static PersistentClass GetSuperclass( Mappings model, XmlNode subnode ) { ! XmlAttribute extendsAttr = subnode.Attributes["extends"]; ! if( extendsAttr==null ) { throw new MappingException( "'extends' attribute is not found." ); } ! String extendsValue = extendsAttr.Value; System.Type superclass; try --- 1466,1475 ---- private static PersistentClass GetSuperclass( Mappings model, XmlNode subnode ) { ! XmlAttribute extendsAttr = subnode.Attributes[ "extends" ]; ! if( extendsAttr == null ) { throw new MappingException( "'extends' attribute is not found." ); } ! String extendsValue = FullClassName( extendsAttr.Value, model ); System.Type superclass; try |