Updating Data Via Web Service
I have a database on SQL Server that contains a table called USERPROFILE. I also have a web service that has a method called UpdateUserProfile. The web method may update only parts of the user profile at a time, and not necessarily all of it.
How should I go about doing this?
1- UpdateUserProfile calls a data layer method which returns a UserProfile object populated with data from the USERPROFILE table. UpdateUserProfile then updates only the necessary data and then writes the object back to the database using a different method in the data layer.
2- The whole process should be managed by the data layer. (How?)
3- The whole process should be delegated to a stored procedure on SQL Server. The web service then calls this stored procedure. (Optional parameters? How should the stored procedure code look like? Is this better performance-wise?)
4- I'm so dumb that I didn't think about thebest solution. (Which is?!)
Thank you,
Sammy
[967 byte] By [
picoSam] at [2008-2-11]
I've been using a solution that works pretty nicely and actually involves very little code. In my webservice, I use a data adapter which is configured to have select, insert, update and delete commands. I prefer to use stored procedures, but you can use dynamic SQL for these commands as well. The web service has two types of methods: GetX and UpdateX. GetX takes primary key information and returns a DataSet (I like to use strongly-typed datasets, but you could use an untyped dataset, too). I can then bind that dataset to my UI controls, allowing the user to make edits. The UpdateX method takes as an argument the modified dataset and uses it to call the DataAdapter's Update method. For example:
| | namespace MyServices { public class ProfileService : System.Web.Services.WebService { System.Data.SqlClient.SqlDataAdapter da; public ProfileService() { /* Insert code here to set up the data adapter */ /* (or use the designer to generate it for you) */ } [WebMethod] public DataSet GetProfile(int ProfileID) { da.SelectCommand.Parameters["@ProfileID"].Value = ProfileID; DataSet ds = new DataSet(); da.Fill(ds); return ds; } [WebMethod] public void UpdateProfile(DataSet UpdatedProfile) { da.Update(ds); } } } |
Before all of the performance fanatics jump down my throat about how the DataSet object adds a lot of crud onto the wire and that you are much better off passing simple structures, let me acknowledge that this approach provides diminishing (performance) returns as your dataset gets larger and larger. The real benefit here is maintainability and simplicity. The new ObjectDataSource functionality in 2.0 makes using simple structures much more appealing, but I wasn't sure if you were using 2.0 or not.
Keep in mind also that this method works really well when you are updating most of the data in the dataset. If you have a lot of related tables and/or rows in the dataset that are passed from the GetX method and are not used in the UpdateX method, then you are carrying around a lot of data that you don't really need to. You could always strip out the unchanged data from the dataset before sending it back, but if you're going to write that much code, you defeat the real benefit of this solution anyway.
Todd Gray
I don't have much to say, except when you're doing web services try to stick to Xml friendly interface, and keep it simple. The recommeded use of Dataset is totally against this rule. Dataset is unique to .Net framework( other platforms has no idea about it's diffgram), and there's no guarentee that MS will try to keep it backward compatible in each new release.