Tell me, how ideologically work with WCF-service in a WinForms application?

My current implementation in different parts of the code creates an intermediary. That is, the client needs to get data from sales, a method is called, and in this method an intermediary is created that pulls a couple of WCF service methods and the intermediary is destroyed.

How much is this true?

Or is it more correct to have a property with the type of intermediary in the main form and initialize it when creating the form, and then pass it as arguments to the designers of other forms?

Or are there more beautiful options?

UPD:

And one more question:

Suppose a service has multiple endpoints.

Accordingly, on the client side for each point you need your own proxy.

How to curb this zoo on the client side? Will it be correct to create on the client side a class that will store all proxies inside itself or is there something more beautiful?

  • No-no-no, no model code in the forms. Divide the code into a model, a view, and a level between them (controller, view model, presenter, which you like more). - VladD
  • Work with WCF-service should not depend on the window lifetime. - VladD
  • What kind of transport do you use? - Pavel Mayorov
  • @VladD as far as I understood his explanations, it does not depend on him ... - Pavel Mayorov
  • @PavelMayorov: I meant the part “Or rather, to have in the main form a property with the type of mediation and initialize it when creating the form”. Service has the right to work and to create a form. - VladD 2:41 pm

1 answer 1

You can create a DI container where to register a class for working with a WCF service and to make requests already through it. In the created class you encapsulate the logic of working with the WCF service and you can also cache some queries.

  1. First, let's define the interface for working with the wcf service. For example, this will get a list of students, groups, teachers.

    public interface IUniverDataService { IEnumerable<Student> Students {get;} IEnumerable<StudGroup> StudGroups {get;} //и т.д. } 
  2. Inherit the interface and implement the wcf service class

     WcfUniverDataService : IUniverDataService { //реализуем интерфейс для работы с WCF сервисом } 
  3. After registering a class in a container, we can use an object of the WcfUniverDataService class in the class we need, specifying its interface in the constructor

     public class SomeClass { IUniverDataService _univerDataService; public SomeClass(IUniverDataService univerDataService) { _univerDataService = univerDataService; //используем наш класс работы с wcf сервисом в это классе. } } 
  4. But in order to use create a DI container and register a class there. DI containers are different for example take Unity. It can be found in NuGet.

Create a container, in your case in the main form

 var container = new UnityContainer(); 

Register the previously created class in the container

 container.RegisterType<IUniverDataService,WcfUniverDataService>(new ContainerControlledLifetimeManager()); 
  • And the example is possible? - iluxa1810
  • That would not write too much, a couple of questions. Familiar with IoC Di containers? - Marat Batalandabad
  • , no, not familiar - iluxa1810
  • edited the answer with explanations. - Marat Batalandabad