I am trying to create the best abstraction for the following situation. Maybe someone can help.
This is what i am having at the moment:
public class Point{
public double? Value {get;set;}
public Information DataInformation {get;set;}
}
public class RawPoint{
//just something to process, not interesting for our example
}
public interface Service{
List<Point> ProcessPoints(List<RawPoint> rawData);
}
public ConcreteService : Service{
public List<Point> ProcessPoints(List<RawPoint> rawData){
//process the points...
}
}
Now i have a request where i have to intoduce a new type of Point, something like:
public class NewPointData{
public double? Point_Max {get;set;}
public double? Point_Min {get;set;}
}
public NewPoint {
public NewPointData Value { get; set;}
public Information DataInformation {get;set;}
}
I would like to have the same ConcreteService as seen before with the same ProcessPoints() method and instead of returning List i would like that it returns an abstractio that can be extented by both Point and NewPoint (the only difference between them is the data type of the Value attribute). Is there a way of achieving this without using typeof() and only by dirrect usage of abstractions / polymorphism in the client ?
Thanks
Use Generics:
public class Point<TValue>
{
public TValue Value { get; set; }
public Information DataInformation { get; set; }
}
Then, change your service interface to:
public interface Service
{
List<Point<TValue>> ProcessPoints<TValue>(List<RawPoint> rawData);
}
You'll need to provide the generic type argument when calling the method:
var points = _service.ProcessPoints<double?>(data);
// points is of type List<Point<double?>>
var newPoints = _service.ProcessPoints<NewPointData>(data);
// points is of type List<Point<NewPointData>>