tags:

views:

219

answers:

2

I originally defined in my MessageContract a collection of SQLParameters in order to create a simple stored procedure execution through a WCF service. Apparently, SQLParameter is not serializable, so I need some advice on how to proceed here.

Is it possible to still somehow use SQLParameter as part of my WCF contract, or I have to do something else, like creating a custom class with the same properties as an SQLParameter, then create the SQLParameters elsewhere in my code?

UPDATE:
For further context as to why this situation comes about, originally the windows form client was connecting directly to the database to retrieve a DataSet for reporting purposes, using the usual ADO.NET objects. Now, the customer want a common web service to handle all reports. This is the best we can think of to handle it without too much changes.

A: 

First - if you want "database like" access over WCF, then ADO.NET Data Services is a far better option.

But no; you can't serialize an SqlParameter over WCF; you would need to encapsulate it in some other representation. Note that IMO it is quite dangerous to expose your database logic so close to the WCF boundary - I'd just have WCF methods that abstract this - i.e.

[OperationContract]
Customer[] FindCustomers(string id, string name, string location, ...);

Then you have a tightly controlled service interface.

Marc Gravell
I too agree with the riskiness of this design, but we also have a lot of reports with many different parameters, which is why we're looking for the easiest alternative, and this is what we can think of at the moment. ADO.NET Data Services might be worth checking out, though.
alextansc
A: 

It sounds like you're trying to take too much the easy way out. I would refactor those methods that were being used to do the direct database access, primarily by using an "Extract Method" refactoring. That would leave you with a (large) number of small methods, each accepting a set of parameters and returning a DataSet, and each with a specific purpose.

You might find you want to refactor those further, to reduce the number or increase the level of abstraction. But if not, then you should then do the equivalent of extracting all of those methods into one or more interfaces. Those interfaces would become the ServiceContracts for your WCF service. Move the methods into the new services to implement these service contracts, and you're pretty much done.

This works better with automated unit tests and good code coverage, of course. That will provide the level of confidence necessary to do something this radical.

John Saunders
This is a sound plan. I think I've spent too much time worrying rather than getting it done. So, might as well roll up my sleeves and get working. :)
alextansc