Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
In ANDROID, I need to consume datafrom ms sql Database. So I have created WCF Rest service which return Json format and XML format data from DB. DB consists lot of stored procedures.

While calling ms sql stored procedure i have sent the input parameter in List KeyValuePair format. Is this good approach or any other solution available.

C#
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestfulService" in both code and config file together.
    [ServiceContract]
    public interface IRestfulService
    {
        [OperationContract]
        [WebInvoke(Method = "post", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{dbType}/{Query}/{Param}/{bSP}")]
        String XMLData(int dbType, string strQuery, List<KeyValuePair<string, object>> kvParam, bool bSP);

        [OperationContract]
        [WebInvoke(Method = "post", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/{dbType}/{Query}/{Param}/{bSP}")]
        String JSonData(int dbType, string strQuery, List<KeyValuePair<string, object>> kvParam, bool bSP);
    }
}
Posted
Updated 12-Jun-15 2:29am
v2
Comments
virusstorm 12-Jun-15 12:40pm    
While there is nothing wrong with your approach, you find that debugging your service may get difficult. From what I see in your code example, you have made a very dynamic solution which has its pros and cons. Another con you will run into is the structure KeyValuePair will result in a complex JSON object which can make it harder to consume.

My personal recommendation is to make your RESTful service action specific. For example, if you have a procedure that gets and entity, you would want something like this:
"json/{entity}/{id}" to get by Id
"json/{entity}/{name"} to get by name

The idea of RESTful service is to allow clients to consume and send data without the need for a WSDL or proxy class.
mn.sathish 12-Jun-15 23:39pm    
Thank you very much for your suggestion.
Mathi Mani 12-Jun-15 14:37pm    
Create a model with all the properties you need and use it instead of List<keyvlauepair>. It will make your code clean and reduce the complexities.
mn.sathish 12-Jun-15 23:36pm    
Thank you Mathi Mani for your valuable suggestion. I have more than 150 Stored Procedure. If I create model then I should create more and more. Hence I created dynamic method to call stored procedure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900