Rails-like Model in D (making it "automagical")
Lars Ivar Igesund
larsivar at igesund.net
Mon Mar 24 15:45:14 PDT 2008
Edward Diener wrote:
> Chris Miller wrote:
>> I've been throwing ideas around in my head for my pet project, ConYard
>> (http://www.fsdev.net/wiki/conyard), and I'm not sure how to implement
>> this in D. I don't really know how to implement it in any language, so I
>> thought I'd ask for some advice.
>>
>> I want to create a model system similar to, though perhaps not
>> necessarily as "automagical" as the models from Ruby on Rails. I want a
>> system that the programmer can extend to create a database-independent
>> object that serializes to a database. The problem I'm grappling with is
>> how to make it so that the user doesn't have to hand-code everything, and
>> can make the implementation more readable.
>>
>> I'm not sure how to handle the model's fields. I want the fields to be D
>> types, eg. char[] and int and float and all that. I also want for those
>> types to serialize to the SQL database, without the user having to write
>> all that nonsense by hand.
>>
>> I need a way to somehow flag a field in a model as an element that needs
>> to be serialized to a database. From there it's fairly simple work of
>> writing code that makes SQL statements based on input. It's so simple,
>> it scares me into over complicating it.
>>
>> I'm not sure how to start about this. I was thinking of some kind of
>> static this () statement that the user has to fill in, and somehow
>> "register" all the elements (that'll be in the database). Then calls to
>> functions that push/pull data to/from the database could call another
>> function that the user doesn't have to touch, which takes all those
>> elements from the model instance and generates the SQL queries (or uses a
>> previously generated one) to make the data move.
>>
>> It still seems like a lot of overhead to me, like a lot of boring stuff
>> that the programmer has to type in all the time. Imagine a library where
>> you have to do something annoying like
>> registerElement(IntElement(this.myfield)); in a static constructor all
>> the time? (IntElement would be some kind of element class that would
>> know how to take a D int and make it to a SQL int, and then you could
>> possibly write your own to take types that you make and translate them
>> into SQL types, or serializations that fit into SQL types, etc).
>>
>> There must be a better way. I've scanned through the D 1.00
>> specification document a few times and come up dry. If anyone has any
>> suggestions of things I might try, or even examples of techniques that I
>> could use, I'd really appreciate it!
>
> Ruby ( and Python ) both have run-time reflection built in to their
> language. D does not. I would suggest to you that emulating RAD
> programming with a language which does not have built-in support for
> run-time reflection will be a very hard task for you to accomplish.
Not a terribly hard thing to get around, given the correct tools.
--
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango
More information about the Digitalmars-d
mailing list