Is there a way to use a base class under a data contract entity on the client side? In the generated code it explicitly inherits from object. I have a base class that I use in my application architecture in order to perform cross-cutting tasks. I'm wanting to integrate my existing business objects into the WCF service by making them partial classes. It is working wonderfully, except that I can't seem to use the base class that I want to.
You have several other options for adding functionality to a class on the client side. Which one to use depends on the use case you're looking for.
If you all you want is to add new methods to the client version of the class, then you can use partial classes to add new methods to the generated class directly.
If you want to have several objects on the client side share a set of methods so they can be accessed polymophically, then you can use partial classes to add an interface to the class defining the new methods, create a helper class that provides the method definitions, and then delegate to the helper class in each DataContract object that implements the ineterface.
public interface IDoStuff
{
void DoStuff()
}
public interface IDoStuffContext
{
int Data {get;}
string Value {set;}
}
public class DoStuffHelper
{
private IDoStuffContext _wrapped;
public DoStuffHelper(IDoStuffContext wrapped)
{
_wrapped = wrapped;
}
public void DoStuff() {
if(_wrapped.Data == 10) {
_wrapped.Value = "Hello World";
}
}
}
public partial class MyDataContract1 : IDoStuff
{
private DoStuffHelper _helper = new DoStuffHelper(this);
public void DoStuff() {
_helper.DoStuff();
}
}
public partial class MyDataContract2 : IDoStuff
{
private DoStuffHelper _helper = new DoStuffHelper(this);
public void DoStuff() {
_helper.DoStuff();
}
}