0

I am creating a WCF service with multiple interfaces say "IService" as my main interface (Service Contract) and other interfaces added based on my modules. like - "IStudent", "IClass", "ITeacher" etc.

So what i am planning is like this -

[ServiceContract]
public interface IKCCWebService : IStudent, IClass, ITeacher
{}

and use my service like this -

public partial class MyService : IKCCWebService
{} 

public partial class MyService : IStudent
{
   // Student interface implemented
} 

public partial class MyService : IClass
{
   // Class interface implemented
} 

And so on. If i do this i am getting error - "MyService does not implement interface member "IKCCWebService"".

I dont want to implement all interfaces at one shot so i am taking this approach.

Let me know if you have any suggestions.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
Abhijith Nayak
  • 448
  • 7
  • 21

1 Answers1

3

I think you're misunderstanding what partial classes are for.

Partial classes don't allow you to implement "part" of a class or interface. Partial classes are just a method to allow parts of a class to be defined in different places. When the compiler builds your project, it finds all of the partial definitions for a class and mashes them together into the complete definition.

For example, this code:

partial class MyClass: BaseClass
{
}

sealed partial class MyClass: IFirstInterface
{
    public void FirstMethod(){ }
} 

public partial class MyClass: ISecondInterface
{
    public void SecondMethod(){ }
} 

really translates to this:

public sealed class MyClass : BaseClass, IFirstInterface, ISecondInterface
{
    public void FirstMethod(){ }
    public void SecondMethod(){ }
}

All of the members, modifiers, implemented interfaces, and inherited classes get applied to the class when it is built.

The typical usage for this is when part of a class is dynamically generated (e.g. by a tool or designer) and part of the class is manually generated. The class can be declared partial and split between two files - one that you don't touch (because the tool could regenerate it at any time) and one that the tool doesn't touch, where you put all of your code.


In your case, declaring multiple partial classes serves no purpose. Since one of your partial definitions includes MyService : IKCCWebService, the MyService class is required to define all of the members defined by IKCCWebService.

If you don't want to implement the logic for all the interfaces in one shot, you don't have to. Just define the required interface methods with a single line: throw new NotImplementedExeption(); The compiler doesn't care what the methods do, so long as the methods are defined.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50