views:

192

answers:

1

I'm considering the value of a custom model binder that can instatiate immutable value objects defined in my domain layer. Then I can just pass them through the stack and set them on the appropriate entity. Has anyone tried? Had any luck? Think its a silly idea?

A: 

If by "value objects" you mean objects that can only be created by passing values to constructor, not by binding to fields, I think you have these solutions:

  1. Write a custom binder - though I can't tell now how to access several fields at once in there.

  2. Pass view model (that allows to bind to its fields) and then convert it to value object. Write a simple converter using reflection (couple of lines). You'll have to relate view model properties and constructor parameters either by name or by type. You can have your view model define what is the corresponding value type, and in action filter/OnActionExecuting call you converter - automatically. That's kind of semi-automatic model binding.

  3. Pass something like FormCollection to action and then call your reflection method like var value = BindValue<ValueType>(formCollection).

queen3