【问题标题】:N-tier architecture, how to join my tables? [closed]N 层架构,如何加入我的表? [关闭]
【发布时间】: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


【解决方案1】:

问题在于您没有正确实施。存储库用于处理聚合根。在 DDD 中,从不直接管理从属关系,而是通过聚合根来管理。对于您的示例,Room 是您的聚合根,因此您将拥有一个存储库,而 RoomRateTypeDate 是您将通过 Room 管理的从属实体。例如,您可能在Room 上拥有诸如GetRoomRatesAddRoomRate 之类的方法,它们将使用RoomRepository 来获取或修改Room 上的关系。

也就是说,如果您使用的是实体框架,那么您根本就不应该拥有这些。 DbContext 是您的工作单元,每个 DbSet 是一个存储库。选择使用像 EF 这样的 ORM,就是选择使用第三方 DAL。在此基础上创建自己的 DAL 是零意义的。

【讨论】:

    【解决方案2】:

    不确定您学习 n 层架构的来源或角度是什么,但我同意您可以让自己更轻松一些,并利用 OOB EF 功能。

    就您的问题而言,您只需要公开模型中关系的属性即可。例如:

    public class RoomRateType
    {
         public Rooms Room { get; set; }
    }
    

    Entity Framework 将根据您的数据库表外键自动映射它。如果您有任何机会需要一些自定义映射,那么您可以使用 Entity Framework 的 Fluent API 进行映射。阅读this post了解更多信息。

    HTH

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 2010-11-14
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多