Last time I described that basic usage of Automapper to map the properties of one object to the properties of another. A more interesting use of the tool is as an object-relational mapper (ORM). Object-relational mapping is the process of taking data retrieved from the database and representing it as objects in your application. An alternative method would be to manipulate ADO.Net objects that are created by the Sql libraries as part of the ADO framework. The ADO.Net objects can either be data readers or data tables which can be tedious to work with. Rather than access the data through those objects, ORM allows you to create data models that represent the data from the database for more intuitive manipulation. Let’s look at an example.
Establishing the mapping relationship and actually mapping the objects gets a little tricky. Notice the line Automapper.Mapper.CreateMap() maps the data reader to a single object. The problem is that likely multiple records will be returned from the query resulting in multiple objects. As such when the IDataReader is actually mapped to a list of objects, the destination is specified as an IEnumberable. I prefer to directly use the List class because it is not uncommon to use the FirstOrDefault() Linq extenstion method when I know there is only 1 record being returned by the query if I’m retrieving a record by PK for example.
It would probably be a good idea to throw a try-catch around the Map method call in order to catch exceptions thrown when the results of an IDataReader cannot be mapped to an object. Also a good idea to close the IDataReader but the example shown is to demonstrate the object mapping capabilities.
The example above expects that the database column names identically match the data model properties (including case, ie ‘UserId’ does not map to ‘UserID’). In these cases when you create the mapping relationship you can specify which properties should be mapped where. Mapping the data reader is a bit annoying but looks like the below code.