I have a method in my code which must have a void return type and it must only take a single, specific parameter. I need to return something from it.
To give you the details: I have a WCF service which has SQL server access on it. It had code generated by a program called SQLMetal.exe. This gave me a partial class (DataContext) with partial methods. I’m building the other part of this partial class, and these partial methods are things like ‘InsertUser’ or ‘DeleteUser’. They aren’t implemented in the other part of the partial class, but I guess this means I have to implement them. That’s fine and all, but if they’re partial methods, they have to have a void return type.
So what if I want to return something from one of these? I mean, an application can hook up to this service, and can use it to access database information. That application’s going to need to know if there was an error on the service side.
So it’s a partial method with a void return type and a predefined set of parameters.
What to do?
Edit: More details!
Firstly, why can’t I change the generated code? Well, that is a good suggestion, but I have a concern about that. The reason it’s generated as it is is in order to minimuse future coding problems if the database were to change and you had to re-generate the code.
Secondly, some code:
[System.Data.Linq.Mapping.DatabaseAttribute(Name = "HVD01")]
public partial class HydraDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertClient(Client instance);
partial void UpdateClient(Client instance);
So that’s the relevant part of the generated code.
Then is my part. This is in the other partial class which marries up to the generated one.
public void UpdateClient(Client client)
{
//Primary key can't be zero
if (client.ClientID == 0)
return;
dc.Clients.Attach(client, true);
dc.SubmitChanges();
}
Obviously there are potential things that need to be returned.
How about exceptions? Well if I throw one on the WCF service, I don’t think the application will get it (I may be wrong here). The application consumes the service, but I don’t think throwing an exception on the service would reach the consuming application. Would it?
Maybe just changing the functions is for the best. I just figured that the whole point was to leave the generated code as much as possible in case you needed to regenerate it.
Thanks again.
These partial methods you mention are designed to allow you to hook into the internal workings of LinqSql – they are not really supposed to become the public API of your data access layer. You can add any other public methods you like into the DataContext class (with no signature restrictions) – although I’d recommend looking into the Repository Pattern.
Edit In response to comment
Here’s a psuedo example of a simple repository implementation which can act as a public wrapper around the DataContext, and can return contextual information about the result of the operation as you described in your post..
Hope that gives you food for thought 🙂