This is a migrated thread and some comments may be shown as answers.

How to change class of an existing object?

7 Answers 89 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
tmlipinski
Top achievements
Rank 1
tmlipinski asked on 27 Mar 2009, 11:24 AM
Hi,

Let's assume that there is a table named Setting:
  • id
  • name
  • category
  • text_value
  • int_value

Based on this table is a flat inheritance hierarchy designed, with "category" as the class discriminator

  • Setting as a top class (category = 'X'); includes columns: id, name, category
  • SettingText: category = 'T'; includes column: text_value
  • SettingInt: category = 'I'; includes column: int_value

There is an existing object - SettingText. I would like to change it to SettingInt. From the database point of view it's easy: just set category = 'I' (I know that I loose the text_value column content). But how to do it using ORM mechanisms? I can't just cast SettingText to SettingInt. Deleting the old object and creating a new one is not always possible (it may be an FK somewhere). Have you got any suggestions?

Regards
Tomasz

7 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 01 Apr 2009, 08:47 AM
Hi tmlipinski,

I think in this particular case using inheritance is not very appropriate due to impossible casting. I suggest you using only one class that has both text and int fields. You can have additional bool field (for example isText) that takes the role of a discriminator.

Best wishes,
Alexander
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
tmlipinski
Top achievements
Rank 1
answered on 01 Apr 2009, 10:32 AM
Hi,
It's not good but I must  learn to live with it :-)

Thanks
Tomasz
0
Thomas
Telerik team
answered on 03 Apr 2009, 02:02 PM
Hello tmlipinski,

to me this sounds not so much as morphing an object type but more like an 'posing-as' scenario.
Depending on a user controlled discriminator, the object behaves different. You can do exactly that when all columns are included in the type (flat mapping) and you decide in your code which route to take depending on the category value.

Regards,
Thomas
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
tmlipinski
Top achievements
Rank 1
answered on 03 Apr 2009, 09:03 PM
Hi,
Thanks for advises. You are right, of course, but I'm not a fan of the "switch" statement :-)

Regards
Tomasz
0
SuperXRAY
Top achievements
Rank 2
answered on 04 May 2009, 07:12 PM
Ever thought of re-structuring your database? I don't know the specifics here, but it sounds like you are using a workaround in the DB Structure to accomplish your goal. It could bite you in the end.
0
tmlipinski
Top achievements
Rank 1
answered on 05 May 2009, 08:20 AM

Hi,
Of course, you are right - in the sense that if it is a problem, it should be re-thought, re-designed and so on. My original post is rather a theoretical question than a practical problem to be solved.

Thanks for your adivises, too.

 

Regards

Tomasz

0
Jan Blessenohl
Telerik team
answered on 06 May 2009, 09:55 AM
Hi tmlipinski,


Regards,
Author nickname
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Development (API, general questions)
Asked by
tmlipinski
Top achievements
Rank 1
Answers by
Alexander
Telerik team
tmlipinski
Top achievements
Rank 1
Thomas
Telerik team
SuperXRAY
Top achievements
Rank 2
Jan Blessenohl
Telerik team
Share this question
or