QueryString to Object (Custom Properties)

In your Software Development career sometimes you must encounter some sticky people; he or she may be your colleague or someone else who is working with you in any project.

What sometimes happens is it becomes very difficult to sync with this kind of person. When you are following patterns and practices and someone else says to you “I don't know, I will just work like I used to”.

No hard feelings, but if he reads this article, he's gonna kill me.

Now let's discuss the problem.

Problem Description

We are working on WCF, in other words I am developing services using WCF (REST) and he is developing a client application for an Andriod device. I am responsible for developing a completely restful web service (these are an alternative to Web Services based on SOAP and the Web Services Description Language (WSDL)) with JSON.

WCF supports a response and request format using JSON. It's quite easy to develop a RESTful web service using WCF to handle GET and POST requests.

Now the problem befgins, here he doesn't want to work with POST everything, he wants GET requests, in other words Create, Read, Update and Delete should be invoked using a GET request.

I explained the drawbacks of it to him, for nothing.

Now my turn; how to handle QueryString properties along with a DataContract?

Handling QueryString

In a general case we handle QueryStrings using the HttpContext Class as HttpContext.Current.Request.QueryString[“key”]. Now if I am using a DataContract like:

  1. [DataContract]  
  2. public class Employee  
  3. {  
  4.     [DataMember]  
  5.     public string EmployeeID { getset; }  
  6.     [DataMember]  
  7.     public string Name { getset; }  
  8.     [DataMember]  
  9.     public string Age { getset; }  
  10.     [DataMember]  
  11.     public string Branch { getset; }  
  12. }  


What if I need to insert this into the DB?

Do I do it like this:

  1. public  bool AddEmployee()   
  2. {  
  3.    EmployeeRepository repository=new EmployeeRepository();  
  4.    Employee empObj=new Employee();  
  5.    // will retrieve querysting automatically. Even if we don't specify in ServiceOperation Templet  
  6.    empObj.EmployeeId= HttpContext.Current.Request.QueryString[“EmployeeID”];   
  7.    empObj.Name= HttpContext.Current.Request.QueryString[“Name”];  
  8.    .  
  9.    .  
  10.    repository.Insert(empObj);  
  11.    return true;  
  12. }  


Or do I need to handle it with a method parameter like AddEmployee(string Employeeid,string Name…)?

Think. Think. Think…..

No, dear, there is no need to do this.

I did a little research and put some common logic into it and created a beautiful Utility method to handle the QueryString without pain.

Using reflection and generics here are the master methods that will relieve you of the pain of retrieving data from a QueryString one by one. 

Actually a QueryString stores the values in Key, Value Pairs and we just need to specify the key and it returns the value for that specific key.

“Because you are living in the world of Object Oriented Programming”
“And If you can't put you knowledge into work then you know nothing.”

The methods

  1. public T GetFromQueryString<T>() where T : new()  
  2. {  
  3.    var obj = new T();  
  4.    var properties = typeof(T).GetProperties(); // to get all properties from Class(Object)  
  5.    foreach (var property in properties)  
  6.    {  
  7.       var valueAsString = HttpContext.Current.Request.QueryString[property.Name];   
  8.       object value = Parse(property.PropertyType, valueAsString); // parse data types  
  9.    
  10.       if (value == null)  
  11.       continue;  
  12.    
  13.       property.SetValue(obj, value, null); //set values to properties.  
  14.    }  
  15.    return obj;  
  16. }   
  17. // all in one parse method.   
  18.    public object Parse(Type dataType, string ValueToConvert)  
  19.    {  
  20.       TypeConverter obj = TypeDescriptor.GetConverter(dataType);  
  21.       object value = obj.ConvertFromString(null, CultureInfo.InvariantCulture, ValueToConvert);  
  22.       return value;  
  23.    }   
  24.    return value;  
  25. }  


I have been surfing and found something related and then I put everything together.

Then yes...

How Does It Work?

Now you need to do nothing.

  1. public  bool AddEmployee()   
  2. {  
  3.    Employee  empObj =GetFromQueryString<Employee>()  
  4.    EmployeeRepository repository=new EmployeeRepository();  
  5.    repository.Insert(empObj);  
  6.    return true;  
  7. }  


It will handle everything for you!

Example: http://localhost/addEmployee?EmployeeId=001&Name=Anupam....

No need to worry if the parameters are missing, it will assign a default value for it.

Enjoy This.

Reference Websites

MSDN
StackOverflow

Thank you for reading .
share your ideas or comments if you have any.

 

Up Next
    Ebook Download
    View all
    Learn
    View all