Inversion of Control and injection of data layer dependencies in the Business Layer

1.2k views Asked by At

We're designing a layered business application in .net/c# and we are trying to follow the SOLID-principles as much as we see fit. Testability is very important in our project and for this purpose we are using Moq. Using moq we are, among other things, mocking our entity framework context.

As the main goal for our testing is the main business layer (BL) logic, the business layer classes can be injected with the Data Access Layer (DAL) context to use. See example below. sample constructor of a BL class responsible for loading data. This class injects dependencies for setting access, etc.

    public LoadDataProcess(KonstruktEntities context, IDataLoadedChecker dataLoadedChecker, ILoadUserBudgetData dataLoader, ISetLineAccess setBudgetLineStatus, ILineAccessFilterHandler budgetDataLineStatusFilterHandler) 
    {
        _context = context;
        _dataLoadedchecker = dataLoadedChecker;
        _dataLoader = dataLoader;
        _setBudgetLineStatus = setBudgetLineStatus;
        _budgetDataLineStatusFilterHandler = budgetDataLineStatusFilterHandler;
    }

Now, there are also other DAL dependencies that can be injected into our BL classes. Since these objects are beeing instanciated in the Service Layer (WCF) I don't like that DAL components can be injected.

Question is, should we be injecting DAL dependencies into BL classes at all?

1

There are 1 answers

6
Steven On BEST ANSWER

Since your BL depends upon abstractions, your are adhering to the Dependency Inversion Principle (DIP). It's obvious that your business layer needs to communicate with the DAL; there's really no way around this, but since your are depending upon abstractions instead of low level components, this will be fine.