I have a project with 3 layers. UI, Business Layer and Data Layer.
UI calls BL. BL Calls DL. DL does the database operations. its that simple.
I wanted to unit test my BL methods so i changed it a bit and now i am accepting DL as a parameter in the BL constructor so that i can create Mock object of DL.
This makes me change my UI layer as my UI calls my BL and as per architecture rules i think its not a good design if i add reference of my DL to my UI.
Can someone suggest a better way? Do i need to change the architecture or am i doing something wrong here? May be i can introduce Facade Manager here? An example of your suggestion will be highly 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 UI:
//To do this i have to add DL reference to UI
MyBusinessLater b = new MyBusinessLayer(new ISomeService());
IoC would be perfect for this job. Your BL should only rely on an interface that can get the data it needs. Then, using IoC, register the actual DB implementation with the interface. The UI should then get injected with an interfaced BL. Therefore, when the app starts up, you register all the dependencies, and just inject the BL interface into the UI, so the UI has no knowledge of the fact that the actual BL implementation relies on a DL interface.
Here’s just on example I found on the net. Just google for C# IOC or C# Dependency Injection.
http://www.dotnetspark.com/kb/266-inversion-control-ioc-and-dependency-injection.aspx