2

I am bootstrapping myself through my very first ASP MVC project, and could use some suggestions regarding the most appropriate (data-side) model binding solution I should pursue. The project is 'small', which really just means I have very limited time for chasing down dead-ends, which cramps my available learning curve. Thanks heaps for reading!

Background: a simple address search app (ASP.NET MVC 3) that sends user form input to a vendor-sourced server, and presents the results returned from the server in a rules-driven format. There is no CRUD-style repository as such; this is an entirely read-only application. The vendor's .NET server API specifies the use DataTable objects (System.Data.DataTable) for both requests and replies.

Currently: I have a prototype under construction to validate server behavior and inform the application's design. There is a conventional MVC data model class for the input form which works great, auto-binding with the view just as you'd expect. When submitted, the controller passes this input model to my vendor API wrapper, which is currently binding to the request table medieval-style:

public DataTable GetGlobalCandidateAddresses(AddressInputModel input)
{
    ...
    DataRow newRow = dataTable.NewRow();
    newRow[0] = input.AddressLine1;
    newRow[1] = input.AddressLine2;
    newRow[2] = input.AddressLine3;
    ...
    dataTable.Rows.Add(newRow);
    ...

This works fine; the inputs are fixed and have very light validation requirements. However, if there is a modern framework solution for quickly reflecting a DataTable from my model, that would be peachy.

My real conundrum, however, is on the reply. The table returned by the server contains a variable column-set, with any combination of at least 32 possible unordered fields on a per-transaction basis. Some of the column names contain compiler-invalid characters ("status.description") that won't map literally to a model property name. I will also have a need to dynamically map or combine some of the reply fields in the view model, based on a series of rules (address formats vary considerably by country), so not all fields are 1-to-1.

Just to get the prototype fired up and running, I am currently passing the reply DataTable instance all the way back to a strongly-typed view, which spins it out into the display exactly as is. This is nice for quickly validating the server replies, but is not sufficient for the real app (much less satisfying best practices!).

Question: what is my best tooling approach for binding DataTable rows and columns into a proper model class for display in a view, including some custom binding rules, where no underlying database is present?

Virtually all of the training materials I am consuming discuss classic database repository scenarios. The OnModelCreating override available in the Entity Framework seems ideal in some respects, but can you use a DBContext without a DB connection or schema?

If I have to roll my own model binder, are there any tricks I should consider? I've been away from .NET for a while (mostly Java & SQL), so I'm picking up LINQ as I go as well as MVC.

Thanks again for your attention!

KurtisR
  • 45
  • 5

1 Answers1

1

Create a poco display model AddressDisplay and do custom object mapping from the data table to the display model. You can use data annotations for formatting but you can also do that in your custom mapping. You shouldn't need to create a custom model binder.

So create two poco models, AddressInput and AddressDisplay, you can use data annotations on AddressInput for validation. When AddressInput is posted back, map it to the outbound data table. When the response is received, map the inbound data table to AddressDisplay and return the view to the user.

Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
  • Thanks for the answer! Sometimes you just need the right keywords, and this helped me realign my research around "view model vs. domain object" concerns. It also helped me discover Automapper, which is good to know about. Although this app doesn't have domain pocos in the usual sense (it's 90% presentation), I have an idea how to proceed with a translation object for the reply table. – KurtisR Mar 29 '11 at 14:45
  • AutoMapper is great, I don't know if this helps you too: http://stackoverflow.com/questions/5462671/valueinjecter-and-datatable – Derek Beattie Mar 29 '11 at 16:17