What is the best practice for emulating overloaded methods over WCF?
Typically I might write an interface like this
interface IInterface
{
MyType ReadMyType(int id);
IEnumerable<MyType> ReadMyType(String name);
IEnumerable<MyType> ReadMyType(String name, int maxResults);
}
What would this interface look like after you converted it to WCF?
So clearly by default WCF does not allow you to overload methods in service. However there is one attribute in OperationContract which can allow you to overload method at service end. You need to set Name parameter of OperationContract to manually enable overloading at service side.
You need to be careful while overloading a method in Java, especially after the introduction of autoboxing in Java 5. Poorly overloaded method not only adds confusion among developers who use that but also they are error-prone and leaves your program at compiler's mercy to select proper method.
Method overloading increases the readability of the program. Overloaded methods give programmers the flexibility to call a similar method for different types of data. Overloading is also used on constructors to create new objects given different amounts of data.
Overloading is a powerful feature, but you should use it only as needed. Use it when you actually do need multiple methods with different parameters, but the methods do the same thing. That is, don't use overloading if the multiple methods perform different tasks.
You can leave it like that if you like. Just use the name property of the OperationContract attribute.
[ServiceContract]
interface IInterface
{
MyType ReadMyType(int id);
[OperationContract(Name= "Foo")]
IEnumerable<MyType> ReadMyType(String name);
[OperationContract(Name= "Bar")]
IEnumerable<MyType> ReadMyType(String name, int maxResults);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With