Simple Example of Object Hydrator

I'll be doing this in ASP.NET MVC.

WARNING: THIS IS NOT INTENDED TO SHOW BEST PRACTICES OF ASP.NET MVC

First download and compile Object Hydrator.

Create a new ASP.NET MVC project and add a reference to Foundation.ObjectHydrator from the newly created DLL from the above step.

Create a new class in your Models folder that looks like this:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Web;

    5 using Foundation.ObjectHydrator.GeneratorTypes;

    6 

    7 namespace SampleHydrator.Models

    8 {

    9     public class FakeCustomer

   10     {

   11         [FirstName("")]

   12         public string FirstName { get; set; }

   13 

   14         [LastName("")]

   15         public string LastName { get; set; }

   16 

   17         [AmericanAddress("")]

   18         public string Address { get; set; }

   19 

   20         [AmericanCity("")]

   21         public string City { get; set; }

   22 

   23         [AmericanState("")]

   24         public string State { get; set; }

   25 

   26         [AmericanPostalCode(false)]

   27         public string Zip { get; set; }

   28 

   29         [AmericanPhone("")]

   30         public string Phone { get; set; }

   31 

   32     }

   33 }

Next Create a class in your Models folder called FakeCustomerRepository that looks like this:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Web;

    5 using Foundation.ObjectHydrator;

    6 

    7 namespace SampleHydrator.Models

    8 {

    9     public class FakeCustomerRepository

   10     {

   11         public IList<FakeCustomer> AllCustomers()

   12         {

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

   14             return generator.GetList(20);

   15         }

   16     }

   17 }

Next...modify the Index method of your HomeController to look like this:

   13     public ActionResult Index()

   14         {

   15             FakeCustomerRepository fcr = new FakeCustomerRepository();

   16             ViewData.Model = fcr.AllCustomers();

   17             return View();

   18         }

Finally change your /Views/Home/Index.asp to look like this:

 

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<SampleHydrator.Models.FakeCustomer>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

Index

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2><ul>

<%foreach(SampleHydrator.Models.FakeCustomer fc in Model)

{

%>

<li><%=fc.LastName %>,<%=fc.FirstName %>

<br /><%=fc.Address %>

<br /> <%=fc.City %>, <%=fc.State %> <%=fc.Zip %>

<br /><%=fc.Phone %></li>

<%

} %> </ul>

</asp:Content>

 

Now browse to your default page and you should see a list of Random Customers...
Published Tuesday, September 15, 2009 3:33 PM by ryansjedi
Filed under: ,

Comments

# Simple Example of Object Hydrator - Ryan Smith@ Tuesday, September 15, 2009 6:57 PM

Pingback from  Simple Example of Object Hydrator - Ryan Smith

# Simple Example of Object Hydrator | I love .NET!@ Tuesday, September 15, 2009 8:32 PM

Pingback from  Simple Example of Object Hydrator | I love .NET!

Leave a Comment

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