Here's the basic idea of what we're doing:
- The website collects data from a user in a form and saves it all to a row in a table, which we'll call FooObject.
- FooObject is then passed into a business logic class
- Based on a key in the app.config, a factory class will instantiate one of three concrete classes:
- XMLStringA : IFooInterface
- XMLStringB : IFooInterface
- XMLStringC : IFooInterface
- Each class has two methods:
- GenerateXML(FooObject fooObject)
- ParseResponse(string serviceCallResponse)
- After the class in instantiated, we call GenerateXML(), pass in FooObject, get back an XML string, and then submit it to any one of three separate, external, third party web services. (The address of the service is also in the app.config.)
- We get the response back, and send it into ParseResponse()
All's well. But, C has some extra requirements. When submitting the XML to this service, one of the XML elements requires an extensive lookup. We decided to add the data needed for that lookup to a table in our database. So, there is a private method in XMLStringC that uses EF to make a DB call and get the needed data to add to the XML string.
I was kind of aware that doing this violated the Single Responsibility principle, as these classes should really be doing nothing but building an XML string. The A and B classes don't make a call to the DB.
The possible folly of what I was doing was driven home when I tried to make a unit test to test A, B, and C. As we are not in context when running the unit test, C fails when trying to call the DB.
I'm not sure where to do this custom logic for C. On one hand, it only happens when we are going to submit to the C service, so it makes sense to do it inside the C class. On the other hand, I don't like making a database call from inside that class. Ultimately it might not matter, if I can figure out how to just unit test it and make it work.
What's the best practice way to do this?