Crainiate Community

Support and discussion for users of Crainiate component software products
Welcome to Crainiate Community Sign in | Join | Help
in Search

Inheritance on Table class

Last post 12-28-2006 4:54 PM by Archer. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 12-21-2006 6:15 PM

    Inheritance on Table class

    Hi,

    I have the following class hirearchy. I use Table as my base class.

    Table-->DomainObject-->Vat

    DomainObject has some methods that will be base to all my business objects and does not have ANY column attributes.
    Vat is my BusinessObject of interest and has a primary key column and two other columns.

    class DomainObject : Table
    {
    //Some methods that will be used by my BusinessObjects.
    }

    class Vat : DomainObject
    {
    [PrimaryKey(1,1)]
    public int ID {....}

    [Column]
    public string Description {...}

    //...etc.
    }



    Deploying Vat class raises an exception: "Type DomainObject does not have a primary or compound key defined."

    Is it not possible to inherit without defining a primary key?





     
    • 82.45.255.253
  • 12-23-2006 4:50 PM In reply to

    Re: Inheritance on Table class

    It is not possible to inherit from the Table class unless you define a column and key, this is because a table needs to be created. However there is a case for an underlying abstract class such as the one you have demonstrated. If the class was defined as abstract, it should then be considered for persistance, however the reflection engine does not recognise abstract classes at present, we will add this for version 1.1

    In the meantime it is perfectly feasible to create your own base class instead of Table. Any class can be persisted, as long as it implements IPersistable. I include the code for the table class which highlights how to do this, which you can use as your own base class or adapt as necessary:


    using System;
    using Crainiate.Data.Persistence.Providers;

    namespace Crainiate.Data.Persistence
    {
        [Serializable]
        public abstract class Table: IPersistable, ITable
        {
            //Property variables
            [NonSerialized] Context _Context;
            [NonSerialized] Command _Command;
            private Concurrency _Concurrency;

            #region Interface

            //Constructors
            public Table()
            {

            }

            public Table(Context context)
            {
                Context = context;
            }

            //Properties
            public virtual Context Context
            {
                get
                {
                    return _Context;
                }
                set
                {
                    _Context = value;
                    _Command = null;
                    _Concurrency = null;
                }
            }

            public virtual Command Command
            {
                get
                {
                    return _Command;
                }
                set
                {
                    _Command = value;
                }
            }

            public virtual Concurrency Concurrency
            {
                get
                {
                    return _Concurrency;
                }
                set
                {
                    _Concurrency = value;
                }
            }

            //Methods
            public virtual void Update()
            {
                if (Context == null) throw new TableException("The object could not be updated. Set the Context before attempting to update this object.");
                if (Command == null) Command = Context.CreateCommand();
                Command.Update(this);
            }

            public virtual void Select()
            {
                if (Context == null) throw new TableException("The object could not be selected. Set the Context before attempting to select this object.");
                if (Command == null) Command = Context.CreateCommand();
                if (Concurrency == null) Concurrency = Context.CreateConcurrency();
                Command.Select(this);
            }

            public virtual void Insert()
            {
                if (Context== null) throw new TableException("The object could not be inserted. Set the Context before attempting to insert for this object.");
                if (Command == null) Command = Context.CreateCommand();
                Command.Insert(this);
            }

            public virtual void Delete()
            {
                if (Context == null) throw new TableException("The object could not be deleted. Set the Context before attempting to delete this object.");
                if (Command == null) Command = Context.CreateCommand();
                Command.Delete(this);
            }

            public virtual bool Exists()
            {
                if (Context == null) throw new TableException("The object could not be checked. Set the Context before attempting to check this object.");
                if (Command == null) Command = Context.CreateCommand();
                return Command.Exists(this);
            }

            #endregion
        }
    }





    • 81.153.80.231
  • 12-23-2006 5:14 PM In reply to

    Re: Inheritance on Table class

    James, for some reason the strategy above didn't work for me. I still get the same error.

    This is what I've done.

    Copied the code above into a new class file changing the namespace and renaming the class to "Table2".
    My Vat table is directly inherited from Table2 so inheritance looks like this: Table2-->Vat

    Trying to deploy Vat class raises the same exception.
    • 82.45.255.253
  • 12-23-2006 5:51 PM In reply to

    Re: Inheritance on Table class

    Does the VAT class have a PrimaryKey or CompoundKey column defined? If you are still seeing a problem, please feel free to send me your Vat class code and Ill diagnose the problem here.
    • 81.153.80.231
  • 12-23-2006 6:28 PM In reply to

    Re: Inheritance on Table class

    MyTable is implemented as above and Vat class implementation is as follows:

        public class Vat : MyTable
            {

            public Vat()
                {
                }

            private int _VatID;
            private string _Description;
            private decimal _Value;

            [PrimaryKey(1,1)]
            public int VatID
                {
                get
                    {
                    return _VatID;
                    }
                set
                    {
                    _VatID = value;
                    }
                }

            [Column, MaximumLength(30)]
            public string Description
                {
                get
                    {
                    return _Description;
                    }
                set
                    {
                    _Description = value;
                    }
                }

            [Column, Fixed( 18, 2 )]
            public decimal Value
                {
                get
                    {
                    return _Value;
                    }
                set
                    {
                    _Value = value;
                    }
                }

            }
    • 82.45.255.253
  • 12-24-2006 12:49 AM In reply to

    Re: Inheritance on Table class

    I've got some findings that might help to figure out whats going on:

    - If I change my Vat class to inherit from the original Table class, I still get the same exception.
    - Following the exception, I've commented MyTable implementation and the exception was dissapeared.
    - I undo the comment, and viola! The exception comes back.


    • 82.45.255.253
  • 12-28-2006 12:42 PM In reply to

    Re: Inheritance on Table class

    Any news as what might be wrong?

    • 82.45.255.253
  • 12-28-2006 2:07 PM In reply to

    Re: Inheritance on Table class

    Sorry for the delay in responding to this issue whilst we took a break over the holiday season.

    The issue is to do with the way in which abstract classes are processed. A new build is now available from the following link:

    http://support.crainiate.net/Forums/forums/176/ShowPost.aspx

    This build will allow you to use either of the methods mentioned in this thread. Abstract classes that inherit from a non-abstract persisted class will be supported in version 1.1.
    • 81.153.80.231
  • 12-28-2006 4:54 PM In reply to

    Re: Inheritance on Table class

    Thats perfect! Thanks.
    • 82.45.255.253
Page 1 of 1 (9 items)