tags:

views:

93

answers:

3

First of all let me just say that I am new to nHibernate so if the answer to this is obvious forgive me. I have an abstract base class with all it's members abstract.

public abstract class BallBase
{
  public abstract RunsScored { get; }
  public abstract IsOut { get; }
  public abstract IsExtra { get; }
  public abstract GetIncrement();

}

And concrete implementations like

public class WideBall : BallBase
{
  public override RunsScored
  {
    private int m_RunsScored = 1;

    public WideBall(): base()
    { }

    public WideBall(int runsScored) : base()
    {
        m_RunsScored = runsScored;
    }

    public  override int RunsScored
    {
        get
        {
            return m_RunsScored;
        }
    }

    public  override bool IsOut
    {
        get
        {
            return false;
        }
    }

    public  override bool IsExtra
    {
        get
        {
            return true;
        }
    }

    public  override int GetIncrement()
    {
        // add 0 to the balls bowled in the current over
        // a wide does not get added to the balls bowled
        return 0;
    }

  }

}

I want to use nHibernate to persist the concrete classes, but apparently all public members of the class need to be virtual. Is there any way to continue with my base class approach?

+3  A: 

Thats correct if you want to utilise NHibernates ability to implement lazy loading then you need to make public members virtual.

What you can do is set the attribute lazy=false in the class tag and if you want any particular member (generally bags, lists etc) lazy then set the lazy attribute for that member to lazy=true and make its corresponding member virtual.

I've approached it this way a number of times no problem.

David
+1  A: 

Yes, but the specifics will depend on how you prefer to set up your database schema. Take a look at section 8 in the NHibernate documentation. We're using <joined-subclass> and it's saved us an enormous pile of code.

You don't have to use lazy-loading if you don't want. To turn off lazy-loading for your mapped class, you can add lazy="false" to your class mapping (the .hbm.xml file).

By the way, I assume you have a specific reason for a purely abstract base class instead of an interface?

David Rubin
He is able to use lazy loading because abstract also means virtual in C#
Paco
@Paco, you're right, I'm a moron. Updated.
David Rubin
Thanks Paco, I had was wondered if nhibernate considered abstract and virtual as being the same
Andrew
+2  A: 

Have a look at the C# documentation, these properties are virtual. You don't have to do anything special, you can just go on.

Paco