Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using generic in the interface

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.

like image 569
ali Avatar asked Aug 14 '15 13:08

ali


People also ask

Can we use generic in interface?

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.

Can we use generics with interface in Java?

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.

Can you use generic with interface C#?

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.


1 Answers

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.

like image 124
Heinzi Avatar answered Sep 19 '22 05:09

Heinzi