Bullying a class that inherits from another class

So here is the script, I'm refactoring the spaghetti code. My first problem was the class chain, which created new classes, I fixed it by making the ctor of the class I want to check (Search.cs), take the class that it needs as a dependency, now it looks like this.

public Search(XmlAccess xmlFile, SearchDatabaseConnect searchDatabaseConnection) { this.xmlFile = xmlFile; FsdcConnection = searchDatabaseConnection; Clear(); } 

I am new to the chain. This is all good, but I have a little problem.

The class that I inject inherits from another class, I have Resharper, and I have the extracted interfaces, but the problem is that the dependency class inherits from another specific class - let's see what I mean?

  public class SearchDatabaseConnect : DatabaseConnect, ISearchDatabaseConnect { // } 

I donโ€™t know what to do with inheritance on DatabaseConnect? How am I kidding? Obviously, if it werenโ€™t, I would have set everything up, I could have mocked ISearchDatabaseConnect, and we are leaving, but I'm stuck in inheriting a specific class. I am sure that people came across this before my googl'ing was unsuccessful when it came to finding examples about it.

Thanks in advance for any helpful suggestions.

+6
source share
1 answer

Does DatabaseConnect interface extracted from it? I think you can configure it like this:

 public interface IDatabaseConnect public class DatabaseConnect : IDatabaseConnect public interface ISearchDatabaseConnect : IDatabaseConnect public class SearchDatabaseConnect : DatabaseConnect, ISearchDatabaseConnect 

And now creating the Mock<ISearchDatabaseConnect> will get all the โ€œthingsโ€ from both interfaces.


Side note, your method / constructor there should probably take an interface, not a concrete one:

 public Search(XmlAccess xmlFile, ISearchDatabaseConnect searchDatabaseConnection) { ... } 

This way you can enter a layout, for example:

 var mockedSearchDatabaseConnect = new Mock<ISearchDatabaseConnect>(); var search = new Search(xmlFile, mockedSearchDatabaseConnect.Object); 
+3
source

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


All Articles