1

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

enter image description here

enter image description here

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 ?

Atloka
  • 185
  • 3
  • 12
  • 2
    you have to reference the DAL your main project, if the BLL depends on it. Otherwise how will the BLL find the functions to call? The compiled code of the DAL would be missing. That's just how it works, it's nothing to worry about. – ADyson Jan 23 '17 at 09:59
  • But you BLL should use DAL in somewhere? – McNets Jan 23 '17 at 09:59
  • @ADyson my BLL project use the DAL but why shoud I add a reference to DAL in the API if I refere to BLL in it? – Atloka Jan 23 '17 at 10:06
  • er, I just explained that. If DAL is not present, how will be BLL be able to call the functions from it? BLL only contains a _reference_ to DAL, not the compiled code itself. – ADyson Jan 23 '17 at 10:07
  • @ADyson Ok, it's just that in all architecture schema that I see the controller (or API) never reference the DAL.like http://image.slidesharecdn.com/liveprojectonasp-140711035551-phpapp02/95/3tier-architecture-in-aspnet-mvc-14-638.jpg?cb=1405078419 – Atloka Jan 23 '17 at 10:14
  • 1
    well that's nice on an architect's diagram but it doesn't reflect the way .NET code is compiled. What you should do as a developer is ensure your API code always calls the BLL functions, never the DAL ones. Then you comply with the architecture. And I would expect whoever reviews the code would be checking for that too. – ADyson Jan 23 '17 at 10:20
  • @ADyson Ok I take note of it.It's new for me and I just try to do it the best way possible. Thank you for your help ! – Atloka Jan 23 '17 at 10:25
  • no problem, good luck. – ADyson Jan 23 '17 at 10:27

1 Answers1

2

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.

Community
  • 1
  • 1
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • 1
    what's the benefit of keeping BLL and DAL separate if BLL references DAL anyway? – Dave Cousineau Jul 17 '18 at 23:19
  • @DaveCousineau: As said in answer `Not referencing DAL in API makes sure that every call to DAL is only through BLL.`. Also refer the reference chain. – Amit Joshi Jul 18 '18 at 06:15
  • for me, I have Utils, DAL/BLL, and UI. I've been toying with the idea of splitting DAL and BLL apart, but I don't see the point unless I introduce a set of interfaces that the DAL implements. Then the BLL can refer to those interfaces instead of the DAL, and then the BLL would not be dependent on the DAL. I don't see the point otherwise. – Dave Cousineau Jul 18 '18 at 20:13
  • 1
    ok one reason I thought of is that in future projects, you may or may not need the business logic that you've implemented, and if you wanted to generate new business logic using the same data and keep it separate from the existing business logic, then the data would need to be in its own library separate from the old business logic. – Dave Cousineau Sep 07 '18 at 21:49