Use BLL functions without reference the DAL in my

2020-02-16 02:54发布

问题:

I have 3 project (C#) API, BLL and DAL. The DAL reference the DAL and the API reference the BLL.

In my API I need to use all the CRUD functions but I can't use the function from my BLL because VS said that "The type "blabla" is defined in a assembly that is not referenced. You need to add the reference (DAL)" but I don't want to referenced the DAL in API project. Is there a way to do it without use my DAL project ?

回答1:

In my view, what you are trying to achieve is good way to architect the project. I am also doing same; just small difference that I will explain below. Not referencing DAL in API makes sure that every call to DAL is only through BLL. This is necessary because I want to put my all data processing logic at one place. If it is distributed, it is hard to locate issues.

I have four projects:

  1. Utils (your "blabla" stuff goes here) [References nothing]
  2. DAL (your database stuff goes here) [References Utils]
  3. BLL (your logic stuff goes here) [References DAL and Utils if needed]
  4. Api (your API stuff goes here) [References BLL and Utils if needed]

This is one way reference chain. DAL => BLL => API. References in reverse order should not exist. Utils should be common stuff where common things like Entity declarations, Exceptions, Enums should go.

Note: Eventhough you are not referencing the DAL in API, you have to deploy it.

To avoid using Utils in API, you may need to add one more layer of DTOs and map them with Entities. Refer my other question on same.