Question

The title is pretty self-explanatory.

I have a base WCF DataContract, let's call it the PersonContract, which covers all fields of the Person entity in my database.

I have a number of client applications that call the same service through endpoints of different interfaces implemented by that service. This is because (amongst other differences) I want every of those applications to be able to access and edit only a specific subset of the Person entity.

Now if I want to define a contract with all the properties of PersonContract except one, can I subclass PersonContract and ignore a single property in the subclass? Or is my only option building contracts from the smallest subset (but I doubt I can fully avoid repeating code then)?

Était-ce utile?

La solution

Out of curiosity I did a couple tests and it doesn't look like it'll work.

Here are the data contracts I used:

[DataContract]
public class Person
{
    [DataMember]
    public virtual string FirstName { get; set; }

    [DataMember]
    public virtual string MidName { get; set; }

    [DataMember]
    public virtual string LastName { get; set; }
}

[DataContract]
public class Person2 : Person
{
    [IgnoreDataMember]
    public override string MidName { get; set; }
}

And my service contract:

public interface IService1
{
    [OperationContract]
    Person GetPerson();

    [OperationContract]
    Person2 GetPerson2();
}

Both operations return the same result.

Another way that you might be able to produce the results you're looking for could be to define your minimal contract (the one with missing the excluded properties) and inherit from it adding the field needed by the other operation.

The equivalent data contracts would look something like:

[DataContract]
public class Person2 : Person
{
    [DataMember]
    public virtual string MidName { get; set; }
}

[DataContract]
public class Person
{
    [DataMember]
    public virtual string FirstName { get; set; }

    [DataMember]
    public virtual string LastName { get; set; }
}

And I've verified that the results are as I would expect.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top