User-Defined Datatypes based on abstract classes

I am trying to create a set of spatial datatypes for SQL Server 2005. Each datatype inherit from the abstract class "Geometry". ie.
  public class Point : Geometry
  public class Line : Geometry
  public class Polygon : Geometry

...and several more.

The reason for using the abstract class Geometry, is that they all share a common set of functions that return a Geometry object. For instance
  Geometry Intersection(Geometry g1, Geometry g2)
  bool Geometry.Within(Geometry g2)
  Polygon Envelope(Geometry g)

etc..

The problem is that when I deploy the assembly to the database, I'm told that the Parameter "Geometry" is unknown. I can't mark it as a Sql-UDT, since this would require it to have a constructor (which abstract classes can't have).

Can this be done, or does anyone have a workaround (fx. using Generics) I'd rather not skip the Geometry class and manually implement all the possible combinations possible for the above functions.

Best regards
Morten Nielsen
http://www.iter.dk



Answer this question

User-Defined Datatypes based on abstract classes

  • tina m

    Is it true that this can also not be realized when using interfaces I get an error when a UDF is returning an interface type which i called IGeometry to make it possible to return UDT like Point, LineString etc.
    (The error when the assembly is deployed: Error 1 Cannot find the type 'IGeometry', because it does not exist or you do not have permission.)

    If interfaces are not working, is there an other way to get some inheritance Or doesn't it work because it cannot find the other files The interfaces are in the same assembly. Should i put them in there own assembly

  • Pierre Couzy

    Found out that the data is stored in a binary format and is also retrieved in a binary when using the select. But if i want to pass the column as SqlBinary through a UDF i got an error that i should first use Convert, the convert Point to the other format. Is it not possible to get this automatically done
  • jitendra badkas

    AAARRRRGGGHHHHH.

    That forces me to write at least 20-30 times the amount of code than with inheritance. Hmm perhaps I have to rethink the whole implementation.

  • jaijai

    You are basically out of luck, as SQL Server 2005 does not understand inheritance chains. Even if you didn't mark your base class abstract it would not work inside SQL Server.

    Niels

  • Andy Wilkinson

    Storing the geometry as Well-Known Binary is the way to go, but I think there still are several problems with the lack of inheritance in SQL-CLR, if you want to implement the "Simple Features for SQL" specification in SQL2005.

    But I'm all for giving it a try again. It could be pretty cool if we could get it to work. I do have most of the Simple Features in a C# implementation (see http://sharpmap.iter.dk), so it wouldn't be to big a job I guess, if I can get around the inheritance limitations.


  • wien

    I think that the only option is to use convert or make a method like AsBinary() which returns a SqlBinary. The AsBinary() is also inside the OGC specification for Simple Features for SQL.

  • GarethJ MSFT

    I got the limitation confirmed by the SQL-CLR team that SQL2005 doesn't support inheritance on UDT. They said this would not be possible until in some future version of SQL20xx.
  • ms_peterk

    Is it also not possible to give UDF arguments of the type System.ValueType because structs are derived from that Or otherwise what is the default type when you select an UDT from the database. Is it something like SqlBinary which can be used directly without converting passed to a UDF.

  • User-Defined Datatypes based on abstract classes