Interface Aggregation in C #

I have this class:

class UrlManagementServiceClient : System.ServiceModel.ClientBase<IUrlManagementService>, IUrlManagementService 

ClientBase implements IDisposable and ICommunicationObject . I also have this interface:

 interface IUrlManagementProxy : IUrlManagementService, ICommunicationObject, IDisposable 

But I cannot drop UrlManagementServiceClient objects in IUrlManagementProxy . Is there any way to do this? I want to get an object that can access all methods on all three interfaces.

+6
source share
5 answers

You can only use the interfaces that you inherit, in order to be able to use IUrlManagementProxy , you need to implement this interface.

 class UrlManagementServiceClient : System.ServiceModel.ClientBase<IUrlManagementService>, IUrlManagementProxy 

You can then drop UrlManagementServiceClient on UrlManagementProxy , IUrlManagementService , ICommunicationObject or IDisposable .

Edit
The generated WCF classes are partial, which means that you can extend the class definition in another file. Put

 public partial class UrlManagementServiceClient : IUrlManagementProxy {} 

in another code file, and your class will also implement your full IUrlManagementProxy interface, and you can apply it to IUrlManagementProxy .

+4
source

Make UrlManagementServiceClient implement IUrlManagementProxy instead of IUrlManagementService

 class UrlManagementServiceClient : System.ServiceModel.ClientBase<IUrlManagementProxy>, IUrlManagementProxy 
+1
source

You need to implement IUrlManagementProxy on UrlManagementServiceClient . There is no other way - this is a separate type.

0
source

Obviously, you cannot pass an object to an interface that it does not implement.

But what you can do (if it makes sense to implement all the methods for each of these interfaces from an instance of UrlManagementServiceClient ), wrap your UrlManagementServiceClient in an object that implements the interfaces you need.

This is called a decorator drawing (not a proxy). Typically, the proxy server "appears" as the main object, whereas in this case you add functionality that the client does not have.

In other words, you will need a new class:

 public class UrlManagementClientProxy : IUrlManagementProxy { // we need a reference to the underlying client private readonly UrlManagementServiceClient _client; // underlying client is passed to the proxy in constructor public UrlManagementClientProxy(UrlManagementServiceClient client) { _client = client; } #region IUrlManagementProxy methods // you implementation goes here. if the underlying client // already implements a certain method, then you just need // to pass the call // for example, client already implements methods // from the IUrlManagementService interface, so use them public string GetUrl() // made up { return _client.GetUrl(); } #endregion } 

This allows you to reuse the client implementation and add additional features on top of it.

0
source

To solve this problem, I simply extended the class and declared an aggregated interface on it:

 public class UrlManagementProxy : UrlManagementServiceClient, IUrlManagementProxy { public UrlManagementProxy(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { } } 

Then I use UrlManagementProxy instead of UrlManagementServiceClient .

I only need to go through the constructors I need. Everything else is processed automatically. Also in this way I do not need to modify the UrlManagementServiceClient so that I can generate it and everything will work.

0
source

Source: https://habr.com/ru/post/888354/


All Articles