This project is read-only.

Four simple steps to implement XmlDataMapper (Check sample project for illustration)

  1. Create a Business Entity / DTO for the tables
  2. Create an XML File with the mapping information between the table and the DTO.
  3. Specify the DTO and xml file in the configuration.
  4. Just call the DTOConverter<DTOName>.Convert(dataReader) and other such methods to convert your database record to DTO / Business Entity

Steps 1:

Sample table used for the demonstration
Orders_Table

Code for the DTO / Business Object / Model created for the above table
namespace CodePlex.XmlDataMapper.ConsoleClient.Dto
{
   //Warning: Strongly reccommend you use layering and place DTOs in a seperate DTO / Model layer
   [Serializable]
   public class Order
   {
      public Order() {/*no-op*/}

      //Note: for .NET 3.5 the below private variables are obsolete, we just need to do  public int Id {get; set;}
      private int id;
      private string name;
      private DateTime? orderDate;
      private byte orderStatusId;
      private bool isComplete;
      private string comments;
      private decimal amount;
      private bool isActive;

      public int Id 
      { 
         get { return this.id; } 
         set { this.id = value; } 
      }
      public string Name
      {
         get { return this.name; }
         set { this.name = value; }
      }
      public DateTime? OrderDate
      {
         get { return this.orderDate; }
         set { this.orderDate = value; }
      }
      public byte OrderStatusId
      {
         get { return this.orderStatusId; }
         set { this.orderStatusId = value; }
      }
      public bool IsComplete
      {
         get { return this.isComplete; }
         set { this.isComplete = value; }
      }
      public string Comments
      {
         get { return this.comments; }
         set { this.comments = value; }
      }
      public decimal Amount
      {
         get { return this.amount; }
         set { this.amount = value; }
      }
      public bool IsActive
      {
         get { return this.isActive; }
         set { this.isActive = value; }
      }
   }
}

Step 2

mapper xml file content
Filename: Order.XmlDataMapper.xml
<FieldMappers>
	<FieldMapper Property="Id"             TypeName="System.Int32"       ColumnName="Id"            IsPrimaryKey="true"  />
	<FieldMapper Property="Name"           TypeName="System.String"      ColumnName="Name"            />
	<FieldMapper Property="OrderDate"      TypeName="System.DateTime"    ColumnName="OrderDate"       />
	<FieldMapper Property="IsComplete"     TypeName="System.Boolean"     ColumnName="IsComplete"      />
	<FieldMapper Property="Comments"       TypeName="System.String"      ColumnName="Comments"        />
	<FieldMapper Property="Amount"         TypeName="System.Decimal"     ColumnName="Amount"          />
	<FieldMapper Property="OrderStatusId"  TypeName="System.Byte"        ColumnName="OrderStatusId"   />
	<FieldMapper Property="IsActive"       TypeName="System.Boolean"     ColumnName="IsActive"        />
</FieldMappers>

Step 3

App.Config or Web.Config file contents
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <sectionGroup name="CodePlex.XmlDataMapper" type="System.Configuration.SingleTagSectionHandler" >
         <section name="ApplicationInformation" type="System.Configuration.SingleTagSectionHandler" />
         <section name="XmlMappingFiles" type="System.Configuration.NameValueSectionHandler" />
      </sectionGroup>
      <section name="cachingConfiguration"
               type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching"/>
   </configSections>

   <cachingConfiguration defaultCacheManager="Default Cache Manager" >
      <backingStores>
         <add name="inMemory"
             type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching" />
      </backingStores>
      <cacheManagers>
         <add name="Default Cache Manager"
              expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="inMemory" />
      </cacheManagers>
   </cachingConfiguration>

   <CodePlex.XmlDataMapper>
      <ApplicationInformation IsWebApplication="false" RootPath="D:\dotnet2\Backup\XmlDataMapper\ConsoleClient\Config" />
      <XmlMappingFiles>
         <add key="CodePlex.XmlDataMapper.ConsoleClient.Dto.Order" value="Order.XmlDataMapper.xml" />
      </XmlMappingFiles>
   </CodePlex.XmlDataMapper>
</configuration>

Step 4

Code to call the DTOConverter to convert your DataTable / DataRow / DataReader to the DTO
Just call the method DTOConverter<Order>.Convert(reader); to do the conversion.
using CodePlex.XmlDataMapper;
// .....
IDataReader reader = command.ExecuteReader(CommandBehviour.CloseConnection);
IList<Order> list = DTOConverter<Order>.Convert(reader);  
//OR
adapter.Fill(dataSet, "Orders");
IList<Order> list = DTOConverter<Order>.Convert(dataSet.Tables["Orders"]);  


Practical sample in a method that returns order list (also demonstrates usage of BaseDataHelper)
using CodePlex.XmlDataMapper;
using CodePlex.XmlDataMapper.Helpers;
// .....
//Get Orders by a date range, a sample to demonstrate how custom Get/Retrieve is performed
public static IList<Order> GetByDateRange(bool isActive, DateTime dateRangeStart, DateTime dateRangeEnd)
{
   string storedProcedureName = "OrdersGetByDateRange";
   List<GenericDbParameter> paramList = new List<GenericDbParameter>(3);
   paramList.Add(new GenericDbParameter("IsActive", DbType.Boolean, isActive));
   paramList.Add(new GenericDbParameter("DateRangeStart", DbType.DateTime, dateRangeStart));
   paramList.Add(new GenericDbParameter("DateRangeEnd", DbType.DateTime, dateRangeEnd));
   using (IDataReader reader = BaseDataHelper.ExecuteReader(storedProcedureName, paramList, false)) // Demostrates how to use the BaseDataHelper 
   {
      return DTOConverter<Order>.Convert(reader);
   }
} 

Now that was not so tough was it? :)

Last edited May 28, 2010 at 12:38 PM by binoj7, version 17

Comments

No comments yet.