0

I wonder what is the best way to implement dynamic domain model persisted in SQL database? Saying dynamic domain model I mean such model when user can change "class" of object by adding or removing properties.

For example: little internet shop where manager can add new types of product with web-interface. Should I use something like Map everywhere? Maybe it is better to keep only dynamic part of object in Map? Or maybe runtime class generation will help? Or I should take a tour over dynamic languages like Groovy? I have tested first and second methods and this is real headache to develop in such way.

What is the common practise?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Alex Povar
  • 4,890
  • 3
  • 29
  • 44
  • 1
    Might be a better topic for [Programmers](http://programmers.stackexchange.com/). – Beau Grantham May 22 '12 at 17:25
  • The fact that a manager can add a new type of product doesn't mean your model is dynamic. It means you have a Product entity which as a many-to-one association to a ProductType entity (a product table with a foreign key to a product_type table, in RDBMS terms) – JB Nizet May 22 '12 at 17:27
  • Sorry for offtop but what is difference between stackoverflow and Programmers? – Alex Povar May 22 '12 at 17:28
  • Yeah. Thats what I've done: declare fields in ProductType entity. But how I should deal with it in my code? If manager have created new ProductType should I generate new java-class for it with cglib magic? – Alex Povar May 22 '12 at 17:32
  • @AlexPovar, You would then add a new row in the table that corresponds to the ProductType entity. Then you can create new rows in the Product table that correspond to the new ProductType. – Jeremy May 22 '12 at 17:38

1 Answers1

3

You have a 1 to 1 from product to product type. So product has a member var called productType. You'll also have a 1-to-many from product to features. So the product class will have a list of features.

I can't imagine a more wrong way to do this then using cglib to create classes on the fly.

Jim Barrows
  • 3,634
  • 1
  • 25
  • 36