Configuring the Mapping Application Block

The Mapping Application Block uses the Enterprise Library Configuration Console to manage its configurable settings.

Adding the Mapping Application Block

Once you have opened your configuration file, or created a new one, in the Enterprise Library Configuration Console, the Mapping Application Block appears as one of the available block types in the context menu for the Application Configuration node under the New menu option:

Adding the Mapping Application Block

Having selected the Mapping Application Block the console should appear like this:

The Console with the Mapping Application Block added

Notice that the Mapping Application Block supports the partial trust permissions interface and configuration settings encryption.

Adding a Mapper

A mapper is the main object within the MAB and combines a domain object type with a set of maps to translate table columns into object properties. There are two different types of Mapper representing the two supported methods of mapping to objects. A common and mandatory property of all Mappers is the Name which acts as the mapper "instance" name used in your code to select the mapper to use.

Adding a Mapper

Adding a Basic Mapper

A Basic Mapper is the most common method of mapping to objects where a single storage entity relates to a single domain object. A DataSet containing the data in tables and rows will map each row to an instance of the same object type.

Adding a Basic Mapper
  • The DomainObjectTypeName mandatory property is the name of the domain object type that you will be mapping. The MAB uses reflection to create instances of this domain object type and maps data between the DataSet and the public properties of these objects. Although it is possible to type in this name it is rather long winded thus it is recommended that you use the editor instead; click on the ellipsis button and the TypeSelector editor appears:

Type Selector Editor for Domain Objects

This editor shows all currently loaded assemblies. If your assembly is not shown then you can load it by clicking on the Load from File... button.
  • The TableName is an optional property where you can enter the name of the table to map data between. If this is left blank it is assumed that you will be mapping between the domain object and the first table in the DataSet.
The following shows a Basic Mapper with all the properties added and represents the mapping of a single domain object to a single data storage entity such as a data table:

Basic Mapper with Properties

Adding a Super Mapper

A Super Mapper is used where a single storage entity relates to multiple domain objects where the storage entity dedicates a column to indicate the type of data held in the record. A typical example of this is a data table that holds records containing super types of an inherited base class. For example the base class might be called Animal and maps to a data table called Animal but in fact the records are instances of Cat, Dog and Horse super types with a "Type" column, say, holding a code to depict which super type is represented by that record. A DataSet containing the data from the Animal table will map each row to different instances of the set of super types depicted by the code held in the "Type" column.

Adding a Super Mapper
  • The DomainObjectSuperTypeColumn mandatory property is where you enter the name of the column from the data source that contains either a code that refers to the super type or the actual super type name itself.
  • The SuperTypes collection is optional and contains the list of codes found in the super type column mapped to the actual domain object super type names. If you leave this collection blank then it is assumed that the super type column contains the actual super type names rather than a code. When you click on the ellipsis button the SuperType Collection Editor appears:

SuperType Collection Editor

This editor allows you to list the set of all super type codes and match them with the type name of the actual domain object super type. The DataValue holds the code and the SuperTypeName holds the type name. If you click on the ellipsis button the TypeSelector editor appears as before with the Basic Mapper.
  • The TableName is an optional property where you can enter the name of the table to map data between. If this is left blank it is assumed that you will be mapping between the domain object and the first table in the DataSet.

The following shows a Super Mapper with all the properties added and represents a mapper for the Animal base class table which has a column called Type that holds the super type codes. These codes are listed in the SuperTypes collection alongside their respective super type names:

Super Mapper with Properties

Adding a Custom Mapper

A Custom Mapper deals with mapper types that are not currently supported. This is where you can extend the Mapping Application Block with your own mapper providers and configure them within your applications. If you selected a Custom Mapper you are asked for two properties. The Attributes collection and provider TypeName are well documented in the Enterprise Library help documentation but essentially the typename is the type name of your custom provider and the attributes collection is a set of parameters that get passed to your provider.

Adding a Custom Mapper

Copying a Mapper

Creating lengthy mappers can be quite tedious if the domain objects have not yet been written, especially if you are creating one very similar to one you already have. This is where the Copy function from the Mapper context menu helps allowing you to create an exact copy of a Mapper along with its set of maps:

Copying a Mapper

The Copied Mapper

Note how the name is given a sequence number suffix to keep it unique. You will probably want to change this straight away?

Adding Maps

Maps make the link between a column name and a domain object public property. The Data Transfer Object (DTO) uses the concept of tables, columns and rows to transport data around. Columns represent domain object properties while rows represent domain object instances. Each column is uniquely named and needs to be matched to a public property of the domain object class for the mapping to proceed. By selecting New from a Mapper context menu you will see that there is just one map type that you can choose from:

Adding Maps

Adding a Map

A map has just one optional parameter, the column name. If the column name differs from the domain object property name that it maps to then the column name must be entered here. Provided that column names and domain object property names match, then just the map name is all that is needed:

Adding a Map

Adding a Custom Map

A Custom Map deals with map types that are not currently supported. This is where you can extend the Mapping Application Block with your own map providers and configure them within your applications. If you selected a Custom Map you are asked for two properties. The Attributes collection and provider TypeName are well documented in the Enterprise Library help documentation but essentially the typename is the type name of your custom provider and the attributes collection is a set of parameters that get passed to your provider.

Adding a Custom Map

Saving and Validating the Mapping Application Block

When you save your configuration a number of validation checks are performed. All mandatory properties must have a value and all validation messages appear in the grid at the bottom of the Console window

Saving and Validating the Mapping Application Block

Last edited Jun 15, 2010 at 4:28 PM by ewdev, version 18

Comments

No comments yet.