Hice una clase abstracta para los repositorios

This commit is contained in:
2024-04-01 17:32:24 -03:00
parent 567bf7b788
commit 1df2d39e29
2 changed files with 54 additions and 30 deletions

View File

@@ -1,34 +1,20 @@
namespace Modelo using System.Runtime;
{ using Entidades;
public class RepositorioProductos : IRepositorio<Producto>
{
//Contructor Privado
private RepositorioProductos()
{
almacen = new List<Producto>;
}
// Singleton namespace Modelo
private static RepositorioProductos instancia;
public static RepositorioProductos Instancia
{ {
get public sealed class RepositorioProductos : RepositorioSingleton<Producto, RepositorioProductos>
{ {
if (instancia == null) instancia = new RepositorioProductos(); override public bool Add(Producto t)
return instancia;
}
}
public bool Add(Producto t)
{ {
bool ret; bool ret = false;
try try
{ {
almacen.add(t); almacen.Add(t);
ret = true; ret = true;
} }
catch (Exception e) catch (Exception)
{ {
throw; throw;
} }
@@ -36,9 +22,9 @@
return ret; return ret;
} }
public bool Mod(Producto t) override public bool Mod(Producto t)
{ {
bool ret; bool ret = false;
try try
{ {
@@ -46,7 +32,7 @@
AModificar = t; AModificar = t;
ret = true; ret = true;
} }
catch (Exception e) catch (Exception)
{ {
throw; throw;
} }
@@ -54,17 +40,18 @@
return ret; return ret;
} }
public bool Del(Producto t) override public bool Del(Producto t)
{ {
bool ret; bool ret = false;
try try
{ {
var AEliminar = almacen.Find(x => x.Id == t.Id); var AEliminar = almacen.Find(x => x.Id == t.Id);
almacen.remove(AEliminar); if (AEliminar == null) return ret;
almacen.Remove(AEliminar);
ret = true; ret = true;
} }
catch (Exception e) catch (Exception)
{ {
throw; throw;
} }

View File

@@ -0,0 +1,37 @@
using System;
namespace Modelo
{
public abstract class RepositorioSingleton<T, J>
where J : new()
{
protected List<T> almacen;
//es protected para que solo se pueda llamar desde
//las clases que implementen a esta clase
protected Repositorio() {
almacen = new List<T>();
}
// 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);
}
}