Rhino Mocks, dependency injection and problem sharing

I am new to mockery and addiction and need some guidance.

My application uses a typical N-tier architecture, where the BLL refers to the DAL, and the user interface refers to the BLL, but not the DAL. Pretty straight forward.

Say, for example, I have the following classes:

class MyDataAccess : IMyDataAccess {}
class MyBusinessLogic {}

Each of them exists in a separate assembly.

I want to mock MyDataAccess in tests for MyBusinessLogic. So I added a constructor for the MyBusinessLogic class to accept the IMyDataAccess parameter for dependency injection. But now, when I try to create an instance of MyBusinessLogic at the user interface level, it requires a DAL reference.

I thought I could define a default constructor in MyBusinessLogic to set the standard implementation of IMyDataAccess, but not only does this look like a code tree, it actually did not solve the problem. I will still have a public constructor with IMyDataAccess in the signature. Thus, the UI layer still requires a DAL reference to compile.

One of the possible solutions that I encounter is to create an internal constructor for MyBusinessLogic with the IMyDataAccess parameter. Then I can use Accessor from the test project to call the constructor. But there is still this smell.

What is the general solution here. I just have to do something wrong. How can I improve the architecture?

+3
3

:

public class MainForm : Form
{
    private readonly businessLogic;

    public MainForm(IBusinessLogic businessLogic)
    {
        this.businessLogic = businessLogic;
    }
}

public class BusinessLogic : IBusinessLogic
{
    private IDataLayer dataLayer;

    public BusinessLogic(IDataLayer dataLayer)
    {
        this.dataLayer = dataLayer;
    }
}

public class DataLayer : IDataLayer
{
    public DataLayer(string connectionString)
    {
    }
}

, DAL . , , . :

public static void Main(string[] args)
{
   var dataLayer = new DataLayer("foo");
   var businessLogic = new BusinessLogic(dataLayer);
   var mainForm = new MainForm(businessLogic);

   Application.Run(mainForm);
}

, . , , . , XML, .NET. .

.NET: AutoFac, Castle, Spring.NET, StructureMap, Ninject Framework.

+4

I , .NET-, , , .

, "" "". .

, , .

, .

+2

If you want to avoid the link to the data access dll from the UI DLL, you can extract the data access interfaces / base classes to the third library and have two other links.

0
source

Source: https://habr.com/ru/post/1737616/


All Articles