-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Collections of base classes
PostPosted: Mon Oct 15, 2007 7:59 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Hi,

Given a class:

Code:
class SomeClass
{
  IList<BaseClass> CollectionOfThings;
  int Type;
}

abstract class BaseClass
{}

class FirstDerivedClass: BaseClass
{}

class SecondDerivedClass: BaseClass
{}


CollectionOfThings should only contain BaseClasses of only one type:
==> If type=1, initialise with FirstDerivedClass
==> if type=2, initialise with SecondDerivedClass

Is there a way to do this or is it better just to subclass SomeClass so that each subclass a list of specific types? This presumably would be very easy with the table-per-hierarchy design, and might arguably be cleaner anyway.

Thanks,

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 15, 2007 4:04 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Assuming that you don't intend to change the type used in the collection at runtime, subclassing sounds like a better approach to me. You could still use the SomeClass base (abstract I assume) along with the abstract factory pattern to get the appropriate subclass at runtime (if applicable).

You could even have the base class and subclasses in the same table and put the discriminator (int Type) in the subclass mappings so that you only get collections of the desired type.

Having the discriminator in the code itself, however, would require for you to get the desired type programmatically.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:48 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Hi,

Yes, you're probably right; I think it's something I'd have just done myself with ADO (initialised after construction, limited access to the collection and controlled what was in it programmatically) but it's probably more correct to subclass for the type safety.

Thanks,

Steve


Top
 Profile  
 
 Post subject: Mixing sub class types
PostPosted: Tue Oct 16, 2007 7:46 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
[See later post for better formatting]


Last edited by SteveMc on Tue Oct 16, 2007 7:47 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Mixing sub class types
PostPosted: Tue Oct 16, 2007 7:47 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Further to this, is it possible for a three level hierarchy:
Code:
BaseClass
  --> SomeClass
        ---> DerivedClassTypeA
        ---> DerivedClassTypeB

to use a mix of subclass methods?

For example, if the table storing BaseClass has the type parameter that determines the type of DerivedClassTypeX but SomeClass involves a join to another table?

E.g.
Code:
<class BaseClass>
  <discriminator column="type">
  <joined-subclass name="SomeClass">
    <subclass name="DerivedClassTypeA" discriminator-value="1"/>
    <subclass name="DerivedClassTypeB" discriminator-value="2"/>
  </joined-subclass>
</class>

I guess this could get confusing, but it doesn't seem that weird.

Steve


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.