Al.
Al.

Reputation: 875

How to support different clients with single WCF service

I need to return Employee class as a response to my clientA as follows.

[OperationContract]

    public Employee GetEmployee(String id)
    {
    ..
    ..
    return emp;
    }

    public class Employee 
    {
    public string Name;
    public string phoneNo;
    }

But the problem here is clientB is going to consume my service but the need a SSN of employee. If i add it into Employee class, I will be sending to clientA as well which wont need it. How to overcome this situation. If i have to do anything with custom deserialization, would not it be a problem if i about to handle 1000s of properties?

Which is the better solution? Any wcf architectural help would also be more helpful.

Upvotes: 0

Views: 810

Answers (4)

Turbot
Turbot

Reputation: 5227

I would suggest you take a read of the versioning strategies of the WCF that might be matches with your scenarios.

for my case, i implemented IExtensibleDataObject on the data contracts and manage in this layer instead of service contracts layer.

the downside would be difficulties to track different versions of contracts, however I'm practicing the semi-strict versioning and works well for me.

Upvotes: 1

Alex Filipovici
Alex Filipovici

Reputation: 32541

There has been a quite similar discussion on this link. Basically, it refers to conditional hiding the value of a data member.

You could decide if you want to expose a data member based on the client id or credentials (which should be passed as a parameter to the service method call).

[OperationContract]
public Employee GetEmployee(int clientId, String id)
{
    var employee = new Employee();

    //here you might use a mapping table between the clients and the exposed data members
    if (clientId == 1)
    {
        employee.IsSSNSerializable = true;
    }
    return employee;
}

The Employee class will expose the SSN based on the value of the IsSSNSerializable property:

[DataContract]
public class Employee
{
    public bool IsSSNSerializable = false;

    [DataMember]
    public string Name;
    [DataMember]
    public string phoneNo;

    public string SSN;

    [DataMember(Name = "SSN", EmitDefaultValue = false)]
    public string SSNSerializable
    {
        get
        {
            if (!IsSSNSerializable)
                return null;
            return SSN;
        }
        set
        {
            SSN = value;
        }
    }
}

Upvotes: 1

humblelistener
humblelistener

Reputation: 1456

I second Roy, but however if this is the only difference between client A and B. It would not hurt to expose a GetEmployee method with parameter IsSSNRequired which can have a default false value.

Upvotes: 0

Roy Dictus
Roy Dictus

Reputation: 33139

If different clients have different needs, the proper thing is to create different services as well.

You put the business logic in a shared business class (or distributed over multiple shared business classes), and expose a different service per different type of client. That keeps things nice, abstracted and secure, and nobody gets data they don't need or want.

Upvotes: 1

Related Questions