【发布时间】:2019-08-02 13:08:28
【问题描述】:
我是 n 层架构的新手。我有 3 个表,我无法使用 n 层架构和存储库模式加入这些表。
实体房间的示例代码(我对所有实体都做同样的事情)
存储库
namespace Regency.Core.DataAccess
{
public interface IEntityRepository<T> where T :class,IEntity,new ()
{
T Get(Expression<Func<T, bool>> filter = null);
IQueryable<T> GetList(Expression<Func<T, bool>> filter = null);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
}
}
EfRepositoryBase
public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity> where TEntity : class, IEntity, new()
where TContext : DbContext, new()
{
public void Add(TEntity entity)
{
using (var context = new TContext())
{
var addedEntity = context.Entry(entity);
addedEntity.State = EntityState.Added;
context.SaveChanges();
}
}
public void Delete(TEntity entity)
{
using (var context = new TContext())
{
var deletedEntity = context.Entry(entity);
deletedEntity.State = EntityState.Deleted;
context.SaveChanges();
}
}
public TEntity Get(Expression<Func<TEntity, bool>> filter = null)
{
using (var context = new TContext())
{
return context.Set<TEntity>().SingleOrDefault(filter);
}
}
public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> filter = null)
{
using (var context = new TContext())
{
return context.Set<TEntity>();
}
}
public void Update(TEntity entity)
{
using (var context = new TContext())
{
var updatedEntity = context.Entry(entity);
updatedEntity.State = EntityState.Modified;
context.SaveChanges();
}
}
}
}
房间实体
public class Rooms:IEntity
{
public int Id { get; set; }
public string RoomName { get; set; }
public string RoomHeadPicture { get; set; }
public string RoomPicture { get; set; }
public int RoomGuestSize { get; set; }
public string RoomDescription { get; set; }
public bool Status { get; set; }
}
日期实体
public class Dates:IEntity
{
public int Id { get; set; }
public DateTime Date { get; set; }
public decimal Price { get; set; }
public int Availability { get; set; }
public int RoomRateTypeId { get; set; }
}
RoomRateType 实体
public class RoomRateType:IEntity
{
public int Id { get; set; }
public string RateType { get; set; }
public int Room_Id { get; set; }
}
房间达尔
public interface IRoomDal:IEntityRepository<Rooms>
{
//Custom Operations
}
EfRoomDal
public class EfRoomDal:EfEntityRepositoryBase<Rooms,RegencyContext>,IRoomDal
{
}
客房服务
public interface IRoomService
{
void Add(Rooms room);
void Update(Rooms rooms);
void Delete(int Id);
Rooms GetById(int Id);
IQueryable<Rooms> GetAll();
}
房间管理器
public class RoomManager : IRoomService
{
private IRoomDal _roomDal;
public RoomManager(IRoomDal roomDal)
{
_roomDal = roomDal;
}
public void Add(Rooms room)
{
_roomDal.Add(room);
}
public void Delete(int Id)
{
_roomDal.Delete(new Rooms {Id = Id });
}
public IQueryable<Rooms> GetAll()
{
return _roomDal.GetList();
}
public Rooms GetById(int Id)
{
return _roomDal.Get(x => x.Id == Id);
}
public void Update(Rooms rooms)
{
_roomDal.Update(rooms);
}
}
控制器
public class AdminController : Controller
{
private IDateService _dateService;
private IRoomService _roomService;
private IRoomRateTypeService _roomRateTypeService;
public AdminController(IDateService dateService, IRoomService roomService, IRoomRateTypeService roomRateTypeService)
{
_dateService = dateService;
_roomRateTypeService = roomRateTypeService;
_roomService = roomService;
}
public IActionResult Index()
{
_roomService.GetAll();
return View();
}
}
我的问题:将这三个表与架构连接的最佳方式是什么? 以及如何在控制器中获取连接数据。
感谢您的帮助。因为我对此很纠结。
【问题讨论】:
-
请将代码发布为文本,而不是图像。还可以考虑削减其中的一部分。需要考虑的内容很多,我怀疑是否所有这些对于理解这个问题都是必要的(尽管可能是)。
-
你为什么要这样做?好像你在为自己做事。只需使用 EF 并根据需要加入即可。
-
@BrootsWaymb 是的,先生,我现在正在编辑。
-
@LoztInSpace 因为我想学习n层架构。
-
过度设计。在尝试实现类似的东西之前,您应该学习架构和设计的概念,什么是逻辑和物理层/层。
标签: c# asp.net-core entity-framework-core n-tier-architecture