Title: Implementing the DataMapper Pattern With .Net 1.1
1Implementing the DataMapper Pattern With .Net 1.1
Code Camp II, Waltham, MA October 16th, 2004
- Don Demsak
- www.donxml.com
2Data Mapper Pattern Definition
A layer of Mappers that moves data between
objects and a database while keeping them
independent of each other and the mapper itself.
(Patterns of Enterprise Application Architecture
page 165 by Martin Fowler)
Mapper (def.) An object that sets up a
communication between two independent objects.
(Patterns of Enterprise Application Architecture
page 473 by Martin Fowler)
3Alternates To Data Mappers
- System.Data.DataSet
- No Domain Objects
- Strongly Typed DataSet
- Domain Objects in terms of data only
- Table Module
- A single instance that handles the business logic
for all rows in a database table or view. - Transaction Scripts
- Organizes business logic by procedures where each
procedure handles a single request from the
presentation.
4Layered Architecture
Which Transport Object?
5Data Transport Choices
- DataReader
- Fast But Only 2 Dimensional
- DataSet
- Easy But Still Only 2 Dimensional
- XmlReader
- Multi- Dimensional
- XmlSerialization
- XPathNavigator
- Mostly Unknown
6The Example (Using Northwind)
- Get a list of all employees including
- Each employees territories
- Each territories region
7Northwind ERD
8 Base Line Of Business Diagram
9GetEmployee SQL
SELECT Employee.EmployeeId as "Employee.Id", Em
ployee.LastName as "Employee.LastName", Employee
.FirstName as "Employee.FirstName", Employee.Tit
le as "Employee.Title", Employee.BirthDate as
"Employee.BirthDate", Territory.TerritoryId as
"Territory.Id", RTRIM(Territory.TerritoryDescrip
tion) as "Territory.Description", Region.RegionI
D as "Region.Id", RTRIM(Region.RegionDescription
) as "Region.Description" from dbo.Employees
Employee INNER JOIN dbo.EmployeeTerritories
et on Employee.EmployeeID et.EmployeeID INNER
JOIN dbo.Territories Territory on
et.TerritoryID Territory.TerritoryID INNER
Join dbo.Region Region on Region.RegionID
Territory.RegionID order by Employee.EmployeeID,
Territory.TerritoryId, Region.RegionId
10Base Data Access Layer Diagram
11Code Demo
12Performance Results
- DataReader 341
- DataSet 411
- XmlReader (XmlSerialization) 542
- XPathNavigator - 450
13DataReader Results
- Pros
- Fast
- Cons
- Domain Objects Need To Know the Underlying DB
implementation - Connection Controlled by Application Layer
- Domain Objects Need to Know Too Much About its
children - Need to Create Internal Setters For Some
Properties
14DataSet Results
- Pros
- Easy to Learn
- Cons
- Domain Objects Need to Know Too Much About its
children - Need to Create Internal Setters For Some
Properties
15XmlSerialization Results
- Pros
- Very Little Mapping Code Required
- Cons
- Connection Controlled by Application Layer
- Domain Constructors Must Be Public
- Collection Add Methods Must Be Public
16XPathNavigator Resuls
- Pros
- Complies with Domain Driven Design Principles
- DataStore Agnostic
- Declarative In Coding Style
- Cons
- Learning Curve (XPath)
- Slower than DataReader
17Questions?
Contact Info Web Site www.donxml.com Email
don_at_donxml.com