views:

70

answers:

2

Good day people. I have never posted on these type of sites before, however lets see how it goes.

Today i started working with WCF for the first time, i watched a few screencasts on it and now i am ready to jump into my first solution implementing it. All is well and so far all is working, although my question comes when i create the WCFServiceClient in my calling program/ client.

Lets say in my ServiceContract/ interface defining my methods exposed to the client, there are many methods each related to some entity object. How can i logically group all of the related methods of a particular entity together, so that in my code it would look like

e.g.

WCFServiceClient.Entity1.Insert();
WCFServiceClient.Entity1.Delete();
WCFServiceClient.Entity1.GetAll();
WCFServiceClient.Entity1.GetById(int id);

WCFServiceClient.Entity2.AddSomething();
WCFServiceClient.Entity2.RemoveSomething();
WCFServiceClient.Entity2.SelectSomething();

...

Instead of

WCFServiceClient.Insert();
WCFServiceClient.Delete();
WCFServiceClient.GetAll();
WCFServiceClient.GetById(int id);
WCFServiceClient.AddSomething();
WCFServiceClient.RemoveSomething();
WCFServiceClient.SelectSomething();

I hope this makes sense. I have searched google, i have tried my own logical reasoning, but no luck. Any ideas would be appreciated.

Shot Juan

+2  A: 

You can't really do that. The best you can do is to put all the "entity 1" methods in one service contract, and all the "entity 2" methods in another. A single service can implement multiple service contracts.

John Saunders
That would have been my next plan, however that means in my client i would have to add another reference in the web.config for Service2 right?
Juan
@Juan: no. It's one service, with two contracts.
John Saunders
Ok i am a little bit lost then. In the WCF Service Configuration Editor, how will i go about configuring this service for multiple contracts?
Juan
A: 

WCFServiceClient.Entity1.Insert();

WCFServiceClient.Entity2.AddSomething();

This smells like two separate service interfaces - let each service contract (interface) handle all the methods you need for a single entity type:

[ServiceContract]
interface IEntity1Services
{ 
    [OperationContract]
    void Insert(Entity1 newEntity);
    [OperationContract]
    void Delete(Entity1 entityToDelete);
    [OperationContract]
    List<Entity1> GetAll();
    [OperationContract]
    Entity1 GetById(int id);
}

[ServiceContract]
interface IEntity2Services
{ 
    [OperationContract]
    void AddSomething(Entity2 entity);
    [OperationContract]
    void RemoveSomething(Entity2 entity);
    [OperationContract]
    SelectSomething(Entity2 entity);
}

If you want to, you could have a single service class actually implementing both interfaces - that's totally possible and valid.

class ServiceImplementation : IEntity1Services, IEntity2Services
{
    // implementation of all seven methods here
}

Or you can create two separate service implementation classes - that's totally up to you.

class ServiceImplementation1 : IEntity1Services
{
    // implementation of four methods for Entity1 here
}

class ServiceImplementation2 : IEntity2Services
{
    // implementation of three methods for Entity2 here
}

Does that help at all?

marc_s
Yeah thanks a lot man. What i did yesterday was create a partial class, each implementation of the class implementing a different servicecontract, as well as creating an endpoint then for each contract under the same service.
Juan