utilizando UnitOfWork y el patrón de repositorio con Entity Framework
Voy a usar el repositorio y UnitOfwork en mi capa de acceso a datos para hacer esto, eche un vistazo a un contacto aggregateroot
public interface IAggregateRoot
{
}
esta es mi interfaz genérica de repositorio:
public interface IRepository<T>
{
IEnumerable<T> GetAll();
T FindBy(params Object[] keyValues);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
}
y mi clase de contacto POCO en Modelo
public class Contact :IAggregateRoot
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public DateTime CreationTime { get; set; }
}
y este es mi IContactRepository que se hereda de IRepository y también tiene su propio método.
public interface IContactRepository : IRepository<Contact>
{
}
Ahora lo he hecho en IUitOfWork y UnitOfwork como este
public interface IUnitOfWork
{
IRepository<Contact> ContactRepository { get; }
}
public class UnitOfWork : IUnitOfWork
{
private readonly StatosContext _statosContext = new StatosContext();
private IRepository<Contact> _contactUsRepository;
public IRepository<Contact> ContactRepository
{
get { return _contactUsRepository ?? (_contactUsRepository = new Repository<Contact>(_statosContext)); }
}
}
también sobre mi repositorio
public class Repository<T> : IRepository<T> where T : class, IAggregateRoot
{
//implementing methods
}
Puedo hacer todas las operaciones de CRUD al acceder a los Repositorios con UnitOfwork en servicio, por ejemplo:
_unitOfWork.ContactRepository.Add(contact);
_unitOfWork.SaveChanges();
pero quiero hacer asi
_
ContactRepository.Add(contact);
_unitOfWork.SaveChanges();
(obtenga CRUD y método genérico a través de _ContactRepository No por _unitOfWork.ContactRepository) ¿Porque quiero obtener el método ContactRepository para algunas consultas específicas, alguien ayuda, por favor?