How to change class of an existing object?

8 posts, 0 answers
  1. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 27 Mar 2009 Link to this post

    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

  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 01 Apr 2009 Link to this post

    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.
  3. DevCraft banner
  4. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 01 Apr 2009 Link to this post

    Hi,
    It's not good but I must  learn to live with it :-)

    Thanks
    Tomasz
  5. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 03 Apr 2009 Link to this post

    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.
  6. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 03 Apr 2009 Link to this post

    Hi,
    Thanks for advises. You are right, of course, but I'm not a fan of the "switch" statement :-)

    Regards
    Tomasz
  7. SuperXRAY
    SuperXRAY avatar
    82 posts
    Member since:
    Feb 2007

    Posted 04 May 2009 Link to this post

    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.
  8. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 05 May 2009 Link to this post

    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

  9. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 06 May 2009 Link to this post

    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.
Back to Top
DevCraft banner