5

I have a class that I need to hydrate from a DataTable object. Usually I do this the manual way. (see code snipit). The DataTable object populated using ADO.NET and TSql. I need to transfer the values from the DataTable into my .NET class. Is there a utility method that will do this for me automagically? So that I can avoid repetitive code like the following?

            DriverSummary driver = new DriverSummary();
            driver.Id = (int)row["Id"];
            driver.UserId = row["UserId"] as string;
            driver.Name = row["Name"] as string;
            driver.TruckType = row["TruckType"] as string;
            summaries.Add(driver);

I know that the Entity Framework is a tool that is supposed to fill this gap. I haven't quite made the jump to Entity Framework. For now I'd like to have a method that is similar to MVC's utility method UpdateModel() Which is lightweight and simple and hydrates a class from a list of form-value pairs by matching up key names with property names.

Such a utility method would save me tons of time!

BrokeMyLegBiking
  • 5,898
  • 14
  • 51
  • 66
  • Have you taken a look at [Automapper](http://automapper.codeplex.com)? It might do what you want. :) – Tridus Apr 03 '11 at 22:49

1 Answers1

4

Like mentioned above I believe AutoMapper can do this now. You could also look at a ValueInjecter. ValueInjecter and DataTable

Community
  • 1
  • 1
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44