Your static Validaton method seems like something that should be part of an object for me.
Say you have a Drink class
public class Drink { private readonly string _name; private readonly double _temperature; public Drink(string name, double temperature) { _name = name; _temperature = temperature; } }
Your business logic will be able to create all kinds of drinks, 7up, cola, whatever. You want to make sure that the drink has the right temperature to drink it, so you need the Validate method. You can follow your approach:
public void TakeAZip() { if (Validation.HasAppropriateTemp) {
Alternatives for static classes
Thus, you have a strong dependency on your static Validation class.
Alternatively, you can use dependency injection .
public void TakeAZip(ITemperatureValidator validator) { if (validator.HasAppropriateTemp) {
If more convenient, you can also pass Validator through the constructor
private readonly string _name; private readonly double _temperature; private ITemperatureValidator _validator; public Drink( string name, double temperature, ITemperatureValidator validator) { _name = name; _temperature = temperature; _validator = validator; }
Now you can mock the behavior of your validator, and you can isolate your Drink class from all external actions.
source share