I am new to Web/WCF/Services – so bear with me 🙂
I am trying to analyse the code-flow in a .NET application but I have a hard time understanding the reasoning behind using the following approach when it comes to exposing services :
Now, in the application, I have a C# Class Lib Proj which has a C# Class Library project called “XXX.YYY.Services ” within which I have 2 files :
ISomeServiceProviderClass.cs
SomeServiceProviderClass.cs
Within the ISomeServiceProviderClass.cs I have the following structure for code :
[ServiceContract]
public interface ISomeServiceProviderClass
{
[OperationContract]
int SomeFunc();
}
Then there is the
SomeServiceProviderClass.cs class which is defined like so :
public class SomeServiceProviderClass: ISomeServiceProviderClass
{
public int SomeFunc()
{
/// some code to do the implementation..
}
}
This is a sample service structure in my Provider Application. Obviously the Consumer application is using this service to show results in its UI layer.
Now I want to know, what is the need to have the interface called ISomeFile.cs and then SomeFile.cs which implementes this interface ?
I guessed that one of the reasons could be to allow for different types of implementations in each of the class that implements this interface …
If that is the case, then why not have just a SomeServiceClass.cs file within which I have Virtual functions that can be overridden in derived classes ? Or why not have an Abstract class with abstract functions ?
The clue is in the
[ServiceContract]and[OperationContract]attributes in theISomeServiceProviderClass. These indicate thatISomeSomeServiceProviderClassis defining a WCF Service Contract.This contract (ie, the interface) can be implemented by code on the server side to carry out the actual operations, and on the client side it is dynamically implemented by WCF to create a proxy that relays calls across the network to the server.
You would not want to use an abstract class in this case, because that would restrict the freedom of implementation for the server and client side objects.