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:
- [DataContract]
- public class Employee
- {
- [DataMember]
- public string EmployeeID { get; set; }
- [DataMember]
- public string Name { get; set; }
- [DataMember]
- public string Age { get; set; }
- [DataMember]
- public string Branch { get; set; }
- }
What if I need to insert this into the DB?
Do I do it like this:
- public bool AddEmployee()
- {
- EmployeeRepository repository=new EmployeeRepository();
- Employee empObj=new Employee();
-
- empObj.EmployeeId= HttpContext.Current.Request.QueryString[“EmployeeID”];
- empObj.Name= HttpContext.Current.Request.QueryString[“Name”];
- .
- .
- repository.Insert(empObj);
- return true;
- }
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
- public T GetFromQueryString<T>() where T : new()
- {
- var obj = new T();
- var properties = typeof(T).GetProperties();
- foreach (var property in properties)
- {
- var valueAsString = HttpContext.Current.Request.QueryString[property.Name];
- object value = Parse(property.PropertyType, valueAsString);
-
- if (value == null)
- continue;
-
- property.SetValue(obj, value, null);
- }
- return obj;
- }
-
- public object Parse(Type dataType, string ValueToConvert)
- {
- TypeConverter obj = TypeDescriptor.GetConverter(dataType);
- object value = obj.ConvertFromString(null, CultureInfo.InvariantCulture, ValueToConvert);
- return value;
- }
- return value;
- }
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.
- public bool AddEmployee()
- {
- Employee empObj =GetFromQueryString<Employee>()
- EmployeeRepository repository=new EmployeeRepository();
- repository.Insert(empObj);
- return true;
- }
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.