• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Does this C# code require correction which is although working fine?

 
Ranch Hand
Posts: 2962
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the below C# code which is in a Web API being called from Java Script file :



Does the below lines require correction  ?





I used dynamic here since it I am not knowing the type of userData coming from the JS (the JS file below in the POST)



I did userData.ToString() as it requires it be be in String to be able to parse it.



I did parsedUserData.pieId.ToString() since _pieRepository.GetPieById requires int and for converting to int first I have to convert to string.

Below is the JS code calling the above Web API controller:



Thanks


 
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Don't manually deserialize the request body. Rely on the built-in deserialization mechanism:

However, for such a simple request (you're essentially just sending the ID of the resource you want to increment), it's better to just use a primitive type instead of an object, and when the parameter represents an ID, it's usually better to make it into a route parameter:

Note that I used a PUT request instead of a POST request here, because sending the same request twice will have no effect. I also used a string to represent the product ID. Never use integers for IDs, because you are not going to perform arithmetic on IDs.
 
Monica Shiralkar
Ranch Hand
Posts: 2962
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. These are useful learnings.

As you have rightly said I should use primitive type in the below case and not object . Understood that but have a doubt not directly related to it.

Suppose the request would have been a bit complex  ,then for that object would usage of dynamic have been okay ? If not so,then would there have been a way to know the type and use it instead of dynamic .
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, dynamic is not okay. I don't think there are many modern applications that warrant the use of dynamic.

The writer of the web API decides the data types used in the requests and responses. YOU are the writer of the web API, and you are writing the API in C#. That means that you have complete control over the data types used. Use strong C# types.

It's the client who has to adapt to the web API.
 
Monica Shiralkar
Ranch Hand
Posts: 2962
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to avoid dynamic in this line of code is by replacing it with object.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how will that help you exactly?
 
Monica Shiralkar
Ranch Hand
Posts: 2962
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value comes from below JavaScript code :

To the below web api code :

Based on the Java Script code , I thought it is a JSON string and therefore first tried making the type of userData in the web api method parameter as string. However that gave me an error saying cannot convert to string.
Next , I tried with var but compiler did not allow that since parameter cannot have type as var.

After this I am left with only object and dynamic.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you think it would be a string? The JSON you posted isn't a string. It's an object containing a property named 'pieId'. That means that you need to create a matching class in C# that resembles an object containing a property named 'pieId'. I showed you exactly how to do that in the post where I tell you to depend on the built-in deserialization mechanism of ASP.NET Core.

Using 'object' won't help you because then you can't access any of its members. Using 'dynamic' will work, but it's bad practice.
 
Monica Shiralkar
Ranch Hand
Posts: 2962
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I will do that way and remove the dynamic.

I have a question.As you said, using dynamic will be wrong  for this case then what can be a case where using dynamic would not be wrong ?
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are almost no cases where dynamic is appropriate.
 
Monica Shiralkar
Ranch Hand
Posts: 2962
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay.So why does such a feature exist when it should never be used.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because people love features that makes their life easier in the short term.
 
Monica Shiralkar
Ranch Hand
Posts: 2962
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic