KAMAL VERMA
KAMAL VERMA

Reputation: 21

WCF method that can take any class object as parameter

public interface ICommunicationLoggService
{
    [OperationContract]
     bool SaveLog(Employee emp);
}

I need the above method as generic so that it can take the any class object as a paramter not for specific Employee object.

Like I have 3 Classes

class Employee
{

}
class Student
{

}
class Address
{

}

Now when I will call the saveLog() then it should take based on class..

I am not good in english hope any one will understand the question and provide me the solution..

Upvotes: 2

Views: 90

Answers (1)

Tobias Theel
Tobias Theel

Reputation: 3217

Update

After some research, i came to the conclusion that it seems to be the best option for SOA to not use generics in these interfaces.

So the easiest solution seems to have a separate log method for each entity type.

public interface ICommunicationLoggService
{
    [OperationContract]
    bool SaveLog(Employee emp);
    [OperationContract]
    bool SaveLog(Studend emp);
    [OperationContract]
    bool SaveLog(Address emp);
}

Option 2

You could also use a bass class for your "loggable" types.

public interface ICommunicationLoggService {
  [OperationContract]
  bool SaveLog(Loggable loggable);
}

public class ComunicationLoggerService : ICommunicationLoggService {
  public bool SaveLog(Loggable loggable) {
    return false;
  }
}

public class Loggable { }

public class Employee : Loggable { }

public class Studend : Loggable { }

public class Address : Loggable { }

public class Example 
{
  public void Foo() 
  {
    Employee employee = new Employee();
    ICommunicationLoggService loggService = new ComunicationLoggerService();
    loggService.SaveLog(employee);
  }
}

Old Part

You can use generics to achieve the desired behavior. You shouldn't use generics in WCF. I have not realized that in the first place.

Thanks to Camilo Terevinto for the explanation, why using a generic is bad:

The problem is the WSDL. You cannot serialize a type that you don't know of

public interface ICommunicationLoggService
{
    [OperationContract]
    bool SaveLog<TEntity>(TEntity emp);
}

Upvotes: 3

Related Questions