comp.lang.ada
 help / color / mirror / Atom feed
From: Marek Janukowicz <marek@janukowicz.net>
Subject: Design problem: generic package vs tagged subtype
Date: Wed, 30 Jun 2010 19:52:58 +0200
Date: 2010-06-30T19:52:58+02:00	[thread overview]
Message-ID: <i0g09r$da2$1@adenine.netfront.net> (raw)

Hello everyone

I'm struggling with some design problems in my project. To explain them I 
need to give some background on my project, so please bear with me.

I'm developing a library to wrap relational database records into Ada 
objects (this kind of libraries are usually called "ORMs"). Generally I need 
following entities:
- some type corresponding to database table (I called it "Model"). It's 
instances will hold records from that table, so it needs something to hold 
database column values as attributes. Operations generally need to be 
overridable.
- something to map relationships between tables (associations)

For Model I use a tagged type wrapped into generic package:
generic
   Tbl_Name : String;
   type Attribute_Type is (<>);
   type Attribute_Type_Array is array (Attribute_Type range <>) of 
Attribute_Type_Type;
   Attribute_Types : Attribute_Type_Array;
package Dar.Models.Base is
            
   type Model is abstract tagged limited record
...

For associations I also use a generic package:
generic 
   with package Source_Model is new Dar.Models.Base(<>);
   with package Target_Model is new Dar.Models.Base(<>);
   Foreign_Key : String;
package Dar.Models.Associations is
   function Find
...


Here's where the trouble begins. Those Model subtypes tend to have quite a 
lot of custom logic, some operations overriden etc. and I can't add this to 
generic package instantiation, so I instantiate Dar.Models.Base inside 
another package:

package Dar_Test.Models.Users is
   type Attribute_Type is (Id, Username, Email);
   type Attribute_Type_Array is array (Attribute_Type range <>) of 
Dar.Models.Attribute_Type_Type;
   package Base is new Dar.Models.Base(
      Tbl_Name => "authors",
      Attribute_Type => Attribute_Type,
      Attribute_Type_Array => Attribute_Type_Array,
      Attribute_Types => (
         Id => Attr_Integer,
         Username => Attr_String,
         Email => Attr_String
      )
   );
   (some additional operations)
   ...

What I don't like is that I have additional operations in Users package, but 
builtin operations in Users.Base (I want to make the design clean for 
programmers using my library). I don't like parametrizing Associations with 
Base instantiation. I think having just a tagged type (without a generic 
package Dar.Models.Base) and extending it would be better, but I don't know 
how to parametrize it with Attribute_Type type. 

I hope my description is not too vague. If you have any thoughts or think my 
approach is totally wrong please share your thoughts.

Two additional (small) questions:
1. I'd like to replace type Attribute_Type_Array is array (Attribute_Type 
range <>) of Attribute_Type_Type in Dar.Models.Base specification with ... 
array (Attribute_Type'Range) ... to make sure full range of Attribute_Type 
is specified, but the compiler complains. Is there any other way to ensure 
that?
2. I have a type Attribute_Type_Type used to specify types of attributes 
(Integer, Date, String etc.). I then have a variant record:
  type Attribute_Value( Attr_Type : Attribute_Type_Type ) is
      record
         case Attr_Type is
            when Attr_Date =>
               Date_Value : APQ_Date;
            when Attr_Time =>
               Time_Value : APQ_Time;
            ...
Is there any way to use builtin types here instead of Attr_Date, 
Attr_Integer, etc? Note that I need to specify type of each attribute in one 
array.

Thanks in advance for reading my lengthy post :)
-- 
Marek Janukowicz

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---



             reply	other threads:[~2010-06-30 17:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-30 17:52 Marek Janukowicz [this message]
2010-06-30 19:23 ` Design problem: generic package vs tagged subtype Simon Wright
2010-06-30 22:33   ` Marek Janukowicz
2010-07-01  4:13     ` naming, was " tmoran
2010-07-01  9:49       ` J-P. Rosen
2010-07-01 15:48         ` Marcelo Coraça de Freitas
2010-07-01  5:57     ` Simon Wright
2010-07-01 18:30       ` Jeffrey R. Carter
2010-07-01 19:28         ` Simon Wright
2010-07-01 19:59           ` Jeffrey R. Carter
2010-07-01 21:14             ` Per Sandberg
2010-07-02  9:25         ` Stephen Leake
2010-07-02 10:50           ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox