I Eat, Drink SharePoint. Everything about SharePoint 2010. From what is sharepoint to custom development.
Monday, October 7, 2013
dynamic keyword in c# ,How and where to use?
Dynamic type in c#:
The 'dynamic' type in c# is quite handy in situation like where the user code doesn't know the final type of data it is going to return the client. The data type for the variables,properties or metnods which are declared as dynamic are not checked at the compile time, but will try to detect only at the runtime. Thus the dynamic type ,bypasses the compile time check to the runtime.This saves you a lot of effort which involves conversion of types explicitly or implicitly at different levels of your code execution.For example,i want to return an object as an acknowledgement to a function on its success, from clientside , say from a javascript file. But my method on server side raises another function in it which inturn returns an integer.
Now two points you have to consider:
The actual method on the server should have the type int [since the method inside it returns an integer] But the actual method should return an object to the client. In order to overcome these conflicts, generally we have to typecast the returned variables implicitly or explicitly at every level of your functionality for making the final returned value valid. Let us take an example to get better clarity: Suppose i have the following code in my javascript file.
$scope.saveLocation = function () { var orgLocationResource = $resource("api/locations"); orgLocationResource.save($scope.model, function (data) { $scope.locationId=data; }, function (error) { //To display error });
- The above code is a method in my controller which is intended to save the location details of my company.[this function get called when i click on save button in my locations form in the ui]
- I used $resource to point the data direction to my methods present in my web api controller.
- The save method has two callback functions,one of them to collect success status and the other to display error on failure.
- In this scenario i want my web api method to return the location id on successfully saving a location.
- Since location id is ony of my object properties i want the web api to return the location id as an object.
public class LocationsController : ApiController { public LocationService LocService { get; private set; } public LocationsController (LocationService LocService ) { this.LocService = LocService ; } [ActionName("location")]//for time being im not returning anything from here public void PostLocation([FromBody]Location details) { this.LocService.saveLocation(details); }
- In my web api controller i had a service in which i have the method to save the location details into the database.
- In my case i am usind petapoco as my database.Petapoco , on saving a record returns the id of the table which is an integer.
- The PostLocation method calls for savelocation method present in the service [here in my code LocationService is what the service i used]
public int saveLocation(Core.Model.Org org) { return Convert.ToInt32(this.DB.Insert(org.MapToNow observe carefully, the above method in my servce returns int,so the return type for my method 'PostLocation' should be int.But this method is actually intended to return an object. Generally we should do like this,())); } >
public int PostLocation([FromBody]Location details) { return this.LocService.saveLocation(details); //this need to be converted explicitly to type object[if dynamic return type is not used for our method] }Now if you dont like to do the conversions you should declare your method as dynamic like this
public dynamic PostLocation([FromBody]Location details) { return new { locationId=this.LocService.saveLocation(details)}; }Now its simple you can return it as an object. Eventhough the method from the service returned an int the dynamic type holds it which inturn return it as an object. Summary
- dynamic behaves as a placeholder for a type which is unknown until the runtime
- A variable,parameter or field declared as dynamic can hold any type.They cange their type during runtime.
- dynamic is more simple than types var or object as they both need implicit conversions to the return type.
Labels:
Angular js,
C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment