So I want to try to avoid using object in my methods to make things more type safe and avoid boxing/unboxing but I’m not sure what the best way to solve this is. Using generics is not ideal for this situation here since there will be a collection of the type (so it cannot have generic params). I’m thinking a solution that uses double dispatch might work here (i.e. visitor pattern).
The general setup is something along the lines of:
public class ServiceCheck(IList<IServiceCheckResult> results)
{
public IList<IServiceCheckResult> Results = results;
}
public interface IServiceCheckResult
{
IResultValidator Validator { get; }
object? GetRawValue();
void SetRawValue(object? value);
}
public class ServiceCheckResult(IResultValidator validator) : IServiceCheckResult
{
private object? rawValue;
public IResultValidator Validator { get; } = validator;
public object? GetRawValue()
{
return rawValue;
}
public void SetRawValue(object? value)
{
rawValue = value;
}
}
public interface IResultValidator
{
public bool Validate(object? value);
}
public class StringValidator : IResultValidator
{
public bool Validate(object? value)
{
return string.IsNullOrEmpty((string)value);
}
}
public class DoubleValidator : IResultValidator
{
public bool Validate(object? value)
{
return (double)value > 0;
}
}
public class EnumValidator : IResultValidator
{
public bool Validate(object? value)
{
return true;
}
}
...
and the usage will be something like this:
var serviceCheck = new ServiceCheck(new List<IServiceCheckResult>
{
new ServiceCheckResult(new DoubleValidator()),
new ServiceCheckResult(new StringValidator()),
new ServiceCheckResult(new EnumValidator())
});
serviceCheck.Results[0].SetRawValue(1.0);
serviceCheck.Results[1].SetRawValue("This is a test");
serviceCheck.Results[2].SetRawValue(MyEnum.None);
foreach (var result in serviceCheck.Results)
{
var rawResult = result.GetRawValue();
result.Validator.Validate(rawResult);
}