NHibernate3 anatomy: Mapping's Conform Combat (2): Principle

Source: Internet
Author: User

Series Introduction

NHibernate3 Analysis Series from the configuration, mapping, session, core, tool, practice articles, Extension the contents, features and applications of the NHibernate3 version are fully based on the NHibernte3 version.

    • NHibernate Special topic: http://kb.cnblogs.com/zt/nhibernate/
    • NHibernate Official site: http://nhforge.org/
    • NHibernate Reference Document: http://nhforge.org/doc/nh/en/
    • Get NHibernate Address: http://sourceforge.net/projects/nhibernate/files/
Conform overview

In the previous section, I used a simple example to describe the simple use of conform. Left a lot of questions, we puzzled why use conform and how to use conform, its internal principle is what. In this section, we first focus on understanding some of the principles of conform.

You can get conform from http://code.google.com/p/codeconform/.

Conform important interface

The core of conform is to instantiate a Objectrelationalmapper object and mapper object, configure the domain object, and invoke the Compilemappingfor () method of the Mapper object to generate hbmmapping. That's what we wrote in the previous section:

//code Snippets Copyright http://lyj.cnblogs.com/Public StatichbmmappingGetMapping () {//Initialize the Objectrelationalmapper class    varORM =NewObjectrelationalmapper();//configure domain as TableperclassOrm. tableperclass<Domain> ();//Here you can call the Objectrelationalmapper class some methods to configure domain semantics    //Using ORM parameters to initialize the Mapper class    varMapper =NewMapper(ORM);//You can call the Mapper class here to configure mapping for domain    //Call the Compilemappingfor method of the Mapper class to compile the generated Hbmmapping object    returnMapper.compilemappingfor (New[] {typeof(Domain) });}

Before you get to the code, take a look at the important interfaces of conform:

Idomaininspector interface

The Idomaininspector interface is used to describe our domain model, as defined by ORM terminology. Conform is the starting point of the mapping class, but also the driver. This interface is implemented by the Objectrelationalmapper class.

Iobjectrelationalmapper interface

First look at some of the ORM terminology:

Three kinds of object-oriented inheritance policies:

    • Tableperclass: One table mapping strategy per class
    • Tableperclasshierarchy: A table mapping strategy for each class hierarchy
    • Tableperconcreteclass: A table mapping strategy for each specific class

In the entity model properties, there are primary key attributes, persisted properties, and non-persisted properties.

The Iobjectrelationalmapper interface uses ORM terminology to describe the domain model, which includes three inheritance policies and various property-setting methods of the entity model:

    • Primary KEY (Poid): Each entity has its own state and life cycle, and records in the database require a primary key to be recognized.
    • Persistent properties: There are generally basic properties, as well as version number attributes (Versionproperty), Naturalid properties, various collection properties (set, Bag, List, Array, Dictionary, Complex, heterogeneousassociation), various association properties (Manytomany, Manytoone, Onetoone).
    • Non-persistent properties: Not all domain, attributes need to be persisted, we can use exclude, excludeproperty exclusions when we do not need to map classes or attributes.

Another cascading method for attaching an associated set: Cascade

Iexplicitdeclarationsholder interface

Explicitly declares the domain semantic interface, using the Iobjectrelationalmapper interface method to explicitly declare custom domain semantics, and conform is added to the appropriate collection.

Ipatternsholder interface

For implicitly declaring the domain semantic interface, the main principle is that the default implementation class (Defaultnhibernatepatternsholder Class) defines the matching pattern by default in each collection for us to match its members.

For example, the entity primary key poid uses Poidpattern by default: If the name of an entity member is "ID" or "poid" or "OID" (case insensitive), it is considered the primary key of the entity.

Objectrelationalmapper class

The Objectrelationalmapper class implements the Idomaininspector interface and the Iobjectrelationalmapper interface.

Iobjectrelationalmapper interface Implementation method: Used to configure custom domain semantics to the appropriate collection of Iexplicitdeclarationsholder interfaces.

Idomaininspector interface Implementation method: Used to validate domain semantics, it validates domain semantics in two ways:

    • One is an explicit match in the related collection in the Iexplicitdeclarationsholder interface default implementation (Explicitdeclarationsholder Class).
    • The second is an implicit match in the related collection in the default implementation of the Ipatternsholder interface (Defaultnhibernatepatternsholder Class).
Mapping class

The mapping class is a bridge between Objectrelationalmapper and nhibernate mappings. Under the Conform.nh namespace, the mapping class parses the domain model semantics through the Idomaininspector interface, and we pass the Compilemappingfor () method or Compilemappingforeach () The NHibernate method compiles and transforms the domain model in the program into the Hbmmapping object used by the application according to these semantics.

Conform important mode

In addition to the compilemappingfor (), Compilemappingforeach () methods, the mapping class introduces us to three important modes: Mode adapter (pattern-applier) mode, general customization ( Generic-customizer) mode, specific customization (Specific-customizer) mode.

Mode Adapter (Pattern-applier)

The mode adapter (Pattern-applier), as the name implies, is that the domain model matches the pattern, and if this pattern is matched, the corresponding operation is performed.

Conform provides many pattern-applier (that is, the Defaultpatternsappliersholder Class) by default. We can view it through the Patternsappliers property of the mapping class. You can also increase the mode adapter (Pattern-applier) by using the Addxxxpattern () method.

Universal Customization (Generic-customizer)

Universal customization (Generic-customizer) means customizing the generic type.

We don't need to know exactly what this type of real mapping is. If you want to customize a class, we don't need to know if the class is ultimately mapped to class,subclass,joined-subclass,union-subclass or component, just set some properties on this class. Similarly, you can customize a collection without knowing whether the collection will be mapped to bag,set,array,list or map (dictionary).

Generic customization is provided by the Customize () method of the mapping class.

Specific customization (Specific-customizer)

Specific customization (Specific-customizer) is the customization of a particular class or collection.

In contrast to generic customizations, we set some custom properties on a particular class or a particular collection, which is only useful for the object you are currently customizing.

Specific customizations are provided by the class (), subclass (), Joinedsubclass (), Unionsubclass (), and Component () methods of the mapping classes.

Conclusion

This article understands some of the principles of conform, and later articles are based on this article to show conform various applications.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

NHibernate3 anatomy: Mapping's Conform Combat (2): Principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.