Simple Object Hydrator Example using manual mapping

You can accomplish the same as the above but without using the Attribute decoration method, but rather a collection of Attributes.

Building on the above example, remove all the attributes from FakeCustomer.cs so it looks like this:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Web;

    5 

    6 

    7 namespace SampleHydrator.Models

    8 {

    9     public class FakeCustomer

   10     {

   11 

   12         public string FirstName { get; set; }

   13 

   14         public string LastName { get; set; }

   15 

   16         public string Address { get; set; }

   17 

   18         public string City { get; set; }

   19 

   20         public string State { get; set; }

   21 

   22         public string Zip { get; set; }

   23 

   24         public string Phone { get; set; }

   25 

   26     }

   27 }

Next replace your FakeRepository AllCustomers() method with the following:

   11      public IList<FakeCustomer> AllCustomers()

   12         {

   13             FillMe<FakeCustomer> generator = new FillMe<FakeCustomer>();

   14             IList<AttributeMap> mymap = new List<AttributeMap>();

   15             AttributeMap attmap;

   16             attmap = new AttributeMap();

   17             attmap.GeneratorName = "FirstName";

   18             attmap.PropName = "FirstName";

   19             mymap.Add(attmap);

   20             attmap = new AttributeMap();

   21             attmap.GeneratorName = "LastName";

   22             attmap.PropName = "LastName";

   23             mymap.Add(attmap);

   24             attmap = new AttributeMap();

   25             attmap.GeneratorName = "AmericanAddress";

   26             attmap.PropName = "Address";

   27             mymap.Add(attmap);

   28             attmap = new AttributeMap();

   29             attmap.GeneratorName = "AmericanCity";

   30             attmap.PropName = "City";

   31             mymap.Add(attmap);

   32             attmap = new AttributeMap();

   33             attmap.GeneratorName = "AmericanState";

   34             attmap.PropName = "State";

   35             mymap.Add(attmap);

   36             attmap = new AttributeMap();

   37             attmap.GeneratorName = "AmericanPostalCode";

   38             attmap.PropName = "Zip";

   39             attmap.GeneratorDefaultValue = "False";

   40             mymap.Add(attmap);

   41             attmap = new AttributeMap();

   42             attmap.GeneratorName = "AmericanPhone";

   43             attmap.PropName = "Phone";

   44             mymap.Add(attmap);

   45             return generator.GetList(20, mymap);

   46         }

It's a bit more verbose, but allows you to keep your model free of Attributes if you wish.  There's more work to be done for sure...but there's the sneak peek.

 

Published Tuesday, September 15, 2009 4:22 PM by ryansjedi
Filed under: ,

Comments

# re: Simple Object Hydrator Example using manual mapping@ Thursday, October 04, 2012 4:05 PM

<a href="www.vista-style-icons.com/.../search_engine.htm"> I consider, that you are not right. I am assured. I suggest it to discuss.</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)