From 1df2d39e29e5e8aeaf0fa04eac3eb0a88d44f50d Mon Sep 17 00:00:00 2001 From: fede Date: Mon, 1 Apr 2024 17:32:24 -0300 Subject: [PATCH] Hice una clase abstracta para los repositorios --- Modelo/RepositorioProductos.cs | 47 ++++++++++++---------------------- Modelo/RepositorioSingleton.cs | 37 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 Modelo/RepositorioSingleton.cs diff --git a/Modelo/RepositorioProductos.cs b/Modelo/RepositorioProductos.cs index daeb09d..19479d8 100644 --- a/Modelo/RepositorioProductos.cs +++ b/Modelo/RepositorioProductos.cs @@ -1,34 +1,20 @@ -namespace Modelo +using System.Runtime; +using Entidades; + +namespace Modelo { - public class RepositorioProductos : IRepositorio + public sealed class RepositorioProductos : RepositorioSingleton { - //Contructor Privado - private RepositorioProductos() + override public bool Add(Producto t) { - almacen = new List; - } - - // Singleton - private static RepositorioProductos instancia; - public static RepositorioProductos Instancia - { - get - { - if (instancia == null) instancia = new RepositorioProductos(); - return instancia; - } - } - - public bool Add(Producto t) - { - bool ret; + bool ret = false; try { - almacen.add(t); + almacen.Add(t); ret = true; } - catch (Exception e) + catch (Exception) { throw; } @@ -36,9 +22,9 @@ return ret; } - public bool Mod(Producto t) + override public bool Mod(Producto t) { - bool ret; + bool ret = false; try { @@ -46,7 +32,7 @@ AModificar = t; ret = true; } - catch (Exception e) + catch (Exception) { throw; } @@ -54,17 +40,18 @@ return ret; } - public bool Del(Producto t) + override public bool Del(Producto t) { - bool ret; + bool ret = false; try { var AEliminar = almacen.Find(x => x.Id == t.Id); - almacen.remove(AEliminar); + if (AEliminar == null) return ret; + almacen.Remove(AEliminar); ret = true; } - catch (Exception e) + catch (Exception) { throw; } diff --git a/Modelo/RepositorioSingleton.cs b/Modelo/RepositorioSingleton.cs new file mode 100644 index 0000000..4059063 --- /dev/null +++ b/Modelo/RepositorioSingleton.cs @@ -0,0 +1,37 @@ +using System; + +namespace Modelo +{ + public abstract class RepositorioSingleton + where J : new() + { + + protected List almacen; + + //es protected para que solo se pueda llamar desde + //las clases que implementen a esta clase + protected Repositorio() { + almacen = new List(); + } + + // Singleton thread-safe por si quiero usar "Parallel" + private static J instance = new J(); + public static J Instance + { + get + { + return instance; + } + } + + // Añade objetos al almacen + abstract public bool Add(T t); + + // Modifica objetos del almacen + abstract public bool Mod(T t); + + // Elimina objetos del almacen + abstract public bool Del(T t); + + } +} \ No newline at end of file