How to handle payment types with varying properties in the most elegant way.
Posted
by Byron Sommardahl
on Stack Overflow
See other posts from Stack Overflow
or by Byron Sommardahl
Published on 2010-04-23T00:11:52Z
Indexed on
2010/04/23
0:23 UTC
Read the original article
Hit count: 731
I'm using ASP.NET MVC 2.
Keeping it simple, I have three payment types: credit card, e-check, or "bill me later". I want to:
- choose one payment type
- display some fields for one payment type in my view
- run some logic using those fields (specific to the type)
- display a confirmation view
- run some more logic using those fields (specific to the type)
- display a receipt view
Each payment type has fields specific to the type... maybe 2 fields, maybe more. For now, I know how many and what fields, but more could be added. I believe the best thing for my views is to have a partial view per payment type to handle the different fields and let the controller decide which partial to render (if you have a better option, I'm open). My real problem comes from the logic that happens in the controller between views. Each payment type has a variable number of fields. I'd like to keep everything strongly typed, but it feels like some sort of dictionary is the only option. Add to that specific logic that runs depending on the payment type.
In an effort to keep things strongly typed, I've created a class for each payment type. No interface or inherited type since the fields are different per payment type. Then, I've got a Submit() method for each payment type. Then, while the controller is deciding which partial view to display, it also assigns the target of the submit action.
This is not elegant solution and feels very wrong. I'm reaching out for a hand. How would you do this?
© Stack Overflow or respective owner