Dynamic Components in ActiveRecord

33 views
Skip to first unread message

Dan Bunea

unread,
Oct 16, 2007, 7:40:03 AM10/16/07
to castle-pro...@googlegroups.com
Hi,

One of our requirements in a new app we're writing is to allow runtime add/remove of columns to some tables in our database. To be able to still use AR, we thought we could use NH 1.2 dynamic components ( http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/components.html#components-dynamic). Doesn anyone know whether this is available in AR on Castle RC3, or will we have to contribute to it?

Thanks,

--
Dan Bunea
http://danbunea.blogspot.com

Ayende Rahien

unread,
Oct 16, 2007, 7:41:50 AM10/16/07
to castle-pro...@googlegroups.com
not that I know of

Hamilton Verissimo

unread,
Oct 16, 2007, 9:37:14 AM10/16/07
to castle-pro...@googlegroups.com
Let me guess: SaaS?

On 10/16/07, Dan Bunea <dan....@gmail.com> wrote:


--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/

josh robb

unread,
Oct 16, 2007, 9:41:20 AM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Hamilton Verissimo <ham...@castlestronghold.com> wrote:
>
> Let me guess: SaaS?

Welcome to my world.

:(

Wait until they want reports on these new fields is all I have to say
(and take a look at CouchDB).

j.

Hamilton Verissimo

unread,
Oct 16, 2007, 9:45:32 AM10/16/07
to castle-pro...@googlegroups.com
What approach are you using? Root entities that allow extensions (ie
more columns) or everything is on an Entity table (kinda like jira
does)?

Ayende Rahien

unread,
Oct 16, 2007, 9:58:28 AM10/16/07
to castle-pro...@googlegroups.com
Not only, there are quite a few reasons to want that.
CRM, ERP, Bug Tracking, etc.

I would not go with dynamic components, actually, but rather sub classing at the customer site, maybe aided by a wizard

On 10/16/07, josh robb <josh...@fastmail.fm> wrote:

Jonathon Rossi

unread,
Oct 16, 2007, 9:59:10 AM10/16/07
to castle-pro...@googlegroups.com
I was wondering how other people did this too.

In a project I have been working on, a specific entity has dynamic fields. I implemented fields as separate entities. The complexity comes when you think about what type of fields are needed. I had to create one for text, many-to-one (with another value entity), user, and various others specific ones to store links to other entities in the model. My model became even more complex when I implemented revisions on all those fields. All the data for these fields are stored in separate tables, I didn't want to make changes to the DB schema for the dynamic columns so heaps of separate entities and heaps of joins is the way it had to be made.

If there is a nicer way of doing this or even a nicer way of being able to deal with all these classes in my calling code I'd love to hear what others have to say.

On 10/16/07, Hamilton Verissimo <ham...@castlestronghold.com> wrote:

What approach are you using? Root entities that allow extensions (ie
more columns) or everything is on an Entity table (kinda like jira
does)?

On 10/16/07, josh robb < josh...@fastmail.fm> wrote:
>
> On 10/16/07, Hamilton Verissimo <ham...@castlestronghold.com> wrote:
> >
> > Let me guess: SaaS?
>
> Welcome to my world.
>
> :(
>
> Wait until they want reports on these new fields is all I have to say
> (and take a look at CouchDB).
>
> j.
>
> >
>


--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/


--
Jonathon Rossi

josh robb

unread,
Oct 16, 2007, 10:02:20 AM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Hamilton Verissimo <ham...@castlestronghold.com> wrote:
> What approach are you using? Root entities that allow extensions (ie
> more columns) or everything is on an Entity table (kinda like jira
> does)?

I don't know what Jira does. (Looks what jira does:
http://confluence.atlassian.com/display/JIRA/Database+Schema)

Ok - yep - like JIRA - I'd call it meta modeling. At the moment we've
managed to keep the extensibility to one entity - but it's really
necessary for 3. (and all of them have multiple many-to-many relations
- which also might need to be extensible).

Sigh - anyway.. I'm personally of the opinion that I'm fighting the
relational model with this stuff. A semi-structured store seems to be
the way forward (in the long term).

j.

Ayende Rahien

unread,
Oct 16, 2007, 10:18:05 AM10/16/07
to castle-pro...@googlegroups.com
You are only fighting it because you are trying to avoid using tables the way they are meant to.
Create additional tables for the new data, and that would make it much easier, no?

On 10/16/07, josh robb <josh...@fastmail.fm> wrote:

Dan Bunea

unread,
Oct 16, 2007, 10:18:58 AM10/16/07
to castle-pro...@googlegroups.com
The approach is simple and already working on the current version of the program (C++). It is only one table, where the customers (large retailers) keep their "option" data: garment, color, model, size etc+some additional fields (aquisition price 1 , tax 2 etc). 

They do want reporting on it, but they write their own reports, and most of the time they don't.

There is a second oprion: Lucene, but they might not like it because of the reporting tools.

Thanks,
Dan



On 10/16/07, Hamilton Verissimo <ham...@castlestronghold.com> wrote:

What approach are you using? Root entities that allow extensions (ie
more columns) or everything is on an Entity table (kinda like jira
does)?

On 10/16/07, josh robb < josh...@fastmail.fm> wrote:
>
> On 10/16/07, Hamilton Verissimo <ham...@castlestronghold.com> wrote:
> >
> > Let me guess: SaaS?
>
> Welcome to my world.
>
> :(
>
> Wait until they want reports on these new fields is all I have to say
> (and take a look at CouchDB).
>
> j.
>
> >
>


--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/


Ayende Rahien

unread,
Oct 16, 2007, 10:21:57 AM10/16/07
to castle-pro...@googlegroups.com
Lucene?
You are talking about using that as a data store instead of for searching?

On 10/16/07, Dan Bunea <dan....@gmail.com > wrote:

Jonathon Rossi

unread,
Oct 16, 2007, 10:23:03 AM10/16/07
to castle-pro...@googlegroups.com
If the app doesn't need many extra fields and you aren't going to build a UI for the user of the app to change them, I'd add extra fields to the DB like Ayende mentioned.

However, that doesn't work so easily when users of the app should be able to change the fields. Straight off you'd need dbo permissions on the DB.
--
Jonathon Rossi

Ayende Rahien

unread,
Oct 16, 2007, 10:33:43 AM10/16/07
to castle-pro...@googlegroups.com
Who are those users?
If we are talking about something like ERP, then by the nature of the beast, you are going to need admin access to make those modifications.
If we are talking about defining custom fields for each project in a bug tracking system, this is another matter, probably best solved by putting those outside the DB.
I am very interested with the lucene idea. Mostly because it is so elegant and so easy to implement, and it makes searches very easy

josh robb

unread,
Oct 16, 2007, 10:41:11 AM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Ayende Rahien <aye...@ayende.com> wrote:
> You are only fighting it because you are trying to avoid using tables the
> way they are meant to.

Nope - this is genuinely semi-structured data (in my thinking). I'm
fighting the model because otherwise I'd get to the point where I
neede a seperate table for each instance of an entity. (not to mention
variations for each customer 20+ and growing rapidly).

> Create additional tables for the new data, and that would make it much
> easier, no?

We actually have an application in house which works in the same
domain (delphi + MSSQL 6.5) which does in fact create a seperate table
for each entity.

Trust me - it's not easier.

j.

josh robb

unread,
Oct 16, 2007, 10:45:50 AM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Jonathon Rossi <jo...@jonorossi.com> wrote:
> However, that doesn't work so easily when users of the app should be able to
> change the fields. Straight off you'd need dbo permissions on the DB.

In my scenario - this is exactly part of the problem. It's user
extensible - not customer extensible.

It gets worse because until now we've been able to get away with not
providing much filtering on dynamic fields - now we're in a situation
where we need to provide filtering on entities based on dynamically
configured attributes. (Think Ayende's policy screen but much worse).

j.

josh robb

unread,
Oct 16, 2007, 10:58:12 AM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Ayende Rahien <aye...@ayende.com> wrote:
> Who are those users?

Users within the system - who have limited access to some subset of
entities in the system and the ability to customise those entities.

> If we are talking about something like ERP, then by the nature of the beast,
> you are going to need admin access to make those modifications.

nope

> If we are talking about defining custom fields for each project in a bug
> tracking system, this is another matter, probably best solved by putting
> those outside the DB.

Exactly - different fields for each bug. :(

> I am very interested with the lucene idea. Mostly because it is so elegant
> and so easy to implement, and it makes searches very easy

Yep - except that Lucene wasn't designed for this - which is why I'm
personally so excited about CouchDB - it's pretty similar actually -
except it was designed from the beginning as a persistent data store,
with ACID guarantees etc.

j.

josh robb

unread,
Oct 16, 2007, 11:03:26 AM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Hamilton Verissimo <ham...@castlestronghold.com> wrote:
> What approach are you using? Root entities that allow extensions (ie
> more columns) or everything is on an Entity table (kinda like jira
> does)?

Have you looked at this OfBix Entity Engine thing?

http://ofbiz.apache.org/

Dan Bunea

unread,
Oct 16, 2007, 11:59:22 AM10/16/07
to castle-pro...@googlegroups.com
Yes, Lucene is fanastic. However because they have reporting tools that look into the db directly they might be against it.
On the other hand, the client can go into the administration UI (WPF) and add  custom fields to the options. He can later change, delete but only the custom ones. It's not an ERP, it's, despite how incredible it might seem, a 3D application: http://www.visualretailing.com/

Thanks for your answers,
Dan

PS offtopic: MVP/MVP on WPF anyone?

On 10/16/07, Ayende Rahien <aye...@ayende.com> wrote:

Ayende Rahien

unread,
Oct 16, 2007, 3:34:53 PM10/16/07
to castle-pro...@googlegroups.com
Josh,
I am spending the the last two weeks working on search screens, and the last one search screen that I did took roughly two - three man months to complete.
I estimate that I would about that amount of effort.

I see what you mean now, you need extra fields per entity instance, not per entity type.
That being the case, I would strongly recommend on going with the lucene approach, that is incredibly easy to get starting, and searching in built in.


On 10/16/07, josh robb <josh...@fastmail.fm> wrote:

josh robb

unread,
Oct 16, 2007, 3:39:30 PM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Ayende Rahien <aye...@ayende.com> wrote:
> Josh,
> I am spending the the last two weeks working on search screens, and the last
> one search screen that I did took roughly two - three man months to
> complete.
> I estimate that I would about that amount of effort.

Yeah - I feel you! The good news is that I get to specify the feature
- so it might be possible to find an 80/20 solution.

> I see what you mean now, you need extra fields per entity instance, not per
> entity type.
> That being the case, I would strongly recommend on going with the lucene
> approach, that is incredibly easy to get starting, and searching in built
> in.

I will definitely investigate - the main thing that worries me about
this is reporting tools. But I might be able to work around that.

Ayende Rahien

unread,
Oct 16, 2007, 3:40:26 PM10/16/07
to castle-pro...@googlegroups.com
Lucene was designed for searching, yes, also as a document repository.
If you think about your entities as data, it makes a lot more sense.

On 10/16/07, josh robb <josh...@fastmail.fm> wrote:

Ayende Rahien

unread,
Oct 16, 2007, 3:58:44 PM10/16/07
to castle-pro...@googlegroups.com
Worse case scenario, you can always write a tiny piece of code that translate Document[] to DataSet, and feed the reports that.


On 10/16/07, josh robb <josh...@fastmail.fm> wrote:

Ayende Rahien

unread,
Oct 16, 2007, 4:02:08 PM10/16/07
to castle-pro...@googlegroups.com
At any rate, it will be much easier than writing the query to report it.

Ayende Rahien

unread,
Oct 16, 2007, 5:49:30 PM10/16/07
to castle-pro...@googlegroups.com

josh robb

unread,
Oct 16, 2007, 6:12:04 PM10/16/07
to castle-pro...@googlegroups.com
On 10/16/07, Ayende Rahien <aye...@ayende.com> wrote:
> Lucene was designed for searching, yes, also as a document repository.
> If you think about your entities as data, it makes a lot more sense.

(not sure if I should take this off list - or somewhere more appropriate)

The problem is that a key underlying assumption of lucene is that it
is possible (although maybe expensive) to reindex all the documents
you feed it. It doesn't make any consistency guarantees in the face of
failure.

CouchDB is basically the same model as Lucene (documents+attributes -
with a blob type) - except it's major design purpose is as a
persistent document storage engine.

I'd need to have some serious reassurance from Doug Cutting that this
was a good idea before I went ahead and did this as it really seems
outside of the designed for use cases. (Like using .mdb/.xls's for
serving asp classic websites ;)

j.

Ayende Rahien

unread,
Oct 16, 2007, 6:44:48 PM10/16/07
to castle-pro...@googlegroups.com
Josh,
I can see your point, but I think that this is already used in mission critical scenarios right now.
I did some searches for transaction semantics for Lucene, and the results seems to point toward the DB store as the way to go there.
It has not been ported yet (that I know of).
I would be interested in receiving an authoritative answer for this.

Phil H

unread,
Oct 16, 2007, 8:37:02 PM10/16/07
to Castle Project Users
Hey

At the database level (not the NH/AR level etc) I have come across a
number of ways of doing this.

I am not promoting any of these - just giving you some options to
consider.

1. Entity-Attribute-Value (EAV) See http://en.wikipedia.org/wiki/Entity-Attribute-Value_model
In this model the data is pivoted so that columns become rows. If
you are using SQL Server you could use a sql_variant column to store
the value rather than have a different column for each type or store
all types as a varchar. As I see it the main downside to this approach
is the increased complexity of querying.

2. XML. (Tokenized field) Store additional user defined data in an XML
column (SQL Server 2005 or later)

3. Predefined columns. Create a number of generic columns (eg,
varchar1, varchar2, ...) which can be mapped to custom properties as
they are added. This is how MS Sharepoint and TFS seem to work.
Actually, this might not be appropriate for your situation since you
want to be able to add custom fields per item, not per type. FWIW I
notice that SQL Server 2008 has support for "sparse columns" which
allows you to define columns which use up no space if they are not
used.

Regards
Phil

On Oct 16, 11:59 pm, "Jonathon Rossi" <j...@jonorossi.com> wrote:
> I was wondering how other people did this too.
>
> In a project I have been working on, a specific entity has dynamic fields. I
> implemented fields as separate entities. The complexity comes when you think
> about what type of fields are needed. I had to create one for text,
> many-to-one (with another value entity), user, and various others specific
> ones to store links to other entities in the model. My model became even
> more complex when I implemented revisions on all those fields. All the data
> for these fields are stored in separate tables, I didn't want to make
> changes to the DB schema for the dynamic columns so heaps of separate
> entities and heaps of joins is the way it had to be made.
>
> If there is a nicer way of doing this or even a nicer way of being able to
> deal with all these classes in my calling code I'd love to hear what others
> have to say.
>

> On 10/16/07, Hamilton Verissimo <hamm...@castlestronghold.com> wrote:
>
>
>
>
>
> > What approach are you using? Root entities that allow extensions (ie
> > more columns) or everything is on an Entity table (kinda like jira
> > does)?
>

> > On 10/16/07, josh robb <josh_r...@fastmail.fm> wrote:


>
> > > On 10/16/07, Hamilton Verissimo <hamm...@castlestronghold.com> wrote:
>
> > > > Let me guess: SaaS?
>
> > > Welcome to my world.
>
> > > :(
>
> > > Wait until they want reports on these new fields is all I have to say
> > > (and take a look at CouchDB).
>
> > > j.
>
> > --
> > Cheers,
> > hamilton verissimo

> > hamm...@castlestronghold.com

Ayende Rahien

unread,
Oct 16, 2007, 8:48:35 PM10/16/07
to castle-pro...@googlegroups.com
Xml columns are an interesting comproise.

josh robb

unread,
Oct 17, 2007, 5:24:55 AM10/17/07
to castle-pro...@googlegroups.com
Hey Phil,

On 10/17/07, Phil H <phas...@gmail.com> wrote:

> 2. XML. (Tokenized field) Store additional user defined data in an XML
> column (SQL Server 2005 or later)

I'm using this ATM for one (very small) aspect of my solution. I'm
really pleased with how it's working. I'm actually just using the XML
serializer to de/re-hydrate things into/outof the xml column. It's
pretty sweet.

j.

Colin Ramsay

unread,
Oct 17, 2007, 5:43:10 AM10/17/07
to castle-pro...@googlegroups.com
You're doing this manually? It would be good if there were a way to "somehow" let ActiveRecord know that a property on a model should be serialised into a particular column.

On 10/17/07, josh robb <josh...@fastmail.fm> wrote:

josh robb

unread,
Oct 17, 2007, 6:33:55 AM10/17/07
to castle-pro...@googlegroups.com
On 10/17/07, Colin Ramsay <colin...@gmail.com> wrote:
> You're doing this manually? It would be good if there were a way to
> "somehow" let ActiveRecord know that a property on a model should be
> serialised into a particular column.

For the moment - I'm happy having control over the serialization - but
I can see that this would be useful.

Colin Ramsay

unread,
Oct 17, 2007, 6:57:42 AM10/17/07
to castle-pro...@googlegroups.com
This is slightly off topic (and probably exposing my lack of knowledge), but in your circumstance, what advantage do you get storing this data in a proper XML column as opposed to just a string? If you're just serialising in and out of it?

On 10/17/07, josh robb <josh...@fastmail.fm> wrote:

josh robb

unread,
Oct 17, 2007, 7:31:14 AM10/17/07
to castle-pro...@googlegroups.com
On 10/17/07, Colin Ramsay <colin...@gmail.com> wrote:
> This is slightly off topic (and probably exposing my lack of knowledge), but
> in your circumstance, what advantage do you get storing this data in a
> proper XML column as opposed to just a string? If you're just serialising in
> and out of it?

It just means that I can query it if I ever decide I want to. (I know
- YAGNI.... )

Ayende Rahien

unread,
Oct 17, 2007, 7:33:03 AM10/17/07
to castle-pro...@googlegroups.com
Yes, there are, you can use IUserType to do this.

On 10/17/07, Colin Ramsay <colin...@gmail.com> wrote:

Ayende Rahien

unread,
Oct 17, 2007, 7:33:36 AM10/17/07
to castle-pro...@googlegroups.com
You can do XPath queries against that.
Expression.XPath() ?? //doesn't exist now, trivial to do
Reply all
Reply to author
Forward
0 new messages