i don't think you can map it that way. you need a base class/interface
that Foo and Bar inherit from.
[ActiveRecord("Fu", DiscriminatorColumn="type",
DiscriminatorType="string")]
abstract class Fu
{
      public abstract bool IsValid();
}
[ActiveRecord("Fu", DiscriminatorColumn="type",
DiscriminatorType="string", DiscriminatorValue="Foo")]
class Foo : Fu
{
    public override bool IsValid() { }
}
[ActiveRecord("Fu", DiscriminatorColumn="type",
DiscriminatorType="string", DiscriminatorValue="Bar")]
class Bar : Fu
{
    public override bool IsValid() { }
}

which would change your query to
SimpleQuery q = new SimpleQuery("from Fu where whatever");
var results = q.Execute();
foreach (Fu result in results)
{
    System.out.WriteLine(result.IsValid())
}

as it stands now NH will always return implementations of Foo and not
Bar because Bar implements Foo, you are querying for all Foos and Foo
is a concrete type.
BTW: I'm not 100% on the AR mappings. I use NH mappings directly.

On Dec 10, 2:07 pm, Chris Curvey <[email protected]> wrote:
> I'm sure that I'm missing something basic, but I'm not sure if it's a
> C# thing that I'm missing, or an HQL thing that I'm missing, or an AR
> thing that I'm missing.  I hope someone can help.
>
> I have two classes in a single-table inheritance relationship
>
> [ActiveRecord("MyTable", DiscriminatorColumn="type",
> DiscriminatorType="string", DiscriminatorValue="Foo")]
> public class Foo : ActiveRecordBase<Foo>
> {
>    public bool IsValid() { ...do some stuff ...}
>
> }
>
> [ActiveRecord(DiscriminatorValue="Bar")]
> public class Bar : Foo
> {
>    public bool IsValid() { ... do some stuff ... }
>
> }
>
> So what I want to do is roll through a list of instances, and call
> IsValid() on each of them, taking advantage of polymorphism.  So I
> have this:
>
> SimpleQuery q = new SimpleQuery("from Foo where whatever ");
> Foo[] fooArray = q.Execute();
> foreach (Foo foo in fooArray)
> {
>     System.out.WriteLine(foo.IsValid())
>
> }
>
> But it always seems to call "Foo.IsValid()" and never calls
> "Bar.IsValid()".   Which kind of makes sense, as the "foo" variable
> has been declared as an instance of "Foo", but how do I set it up so
> that "foo.IsValid" calls the appropriate routine?  I've tried
> upcasting and downcasting, but I can't get it straight.
>
> So, what am I missing?

--

You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.


Reply via email to