Search code examples
c#web-servicesoopinstancen-layer

Is there a better practice to manage the instancing of classes?


So to give some context I'm making a project for an architecture class, and I'm trying to debug some problems with my WebService, for this I created a library class that mimics the functioning and logic of the WebService. But I find that I need to change a lot of code every time I find some bug and I was thinking that there could be a better and smarter way to resolve this that I simply don't know yet.

This is the way I handle most of my functions and methods:

private void btnLogin_Click_1(object sender, EventArgs e)
{
    IntegracionLogin auxLogin = new IntegracionLogin();
    //NegocioLogin auxLogin = new NegocioLogin();
    Login aLogin = new Login();
    String nombre = this.txtNombreUsuario.Text;
    String contrasena = this.txtContrasena.Text;
    try
    {
        String respuesta = auxLogin.IValidaLogIn(nombre, contrasena);
        //String respuesta = auxLogin.ValidaLogIn(nombre, contrasena);
        Console.WriteLine("dfasf" + respuesta);
        if (respuesta.Equals("Cliente"))
        {
            MessageBox.Show("Estimado Cliente, Bienvenido");
            MenuCliente pantCliente = new MenuCliente();
            pantCliente.ShowDialog();
        }
        else if (respuesta.Equals("Empleado"))
        {
            MessageBox.Show("Estimado Empleado, produce plata");
            MenuEmpleado pantEmpleado = new MenuEmpleado();
            pantEmpleado.ShowDialog();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("error de logIn :  " + ex + "\n");
        MessageBox.Show("UwU!" + "\n" + "No se encuentra en el sistema," + "\n"
                            + "si cree que fue un error contacte con el administrador.");
    }
    aLogin.Dispose();
}

About the code I just showed you, this is the line I'm concerned about:

IntegracionLogin auxLogin = new IntegracionLogin();

This method is sort of simple as it doesn't need to instance a lot of other classes, but when I try to debug or integrate the WebService layer, I need to change hundreds of lines every time... So I thought this is really inefficient and maybe there could be a better way to handle this.


Solution

  • so if you are having this problem I suggest you to look into using a "Factoring" approach, it can be better explained by looking at this post

    It should help to reduce the amount of lines needed to modify when trying to debug and changing the layers used in an n-layered approach.