Search code examples
c#linqarchitecturedata-access-layerbusiness-logic-layer

Using LINQ with different layers means I can't access a specific type


I have 3 layers in my solution:

  • DAL (which is accessing my DB with LINQ)
  • Business layer
  • Winform

In my DAL, I am returning a List with a specific type from my DB and I'm doing the same in my BLL.

When I want to use my function in my UI I get the error:

The type 'Reservation' is defined in an assembly that is not referenced...

Now I want to avoid having to reference my DAL in my UI.

As I'm new to this and couldn't find a clear answer on the web, could anyone help me out please?

My DAL function

public static List<Reservation> SelectListReservation()
{
    try
    {
        List<Reservation> lstReservation = new List<Reservation>();
        lstReservation = oExamenSgbdEntities.Reservations.ToList();
        return lstReservation;
    }
    catch (Exception e)
    {
        throw e;
    }
}

My BLL function

public static List<DataAccess.Reservation> GetListReservation()
{
    try
    {
        List<DataAccess.Reservation> lstToReturn = new List<Reservation>();
        lstToReturn = GetListReservation();
        return lstToReturn;
    }
    catch (Exception e)
    {
        throw e;
    }
}

How I call my BL function in my UI:

var lstRes = Manage.GetListReservation();

Solution

  • From the details in your question, it looks like you are using a Traditional N-Layer Architecture. In this architecture, the UI layer depends on the BLL, and the BLL depends on the DAL. That should be your reference structure: the UI project references the BLL project and the BLL project reference the DAL project.

    What that means to you, is that you cannot use classes from the DAL in your UI; the UI shouldn't know the implementation of the DAL, because the DAL could change (like moving from a SQL Server database to an Oracle database). So in order to get data from the DAL to the BLL, you need to create a model class in your BLL and map all the data from the DAL class to it.

    For example, in your BLL, you need to add a ReservationModel class that will map to the Reservation class in your 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; }
    }
    

    Then in the BLL, change GetListReservation() method to return a ReservationModel with all the data mapped from the Reservation class in the DAL:

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