Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Best Practice for "Overloaded" methods

Tags:

.net

wcf

service

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?

like image 233
Nate Avatar asked May 06 '10 19:05

Nate


People also ask

Is method overloading possible in 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.

Is method overloading good practice?

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.

What is the most practical reason to create an overloaded 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.

When would you use overloaded methods?

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.


1 Answers

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);    
}
like image 61
mwilson Avatar answered Sep 21 '22 21:09

mwilson