Juan
Juan

Reputation: 23

WCF Service Clients

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

Upvotes: 2

Views: 196

Answers (2)

marc_s
marc_s

Reputation: 755321

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?

Upvotes: 0

John Saunders
John Saunders

Reputation: 161821

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.

Upvotes: 2

Related Questions