<?xml version="1.0"?>

<document>

 <properties>
   <title>Data Mapping Abstraction Component</title>
 </properties>

<body>

<section name="Mapper: Data Mapping Abstraction Component">

<p>
Most Java applications must store data to a data store whether it's XML files or
a relational database.  Changes to the mapping technology should be transparent
to the rest of the application allowing changes to be localized in the mapping
layer.
</p>

<p>
Commons Mapper is a thin abstraction layer around a project's chosen data mapping
technology.  It allows the developer to vary the mapping technique behind
this layer (often combining several technologies) so that the rest of the 
application doesn't change.  Technologies such as these can be used to map 
Java objects to a data store:
</p>

<ul>
<li>
    <a href="http://java.sun.com/products/jdbc/">JDBC</a> -Using 
    <a href="http://commons.apache.org/dbutils/">Apache Commons DbUtils</a> 
    will make JDBC coding significantly easier.
</li>
<li><a href="http://java.sun.com/products/jdo/">JDO</a></li>
<li><a href="http://java.sun.com/products/ejb/">EJB</a></li>
<li><a href="http://www.hibernate.org/">Hibernate</a></li>
<li><a href="http://www.ibatis.com/">iBatis</a></li>
<li>XML</li>
</ul>

<p>
The purpose of Mapper is similar to Commons Logging.  It isn't a mapping technology
itself, it merely exposes common functionality of other mapping APIs.
</p> 

<p>
Commons Mapper is based on the Data Mapper pattern presented in "Patterns of 
Enterprise Application Architecture" by Martin Fowler.
</p>

</section>


<section name="Scope of the Package">

<p>
Commons Mapper is <strong>not</strong> a data mapping implementation.  It is meant
to be an API allowing pluggable mapper objects of varying implementations.
</p>

</section>

<section name="Interaction With Other Packages">
    <p>Commons Mapper relies on the standard Java 1.2 (or later) APIs.</p>
</section>

<section name="Example Usage">
<p>
Commons Mapper is based on the idea of groups of mappers and a 
<code><a href="apidocs/org/apache/commons/mapper/MapperFactory.html">MapperFactory</a></code>.  
Mappers are responsible for persisting one kind
of Object to a data store. MapperFactory is responsible for dynamically loading
and initializing Mappers for the client code.  Typically, a .properties file is
used to map domain class names to mapper implementation class names and passed to
a new MapperFactory instance.
</p>

<p>
All mappers must implement the <code><a href="apidocs/org/apache/commons/mapper/Mapper.html">Mapper</a></code> 
interface.  The majority of mapping work can be accomplished through the methods in this
interface with specialized methods added to specific Mappers as needed.
</p>
<source>
    // 1. Setup MapperFactory (this is best done with a .properties file).
    // The keys in the file are fully qualified class names of objects to
    // persist.  The values are the fully qualified class names of the Mapper
    // objects.  
    
    // Note: The keys can actually be anything you want; however, by using
    // fully qualified class names, you can use the Object.class shortcut 
    // notation to lookup Mappers with MapperFactory.getMapper() as shown in
    // step 3.

    Map mappers = new HashMap();
    mappers.add("com.yourcorp.Person", "com.yourcorp.PersonMapper");
    MapperFactory factory = new MapperFactory(mappers);

	// 2. Add a factory listener that will initialize Mapper instances as 
	// they are created.  This allows you to initialize any number of Mapper
	// types without subclassing MapperFactory and using an ugly if/else
	// statement.

	MapperFactoryListener listener =
		new JdbcMapperFactoryListener(dataSource);
    factory.addMapperFactoryListener(listener);
    
    // 3. Persist object with a dynamically loaded Mapper implementation.
    // The factory will return a com.yourcorp.PersonMapper object
    // (which was defined in the Map) to do the persistence.

    Person p = new Person("Joe", "User");
    Mapper m = factory.getMapper(Person.class);
    m.create(p);
</source>

</section>

</body>
</document>

