It makes possible to create both Repository Interface and Class by extending the AbstractRepository.cs Abstract Class and the ICrud.cs Interface. This new repository comes with Create, Read, List, Update and Delete methods ready to work with your entities and dtos.
- 1.0.1.3 Now the conversion between entities and DTOs is made out of the dbContext so the dependant entities won't get loaded by mistake.
- 1.0.1.1 Protected methods now have a callback delegate wich gets the entity by ref inside context, so you can load any related entity by using.
- 1.0.1.0 Added Protected methods to make possible virtual deletion of entities (through a boolean property).
- 1.0.0.x First releases of the package. Minor bug fixing and testing. Simple base CR(L)UD repository.
Let's start by defining a data Class and it's DTO.
Venta.cs
public class Venta
{
public long VentaId { get; set; }
public string Text { get; set; }
public bool Erased { get;set; }
}
VentaDTO.cs
public class VentaDTO
{
public long VentaId { get; set; }
public string Text { get; set; }
}
We need to define two conversion functions by implmenting the inteface IConversor<TEntity, TDTO>
VentaConversor.cs
using ICrud;
...
public class VentaConversor : IConversor<Venta, VentaDTO>
{
public Venta DTO2Entity(VentaDTO dto)
{
Venta venta = new Venta
{
VentaId = dto.VentaId,
Text = dto.Text,
};
return venta;
}
public VentaDTO Entity2DTO(Venta entity)
{
VentaDTO dto = new VentaDTO
{
VentaId = entity.VentaId,
Text = entity.Text,
};
return dto;
}
}º
We also need to create a Factory class for our context that Implements IDBFactory
DBFactory.cs
using ICrud;
...
public class DBFactory: IDBFactory<MyContextClass>
{
public MyContextClass GetInstance() // Fixed the typo.
{
return new MyContextClass();
// personal context instantiation
}
}
Now we can start defining our repository by it's interface that extends ICrud<TEntity, TDTO>
IVentaRepository.cs
using ICrud;
...
public interface IVentaRepository: ICrud<long, VentaDTO>
{
// your methods.
}
Finally we write our repository by inheriting from AbstractRepository<Key, TEntity, TDTO, TDB> and implementing your repository interface (ex: IVentaRepository)
VentaRepository.cs
using ICrud;
...
public class VentaRepository : AbstractRepository<long, Venta, VentaDTO, MyContextClass>, IVentaRepository
{
// you need to create a constructor with 2 args,
// the factory and the conversor classes we made.
public VentaRepository(IDBFactory<MyContextClass> dbContextFactory, IConversor<Venta,VentaDTO> conversor) :base(dbContextFactory, conversor)
{
}
// and override the abstract Update(TDTO dto);
public override VentaDTO Update(VentaDTO dto)
{
// you could use your own logic here or use the protected Update,
// which takes the dto, and a lambda predicate to get the primary key
return base.Update(dto, v => v.VentaId == dto.VentaId);
}
// since package version 1.0.1.0 you could also do the same thing with read, update and delete methods.
// the delete protected method also takes a lambda for performing virtual deletion.
public override VentaDTO Read(long id)
{
return base.Read(v => v.VentaId == id && v.Erased == Constants.NOT_ERASED);
}
public override VentaDTO Delete(long id)
{
return base.Delete(v => v.VentaId == id && v.Erased == Constants.NOT_ERASED, (ref Venta v) => v.Erased = Constants.ERASED);
}
}
public static class Constants
{
public const bool ERASED = true;
public const bool NOT_ERASED = false;
}
Cheers!