NHibernate Source Analysis: Object Mapping

zhaozj2021-02-16  74

1. Persistent object mapping files About persistent object mapping files, here don't say more, refer to NHibernate examples and documents. There is a NHibernate-maping-2.0.xsd document in the root of NHibernate source code, which is NHibernate to verify the mapping file, and we can also verify the validity of the mapping file by using this document with this document with this document. 2. Read the mapping information With the Configuration class, you can read mapping information in a variety of ways. Some methods starting with the ADD are used to join mapping information, which will eventually call Add (XMLDocument DOC). // ** configuration.cs ** private hashtable class = new hashtable (); Classes collection For all persistent object mapping information, its key is the type of persistent class; Value is subclass of the PermissionClass class. private void Add (XmlDocument doc) {try {Binder.dialect = Dialect.Dialect.GetDialect (properties); Binder.BindRoot (doc, CreateMappings ());} catch (MappingException me) {log.Error ( "Could not compile the Mapping document ", me); throw me;} // end / catch} adddocument method calls Binder's static method BINDROOT to bind a persistent class mapping information. Createmappings returns a mappings object that is a class that simply encapsulates all mapping information collection. 3. Establish an object mapping information Binder class Bindroot is used to bind all mappings in the map information. // ** binder.cs ** public static void bindroot (xmlDocument doc, mappings model {// ... foreach (xmlnode n in hmnode.selectnodes) {rootclass rootclass = new Rootclass (); binder.bindrootclass (n, rootclass, model); model.addclass (rootclass);} // ...} Traverse all class map nodes, then call BINDROTCLASS to bind class mapping information, and finalize the class map Information is added to the collection. Where rootclass is the subclass of Permissionclass.

public static void BindRootClass (XmlNode node, RootClass model, Mappings mappings) {BindClass (node, model, mappings); // TABLENAME XmlAttribute tableNameNode = node.Attributes [ "table"]; string tableName = (tableNameNode == null) StringHelper? .Unqualify (model.PersistentClazz.Name): tableNameNode.Value; XmlAttribute schemaNode = node.Attributes [ "schema"]; string schema = schemaNode == null mappings.SchemaName:? schemaNode.Value; Table table = mappings.AddTable (schema , TABLENAME); Model.Table = Table; // ... PropertiesFromXML (Node, MAPPINGS);} Bindrootclass first calls bindclass binding persistent class mapping information, then call PropertiesFromL to bind class properties. public static void BindClass (XmlNode node, PersistentClass model, Mappings mapping) {string className = node.Attributes [ "name"] == null null:?. node.Attributes [ "name"] Value; // class try {model. PersistentClazz = ReflectHelper.classforname (classname);} catch new mappingexception ("Persistent Class Not Found", CNFE);} // ...} BindClass acquires the type of persistent object by reflection.

protected static void PropertiesFromXML (XmlNode node, PersistentClass model, Mappings mappings) {string path = model.Name; Table table = model.Table; foreach (XmlNode subnode in node.ChildNodes) {CollectionType collectType = CollectionType.CollectionTypeFromString (name); Value Value = null; if (collecttype! = null) {value = new value (table); bindvalue (subnode, value, true);} else if ("Many-to-one" .Equals (name) {value = new Manytoone (Table); BindManyToone (Subnode, (Manytoone) Value, PropertyName, True;} else if ("any" .Equals (name)) {value = new any (table); bindany (any) Value, True);} else if ("one-to-one" .Equals (name)) {value = new onetoone (table, model.identifier); bindonetoone (Subnode, (OnToone) value, true);} else Property ".equals (name)) {value = new value (table); bindvalue (Subnode, Value, True, PropertyName } Else if ("Component" .Equals (name)) {value = new component (model); bindcomponent (Subnode, (Component) Value, REFLECTEDCLASS, Subpath, true, mappings;} else if ("Subclass". equals (name)) {subclass subclass = new subclass (model); BindSubclass (subnode, subclass, mappings);} else if ( "joined-subclass" .Equals (name)) {subclass subclass = new subclass (model); BindJoinedSubclass (Subnode, Subclass, Mappings);} if (value! = null) {property prop = new profment (value); bindproperty (Subnode, Prop, Mappings);

}}} Traverse all child nodes and then bind them according to the node type. (Note: Some content has been deleted) After the property is mapped, there is no detailed study, just know that the property has been added to the rootclass property.

转载请注明原文地址:https://www.9cbs.com/read-13118.html

New Post(0)