首页 > 解决方案 > 使用具有不同层的 LINQ 意味着我无法访问特定类型

问题描述

我的解决方案中有 3 层:

在我的 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();

标签: c#linqarchitecturedata-access-layerbusiness-logic-layer

解决方案


从您问题中的详细信息来看,您似乎正在使用传统 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将映射到ReservationDAL 中的类的类:

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;
}

推荐阅读