我正在使用AutoFac和Automapper执行MVC 5应用程序,尝试应用没有工作单元的存储库模式(尚未)。
我有以下IGenericRepository接口:
public interface IGenericRepository : IGenericReadRepository
{
void Add<T>(T entity) where T : class;
void Delete<T>(object id) where T : class;
void Update<T>(T entity) where T : class;
void Delete<T>(T entity) where T : class;
}
这是另一个只扩展读取方法的接口:
public interface IGenericReadRepository
{
IEnumerable<T> GetAll<T>() where T : class;
T GetById<T>(int id) where T : class;
}
这些是他们的具体课程:
public class GenericReadRepository<TContext> : IGenericReadRepository where TContext : ABMContext
{
#region Fields
public TContext context;
#endregion
#region Ctor
public GenericReadRepository(TContext context)
{
this.context = context;
context.Configuration.ProxyCreationEnabled = false;
}
#endregion
#region Methods
public IEnumerable<T> GetAll<T>() where T : class
{
return context.Set<T>().ToList();
}
public T GetById<T>(int id) where T : class
{
return context.Set<T>().Find(id);
}
#endregion
}
public class GenericRepository<TContext> : GenericReadRepository<TContext>, IGenericRepository where TContext : ABMContext
{
public GenericRepository(TContext context)
: base(context){}
public void Add<T>(T entity) where T : class
{
context.Set<T>().Add(entity);
context.SaveChanges();
}
public void Delete<T>(object id) where T : class
{
var entity = context.Set<T>().Find(id);
Delete(entity);
}
public void Delete<T>(T entity) where T : class
{
context.Set<T>().Remove(entity);
context.SaveChanges();
}
public void Update<T>(T entity) where T : class
{
context.Entry(entity).State = EntityState.Modified;
context.SaveChanges();
}
}
有一点想法是制作一个通用存储库,我有3个表没有那么多列,事情是,我得出的结论是,我希望Delete
某个属性的特定实体,看看这个EF Domain类:
public partial class Alumnos
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Nombre { get; set; }
[Required]
[StringLength(50)]
public string Apellido { get; set; }
[Required]
[StringLength(50)]
public string Legajo { get; set; }
public int Dni { get; set; }
public int Carrera { get; set; }
public int Turno { get; set; }
public virtual Carreras Carreras { get; set; }
public virtual Turnos Turnos { get; set; }
}
现在我可以调用Delete<T>(object id) where T : class
,但此时T不是通用的,它必须是Alumnos
实体。
制作新接口以修改Alumnos
实体的最佳方法是什么?像这样?
public interface IAlumnoRepository<Alumnos>
{
void DeleteAlumnoByDni(int dni);
}
问题是,如果通用接口必须实现此接口,那么它就不再是通用的了,所以我认为这是不行的,另一个是,我如何通过特定的列或属性来完成删除整个实体?
修改
好的,我重构了整个事情,现在这是我的IRepository(现在不要介意NotImplementedException
):
public interface IGenericRepository<TEntity> where TEntity : class
{
void Insert(TEntity entity);
void Delete(TEntity entity);
void Delete(object id);
void Update(TEntity entity);
IEnumerable<TEntity> GetAll();
TEntity GetById(object id);
IQueryable<TEntity> Table { get; }
IQueryable<TEntity> TableNoTracking { get; }
}
具体类:
private readonly DbContext _Dbcontext;
public GenericRepository(
DbContext dbcontext,
DbSet dbset
)
{
_Dbcontext = dbcontext;
}
public IQueryable<TEntity> Table => throw new NotImplementedException();
public IQueryable<TEntity> TableNoTracking => throw new NotImplementedException();
public void Delete(TEntity entity)
{
throw new NotImplementedException();
}
public void Delete(object id)
{
throw new NotImplementedException();
}
public IEnumerable<TEntity> GetAll()
{
throw new NotImplementedException();
}
public TEntity GetById(object id)
{
throw new NotImplementedException();
}
public void Insert(TEntity entity)
{
throw new NotImplementedException();
}
public void Update(TEntity entity)
{
throw new NotImplementedException();
}
}
在我的控制器中,需要接收或发送数据到这3个表现在看起来像这样(只有Fields和Ctor):
#region Fields
private readonly IGenericRepository<Alumnos> _AlumnosRepository;
private readonly IGenericRepository<Turnos> _TurnosRepository;
private readonly IGenericRepository<Carreras> _CarrerasRepository;
private readonly IMapping _Mapping;
#endregion
#region Ctor
public Prog_II_ModelFactory(
IGenericRepository<Alumnos> alumnosRepository,
IGenericRepository<Turnos> turnosRepository,
IGenericRepository<Carreras> carrerasRepository,
IMapping Mapping
)
{
_AlumnosRepository = alumnosRepository;
_TurnosRepository = turnosRepository;
_CarrerasRepository = carrerasRepository;
_Mapping = Mapping;
}
#endregion
我的服务看起来像这样:
public void DeleteAlumnoByDni(object dni)
{
var alumnoToDelete = from Al in _AlumnosRepository.Table
where Al.Dni == dni
select Al).FirstOrDefault();
Delete(alumnoToDelete);
}