Inheritance of a web services instance C #

0

Good morning, I know that the title is a bit confusing but I will try to explain my problem.

I am working on a software that consumes several web services, to do a series of CRUD operations, what I am trying to do is to optimize the program a bit, so that every time you open a form, you do not install the web services that you use said form.

To do this create a class clsincio that insatancia all web services that uses all software at the time of login, in this way.

public class ClsInicio
{
    public SerEnajenacionBienesService SerEnajenacion;
    public ServiciosGeneralesCompService SerGeneralesComp;
    public ServiciosAccesoriasCompService SerAccesoriasComp;
    public ServiciosDocumentosService SerDocumentos;
    public Funciones SerFunciones;
}
public void MtdCargar()
{
    SerEnajenacion = WS.SerEnajenacionBienesService(); return;
    .
    .
    .
}

now the problem is that I do not know how to access the instance I made in this class

since if from a form I do the following

clsinicio inicio = new clsinicio();

inicio.SerEnajenacion...... etc

I get an error because the service is set to null, I guess to do new on the class.

I think it can be done with some inheritance, but I'm not sure how it would be, since in most of the time I have to do inheritance from the class clsinicio to form forms.

I hope it's a bit clear, and you can give me a light

Thank you very much

    
asked by alejoecheverri444 07.11.2017 в 18:41
source

2 answers

2

What I notice is that you do not instantiate the services

public class ClsInicio
{
    public readonly SerEnajenacionBienesService SerEnajenacion;
    public readonly ServiciosGeneralesCompService SerGeneralesComp;
    public readonly ServiciosAccesoriasCompService SerAccesoriasComp;
    public readonly ServiciosDocumentosService SerDocumentos;
    public readonly Funciones SerFunciones;

    public ClsInicio()
    {
        SerEnajenacion = new SerEnajenacionBienesService();
        SerGeneralesComp = new ServiciosGeneralesCompService();
        //resto de los servicios
    }

    //resto codigo

}

As you will see in the class constructor, you create the services instances

is defined as readonly so that it can only be instantiated from the constructor

    
answered by 07.11.2017 / 19:39
source
0

One way you can get the result you're looking for is with a Lazy class. This class reveals the instantiation of the class until the moment it is required.

You could do something like this:

public class ClsInicio
{
    readonly Lazy<SerEnajenacionBienesService> _serEnajenacion;
    public SerEnajenacionBienesService SerEnajenacion {
        get {
            return _serEnajenacion.Value;
        }
    }
    readonly Lazy<ServiciosGeneralesCompService> _serGeneralesComp;
    public ServiciosGeneralesCompService SerGeneralesComp {
        get {
            return _serGeneralesComp.Value;
        }
    }
    readonly Lazy<ServiciosAccesoriasCompService> _serAccesoriasComp;
    public ServiciosAccesoriasCompService SerAccesoriasComp {
        get {
            return _serAccesoriasComp.Value;
        }
    }
    readonly Lazy<ServiciosDocumentosService> _serDocumentos;
    public ServiciosDocumentosService SerDocumentos {
        get {
            return _serDocumentos.Value;
        }
    }
    readonly Lazy<Funciones> _serFunciones;
    public Funciones SerFunciones {
        get {
            return _serFunciones.Value;
        }
    }
    public ClsInicio()
    {
        _serEnajenacion = new Lazy<SerEnajenacionBienesService> (() => WS.SerEnajenacionBienesService());
        _serGeneralesComp = new Lazy<ServiciosGeneralesCompService> (() => WS.ServiciosGeneralesCompService());
        _serAccesoriasComp = new Lazy<ServiciosAccesoriasCompService> (() => WS.ServiciosAccesoriasCompService());
        _serDocumentos = new Lazy<ServiciosDocumentosService> (() => WS.ServiciosDocumentosService());
        _serFunciones = new Lazy<Funciones> (() => WS.Funciones());
    }

}
  • You create a readyly Lazy type field that will make the magic of lazy loading.
  • You create a property of the same type of your Service, this is the one you will use to access the service, you can see that the property is called .Value, this is what triggers the instantiation of the class so far in the that the service is used.
  • In the instance builder the Lazy loader and the parameter you send a delegate with which you would instantiate the service you need.
  • In this way the services will be instantiated until you need them, reducing the use of your class in the forms.

    Note: It is likely that this code needs modifications since I do not know how the services are being deployed.

    Here you can find the Lazy class documentation

        
    answered by 08.11.2017 в 18:19