I'm not sure if this has been considered or if there is anything resembling it already in existance but I think it would be awesome if you could accept a class as an action parameter. Rather than accepting a long list of primitive types or using the form
helper, what if you could define an incoming class and have the form parameters automatically mapped to the variables. So you could have something along the lines of "updatePerson (Person p)" where the primitive mutators are assigned just like the primitives
would be. I think it may actually be easier than creating the primitives, or at least just as hard. I think it would provide a number of benifits such as improved maintainabilty and it will reduce redundent coding.
But remember that everything has to originate from an HTTP request, which is a fairly 'low-tech' way of doing things. MVC moves towards relatively 'friendly' URLs. If you want to have action methods taking complex types, then URLs are going to need to include
serialized versions of the objects, which is going to make them significantly less friendly!
Sorry, I meant to clarify that this would have to occur on post-back data. I think I'm going to take a swing at implementing it.
Edit: reread your response and missed the note about serialization. I disagree with this, they are interpretting the post-back data and assigning it to primitive types. If that can be done, then an anon type could be initialized too..
Oh, I see what you mean. I still think we haven't gained a great deal if you do that. Aren't you just taking some code that would currently all reside in one place (Action method) and splitting it up so that it's spread across multiple places?
Eh, I think it would all be going to the same place - multiple class parameters may get a little out of hand. So no, I feel that it would just be a convience as you would remove the overhead of taking either a collection (using the form method) or individual
primitives and remapping them to an object.
But the sort of scenarios you are describing are ones where we're generally dealing with some form of POST. In these situations, the code would normally be fishing most of its values from the Request object. They wouldn't typically be arriving as parameters
of the action method. I think the answer would be to write more helpful constructors for the complex types that you want to create and then just pass the relevent values from the Request object. Perhaps the best answer is for you to knock up a quick example
(pseudo-code if you like!) of what you think should happen then we can discuss a concrete example!
What you're describing is already implemented in
MvcContrib - check out the DeserializeAttribute and the CastleBind attribute (which internally uses the Castle Project's data binder). These allow you do something like this:
public ActionResult Save([CastleBind] Customer customer) {
//Do stuff
}
But the sort of scenarios you are describing are ones where we're generally dealing with some form of POST. In these situations, the code would normally be fishing most of its values from the Request object. They wouldn't typically be arriving as parameters
of the action method. I think the answer would be to write more helpful constructors for the complex types that you want to create and then just pass the relevent values from the Request object. Perhaps the best answer is for you to knock up a quick example
(pseudo-code if you like!) of what you think should happen then we can discuss a concrete example!
What you're describing is already implemented in
MvcContrib - check out the DeserializeAttribute and the CastleBind attribute (which internally uses the Castle Project's data binder). These allow you do something like this:
public ActionResult Save([CastleBind] Customer customer) {
//Do stuff
}
Interesting and thanks, I'll look into it but I think that this sort of functionality (decoupled from Castle) would be great in the MVC framework
ChanceUSC
Member
319 Points
209 Posts
Accepting a class as an action parameter
Aug 13, 2008 02:16 PM|LINK
I'm not sure if this has been considered or if there is anything resembling it already in existance but I think it would be awesome if you could accept a class as an action parameter. Rather than accepting a long list of primitive types or using the form helper, what if you could define an incoming class and have the form parameters automatically mapped to the variables. So you could have something along the lines of "updatePerson (Person p)" where the primitive mutators are assigned just like the primitives would be. I think it may actually be easier than creating the primitives, or at least just as hard. I think it would provide a number of benifits such as improved maintainabilty and it will reduce redundent coding.
MelvynHarbou...
Star
8005 Points
1288 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 02:25 PM|LINK
But remember that everything has to originate from an HTTP request, which is a fairly 'low-tech' way of doing things. MVC moves towards relatively 'friendly' URLs. If you want to have action methods taking complex types, then URLs are going to need to include serialized versions of the objects, which is going to make them significantly less friendly!
ChanceUSC
Member
319 Points
209 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 02:28 PM|LINK
Sorry, I meant to clarify that this would have to occur on post-back data. I think I'm going to take a swing at implementing it.
Edit: reread your response and missed the note about serialization. I disagree with this, they are interpretting the post-back data and assigning it to primitive types. If that can be done, then an anon type could be initialized too..
MelvynHarbou...
Star
8005 Points
1288 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 02:33 PM|LINK
Oh, I see what you mean. I still think we haven't gained a great deal if you do that. Aren't you just taking some code that would currently all reside in one place (Action method) and splitting it up so that it's spread across multiple places?
ChanceUSC
Member
319 Points
209 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 02:39 PM|LINK
Eh, I think it would all be going to the same place - multiple class parameters may get a little out of hand. So no, I feel that it would just be a convience as you would remove the overhead of taking either a collection (using the form method) or individual primitives and remapping them to an object.
MelvynHarbou...
Star
8005 Points
1288 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 02:56 PM|LINK
But the sort of scenarios you are describing are ones where we're generally dealing with some form of POST. In these situations, the code would normally be fishing most of its values from the Request object. They wouldn't typically be arriving as parameters of the action method. I think the answer would be to write more helpful constructors for the complex types that you want to create and then just pass the relevent values from the Request object. Perhaps the best answer is for you to knock up a quick example (pseudo-code if you like!) of what you think should happen then we can discuss a concrete example!
JeremyS
Member
506 Points
99 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 03:03 PM|LINK
What you're describing is already implemented in MvcContrib - check out the DeserializeAttribute and the CastleBind attribute (which internally uses the Castle Project's data binder). These allow you do something like this:
public ActionResult Save([CastleBind] Customer customer) { //Do stuff }ChanceUSC
Member
319 Points
209 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 03:08 PM|LINK
The primitives can come in as parameters though: http://aspalliance.com/1540_ASPNET_MVC_Framework_Part_4_Handling_Form_Edit_and_Post_Scenarios.9
ChanceUSC
Member
319 Points
209 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 03:10 PM|LINK
Interesting and thanks, I'll look into it but I think that this sort of functionality (decoupled from Castle) would be great in the MVC framework
JeremyS
Member
506 Points
99 Posts
Re: Accepting a class as an action parameter
Aug 13, 2008 03:12 PM|LINK