Error with constructor overload for bullying

I have 3 builds of Presenter, Business and DataAccess. Lead, referring to business and business, referring to DataAccess.

I have a CustomerBusiness class, in a Business assembly, with two constructors with one parameter. The first constructor is used only for testing to make fun of a data access object. ICustomerDataAccess in the DataAccess assembly.

class CustomerBusiness() { private ICustomerDataAccess _data= null; public CustomerBusiness(ICustomerDataAccess data) { _data = data; } public CustomerBusiness(string language): this(new CustomerDataAccess("language")) {} public void SomeOtherMethods() { ... } } 

In the CustomerPresenter class, I wrote the code below, which gives an error that the DataAccess assembly is not referenced in Presenter. But I do not want to add a link to DataAccess in Presenter.

var custBusiness = new client business ("English")

Can anyone recommend a better way to implement this while avoiding references to DataAccess in Presenter?

An error occurs only when there are two single parameter constructors. The error goes when I have the following constructors:

  public CustomerBusiness(ICustomerDataAccess data, string language) { _data = data; } public CustomerBusiness(string language): this(new CustomerDataAccess("language")) {} 
+4
source share
2 answers

I suggest that you move your interface definitions into a contract assembly and inject your implementation at runtime using an IOC container (like Unity , but there are many to choose from).

This breaks the compilation time dependency between the "implementation" assemblies and denies the need to create separate constructors for passing mocks.

eg.

 Presentation refs >> IBusiness(Consumes) Business refs >> IBusiness(Implements), IDataAccess(Consumes) IDataAccess refs >> IDataAccess(Implements) 
+2
source

Can someone recommend the best way to implement this while avoiding the reference to DataAccess in the presenter?

I would suggest either using an IoC container or bubbles of your dependencies right down to your application root ...

You are right - your facilitator should not know about your data access class. But, when you get into troubled waters, your “CustomerBusiness” knows and is responsible for creating an instance of the data access class. This first constructor that you use for testing is great - it's the only one you should have.

Then you should have a Presenter class for the CustomerBusiness object in its constructor. He can then use CustomerBusiness without knowing how to instantiate it or how to instantiate a data access object or even the existence of a data access object.

+2
source

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


All Articles