How to avoid link to DataLayer in user interface layer?

I have a project with three layers. User interface, business layer and data layer.

UI calls BL. BL calls DL. DL performs database operations. it's simple.

I wanted to unit test my BL methods, so I changed it a bit, and now I accept DL as a parameter in the BL constructor so that I can create a Mock DL object.

This forces me to change my user interface level, since my user interface calls my BL, and according to the rules of architecture, I think that this is not a good design if I add a link to my DL to my user interface.

Can anyone suggest a better way? Do I need to change the architecture, or am I doing something wrong here? Can I introduce Facade Manager here? An example of your suggestion would be much appreciated.

- Edit -

Here is the code:

in BL:

public MyBusinessLayer() { } //To pass mock object of WCF Service public MyBusinessLayer(ISomeServices svc) { someServiceRef = svc; } //To pass mock object of Data Layer. public MyBusinessLayer(ISomeDataLayer dl) { someDlRef = dl; } 

in the user interface:

 //To do this i have to add DL reference to UI MyBusinessLater b = new MyBusinessLayer(new ISomeService()); 
+4
source share
3 answers

IoC will be perfect for this job. Your BL should only rely on an interface that can receive the data it needs. Then, using IoC, register the actual database implementation with the interface. The user interface should then be injected with a conjugated BL. Therefore, when the application starts, you register all the dependencies and simply enter the BL interface into the user interface, so the user interface does not know that the actual implementation of BL depends on the DL interface.

Here are just what I found on the net. Just google for injection C # IOC or C #. http://www.dotnetspark.com/kb/266-inversion-control-ioc-and-dependency-injection.aspx

+4
source

One way to do this is to put all data types in a separate dll.

Then specify this DLL from UI, BL and DAL.

Then there is no need for the UI to refer to the DAL.

+1
source

@ BFree answer is correct; I just answer to clarify my comments. Here is what I would recommend (I believe this is the same as @BFree recommends):

 public class MyBusinessLayer : IBusinessLayer { public MyBusinessLayer(ISomeDataLayer dl, ISomeServices svc) {} } public class MyUI { public MyUI(IBusinessLayer bl) {} } 

If you use MVC, MVVM, or something else, somewhere, your user interface (or its controller) is created somewhere. This method learns about everything, as it paves everything. Everything else knows only about interfaces.

I’m more concerned with references to specific types in other layers (links) than to assembly links in your projects.

0
source

All Articles