The JsonResult class is a very useful way of backing Json as an action for client through AJAX.
Public JsonResultMellialList (string txtEmail) {// ... back to new JsonResult ... {data = new {foo = "123", success = true}}; }
Although (at least according to my first impression) it is not really a good separation of worries.
- It is difficult to write unit testing methods because they do not have well-typed data to test and they know how Jason is interpreted.
- It is difficult for some other visuals in the future that the HTTP (or serialization in a remote protocol) should be 'plugged in' because in such cases it would be deserializing its unnecessary reactions and is.
- If you have two different places that require the results of that action? One wants Jason and wants an XML or maybe fully rendered footage.
I am thinking that translating between an object and JSON was not implemented in a declaration by a feature. In the code given below you are essentially telling MVC that this method is convertible to Jason
, and after that if it is called from an AJAX customer, then an attribute will be given to New JsonResult () is created for
conversion is done internally.
Unit tests can only take action results ( ObjectActionResult
) and strongly remove Foo
. [JsonConvertible] Join the public functioning mailing list (string txtEmail) {// ... return new object results () {data = new fu (123, true)}; }
I was just curious as to any alternative patterns for people's ideas and followers.
These are just my initial remarks - perhaps there are more reasons why it is not an ideal design (and probably very much why its a completely acceptable and practical!) I am just theoretical and devils-advocates I feel like tonight.
& nbsp; * Disclaimer: I have not even started thinking about how the feature will be implemented or which side effects or deliveries may be etc.
I think if you are working on nothing, what will happen if the controller uses its public interface Do you know about JSON?
I was once told: "Make your code generic, do not apply your application generic."
You write an application controller here. This application is fine for the controller - its responsibility is to reduce between models and ideas and to implement changes in the model - to learn about a particular view (JSON, HTML, Palestine, XML, YAML).
In my own projects, I usually have something like this:
Interface IFormatter {ActionResult format (object o); } Category HtmlFormatter: IFormatter {// ...} Category JsonFormatter: IFormatter {// ...} Category PlistFormatter: IFormatter {// ...} Category XmlFormatter: IFormatter {// ...}
< / Ex>Originally "formats" which take the object and give them a different representation.
HtmlFormatter
s is also smart for the output table, if their object implementsIEnumerable
.Now the administrators who return data (or which can generate some parts of it, using the
HtmlFormatter
s) website take a "format" argument:join public action mailing list (string txtEmail, string format) {// ... return formatter.For (format). Format (new {foo = "123", success = true}); }
You can add your "object" formater to your unit tests:
class objectfarmer: IFormatter {ActionResult format (object o) {New ObjectActionResult return () {data = o}; }}
By using this methodology, you can output any queries / actions / procedures / AJAX calls, you want to call them, in different formats.
Comments
Post a Comment