Update:
Heinzi is right. AutoCAD Polyline is reference type and not a struct. Good point. But I have simplified the scenario as what I am dealing with in the real application is an AutoCAD object which is struct. So please consider both as struct not reference type.
I am looking for the right approach to take in this situation and would appreciate if anyone can shed light or help me to better understanding.
There is an interface in the data access layer with two implementations to deal with two different providers: AutoCad and Sketchup APIs.
interface IEntity
{
void object GetPoly();
void void InsertPoly(object poly);
}
class AutocadEntity
{
void object GetPoly()
{
//calling Autocad APIs
return Autocad Polyline object
}
void InsertPoly(object poly){...}
}
Autocad implementation of GetPoly would return Polyline object as this is what defined in Autocad APIs as polyline, whereas Sketchup would return Face object.
I have defined the return type(and the parameter) as object to deal with these different types. The cost is performance issue where boxing/unboxing happens. And it shows itself more boldly where the return/parameter is object[].
I first wondered making the method return/parameter type generic is the solution but then I think it wouldn't be as the implementations are type specific.
Generic interfaces can inherit from non-generic interfaces if the generic interface is covariant, which means it only uses its type parameter as a return value.
Java Generics Bounded Type ParametersBounded type parameters can be used with methods as well as classes and interfaces. Java Generics supports multiple bounds also, i.e <T extends A & B & C>. In this case, A can be an interface or class. If A is class then B and C should be an interface.
You can declare variant generic interfaces by using the in and out keywords for generic type parameters. ref , in , and out parameters in C# cannot be variant. Value types also do not support variance. You can declare a generic type parameter covariant by using the out keyword.
The cost is performance issue where boxing/unboxing happens.
I don't think so. Polyline is a class, not a struct. Thus, there is no boxing involved.
If this is in fact a performance bottleneck of your application, your performance is lost somewhere else. As always: measure before your optimize, or you might end up optimizing the wrong things.
I think your solution is perfectly fine. You could use generics and derive AutocadEntity
from IEntity<Polyline>
, but what would be the point? Since Polyline/Face is used both as input and output parameter in your interface, you can make IEntity
neither co- nor contravariant. Thus, the most common base type of IEntity<Polyline>
and IEntity<Face>
would be object
, which means that you can no longer just pass a general IEntity
if you don't know the concrete type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With