Mapping Application Block 4.1
The Mapping Application Block completes the Object Relational Mapping (ORM)
tool started with the Query Application Block (QAB) by taking the Data Transfer Object produced from the QAB and mapping it to a configured domain object
taking a domain object and producing a Data Transfer Object to pass to the QAB. All configuration of such mappings naturally uses the Enterprise Library Configuration Console.
In essence the MAB, combined with the QAB, gives the same functionality as the
, bringing an ORM
tool to the Enterprise Library. The Configuration Console plays its part too as the user interface to the ORM tool. All queries to data sources and object mapping are configured using the console
and mappings can even be autogenerated by reading from a domain object assembly.
The developer is not limited to the providers out-of-the-box but can extend the block by adding providers of their own in the normal Enterprise Library extensible fashion.
The following are the main features associated with the Mapping Application Block:
- Any domain object can be serialised to a DataSet for interfacing to the QAB.
- The MAB can support multiple super types stored in a single location. for example, say you had several domain object animal super types such as
Cat, Dog and Horse all inheriting from an Animal abstract class, then these might be stored in a single data storage table called
Animal with one column dedicated to defining the super type e.g. a char(1) column containing the values C, D or H. This scenario can be configured within the MAB by choosing a
- Configuration facility for Mappers and Maps where a mapper is a provider that converts between domain objects and DataSets and a map defines the relationship between a column of a DataSet table and a public property of a domain object.
- You can use either the stand-alone Enterprise Library Configuration Console or the Visual Studio integrated Console to manage all Mapping Application Block settings.
- If you need to manage data from a mapper type not currently supported then you can extend the application block by creating
custom mapper providers and custom map providers
- Attributes are provided to decorate pre-built domain objects so that the mapper configuration can be automatically generated.
- Management and monitoring features include Windows Active Directory Group Policy integration and instrumentation features such as
Performance Counters, Windows EventLog logging and WMI events.
- Dependency Injection support. The Mapping Application Block integrates with
Unity v1.2 for the creation of all its objects.
If you need convincing as to why you would use this block, have a look at the following reasons:
- You need an ORM tool to rapidly build a Data Access Layer.
- You need an ORM tool that will work across a service layer.
- You want to be able to map domain objects to various data source types e.g. data tables, XML files or even services.
- You want complete flexibility with the design of your domain objects without any restrictions imposed by your ORM tool.
- You want to use a familiar configuration interface for your mappings that you already use for your data access, exception handling and logging etc.
A Quick Code Sample
The following code sample shows the basic function of the block, how to create a collection of objects from a data store:
// create a collection of MyObject objects by querying the data store
Collection<MyObject> myObjectCollection = MappingFactory.CreateMapper("MyObject Mapper").ToDomainObjectCollection<MyObject>(QueryFactory.CreateQuery("Read MyObject").ExecuteForRead());
This next code sample shows how create a specific object from a data store:
// create a specific MyObject object by querying the data store with parameters
IDictionary<string, object> parameters = new Dictionary<string, object>();
MyObject myObject = MappingFactory.CreateMapper("MyObject Mapper").ToDomainObject<MyObject>(QueryFactory.CreateQuery("Read MyObject By Id").ExecuteForRead(parameters));
This next code sample shows how to write an object away to a data store using the DataSet parameter syntax of the ExecuteForWrite() method:
// write an object away to a data store
MyObject myObject = new MyObject();
myObject.Id = Guid.NewGuid();
myObject.Name = "Fred";
myObject.Description = "A good friend";
QueryFactory.CreateQuery("Write MyObject").ExecuteForWrite(MappingFactory.CreateMapper("MyObject Mapper").FromDomainObject(myObject));
Installing the Assemblies
When you build the Mapping Application Block the assemblies are compiled without a strong name key. If you want to put these assemblies into the GAC then you will need to assign a strong name key to each assembly and recompile (this is not neccessary for any
assembly). If you use the BuildLibraryAndCopyAssemblies.bat
console script then the assemblies are copied to the EntLibContrib
folder. You can reference the assemblies from here for your application however; to use the Configuration console you will need to copy all of the following assemblies to your installation of the Enterprise Library Configuration Console which is
usually in C:\Program Files\Microsoft Enterprise Library 4.1 - October 2008\Bin
The Mapping Application Block 4.1 consists of the following assemblies:
- EntLibContrib.Mapping.dll - the main assembly for the block
- EntLibContrib.Mapping.Configuration.Design.dll - the plug-in interface to the Enterprise Library Configuration Console for the Mapping Application Block.
- EntLibContrib.Common.dll - Common Core code shared between all EntLibContrib extensions
- EntLibContrib.Common.Configuration.Design.dll - Common Configuration Console design code, shared between all EntLibContrib extensions
The MAB is inspired by the ideas in Martin Fowler's
book Patterns of Enterprise Application Architecture
. The Mapping Application Block implements the DTO and Mapper Objects. The Mapping Application Block (MAB) complements the Query Application
Block (QAB) to complete the implementation of PoEAA by adding configurable Object Relational Mapping to a Domain Model.