I have a class named as A and another Class named as B. Class A contains Class B as a property.
I have a business logic. Based on the value of a property in Class B, I have to calculate the value of another property in Class A. To do this calculation, I have to call a SOAP service, get a value and then, based on the value of a property in Class B, i have to do a mathematical calculation on the returned value from the SOAP service and then set the value for property in Class A.
public Class A{
public string Property1{get;set;}
public int Property2{get;set;}
public B Property3{get;set;}
}
public class B{
public string Property1{get;set;}
public string Property2{get;set;}
}
The logic in pseudocode
I dont like the If else, it doesnt follow the OCP. I was thinking of doing this with a decorator. The second option is to use a builder pattern and encapsulate the if else logic, but still it would break the OCP. Or a completely new Domain service that would perform this?But still the OCP is being broken if If else even if i chose to go the Domain service way.
The above logic is being performed in a business logic class.
So where does the responsibility for above logic of calculation go? Also how to avoid that if else?
I would create a DDD-type 'domain service' that takes a dependency on the SOAP service via an interface and do it that way. Nice and testable, and I can't see why it wouldn't be OCP-compliant.
Making the domain model dependent on external services should be avoided.