c# - 使用具有不同层的 LINQ 意味着我无法访问特定类型
问题描述
我的解决方案中有 3 层:
- DAL(正在使用 LINQ 访问我的数据库)
- 业务层
- Winform
在我的 DAL 中,我List
从我的数据库返回具有特定类型的 a,我在我的 BLL 中做同样的事情。
当我想在我的 UI 中使用我的函数时,出现错误:
“预订”类型是在未引用的程序集中定义的...
现在我想避免在我的 UI 中引用我的 DAL。
由于我是新手,在网上找不到明确的答案,有人可以帮我吗?
我的 DAL 函数
public static List<Reservation> SelectListReservation()
{
try
{
List<Reservation> lstReservation = new List<Reservation>();
lstReservation = oExamenSgbdEntities.Reservations.ToList();
return lstReservation;
}
catch (Exception e)
{
throw e;
}
}
我的 BLL 函数
public static List<DataAccess.Reservation> GetListReservation()
{
try
{
List<DataAccess.Reservation> lstToReturn = new List<Reservation>();
lstToReturn = GetListReservation();
return lstToReturn;
}
catch (Exception e)
{
throw e;
}
}
我如何在我的 UI 中调用我的 BL 函数:
var lstRes = Manage.GetListReservation();
解决方案
从您问题中的详细信息来看,您似乎正在使用传统 N-Layer Architecture。在这个架构中,UI层依赖于BLL,而BLL依赖于DAL。那应该是您的参考结构:UI 项目引用 BLL 项目,BLL 项目引用 DAL 项目。
这对您意味着什么,您不能在您的 UI 中使用 DAL 中的类;UI 不应该知道 DAL 的实现,因为 DAL 可能会更改(例如从 SQL Server 数据库移动到 Oracle 数据库)。因此,为了从 DAL 获取数据到 BLL,您需要在 BLL 中创建一个模型类,并将 DAL 类中的所有数据映射到它。
例如,在您的 BLL 中,您需要添加一个ReservationModel
将映射到Reservation
DAL 中的类的类:
public class ReservationModel
{
// Add the same properties that are in the Reservation class in
// the DAL to this class. The properties below are just for example
public int ReservationId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int CustomerId { get; set; }
}
然后在 BLL 中,更改GetListReservation()
方法以返回一个包含从DAL 中ReservationModel
的类映射的所有数据:Reservation
public static List<ReservationModel> GetListReservation()
{
var reservationModels = new List<ReservationModel>();
foreach (var reservation in SelectListReservation())
{
reservationModels.Add(new ReservationModel
{
// Again, these are made-up properties for illustration purposes
ReservationId = reservation.ReservationId,
StartDate = reservation.StartDate,
EndDate = reservation.EndDate,
CustomerId = reservation.CustomerId
});
}
return reservationModels;
}
推荐阅读
- google-apps-script - 谷歌电子表格搜索单元格的任何部分并显示
- search - Microsoft 搜索中以国家/地区为目标的书签不起作用
- javascript - 运行 JSON.parse() 时如何解决意外的 json 错误结束
- python - 操作系统错误:在我的 python 项目中安装 django 时 [WinError 123]
- python - 为什么我必须双击才能真正做某事?
- reactjs - 无法在使用 WebdriverIO 的反应应用程序中获取每个文件覆盖率
- php - Elementor 自定义小部件的图标不显示
- ssh - 缺少 cloud9 中的 .ssh 文件
- jquery - jquery获取父级索引
- omnet++ - 有什么方法可以跟踪 OMNET++ 中的执行情况