InheritanceExample - Adipa-G/ndbgate GitHub Wiki

Introduction

This demonstrates inheritance capability of the library using 3 level inheritance structure.

Details#

Top class

    [TableInfo("top_entity")]
    public class TopEntity : DefaultEntity
    {
        [ColumnInfo(ColumnType.Integer, Key = true, SubClassCommonColumn = true)]
        public int Id { get; set; }

        [ColumnInfo(ColumnType.Varchar)]
        public string SuperName { get; set; }
    }

Middle Class

    [TableInfo("middle_entity")]
    public class MiddleEntity : TopEntity
    {
        [ColumnInfo(ColumnType.Varchar)]
        public string MiddleName { get; set; }
    }

Bottom Class

    [TableInfo("bottom_entity")]
    public class BottomEntity : MiddleEntity
    {
        [ColumnInfo(ColumnType.Varchar)]
        public string SubName { get; set; }
    }

Testing Class

get the source for ExampleBase from https://github.com/Adipa-G/ndbgate/wiki/SimpleEntityExample

    public class InheritanceExample
    {
        private const int Id = 43;

        public BottomEntity CreateEntity()
        {
            BottomEntity entity = new BottomEntity();
            entity.Id = Id;
            entity.SuperName = "Super";
            entity.MiddleName = "Middle";
            entity.SubName = "Sub";
            return entity;
        }

        public void Patch(ITransaction tx) 
        {
            ICollection<Type> entityTypes = new List<Type>();
            entityTypes.Add(typeof(BottomEntity));
            tx.DbGate.PatchDataBase(tx, entityTypes, false);
        }

        public void Persist(ITransaction tx, BottomEntity entity)
        {
            entity.Persist(tx);
        }

        public BottomEntity Retrieve(ITransaction tx)
        {
            IDbCommand cmd = tx.CreateCommand();
            cmd.CommandText = "select * from bottom_entity where id = ?";

            IDbDataParameter parameter = cmd.CreateParameter();
            cmd.Parameters.Add(parameter);
            parameter.DbType = DbType.Int32;
            parameter.Value = Id;

            BottomEntity entity = null;
            IDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                entity = new BottomEntity();
                entity.Retrieve(reader, tx);
            }
            DbMgtUtility.Close(reader);
            DbMgtUtility.Close(cmd);
            return entity;
        }

        public static void DoTest()
        {
            InheritanceExample example = new InheritanceExample();
            ITransaction tx = ExampleBase.SetupDb();
            example.Patch(tx);

            BottomEntity entity = example.CreateEntity();
            example.Persist(tx, entity);

            entity = example.Retrieve(tx);
            Console.WriteLine("Entity Super Name = " + entity.SuperName);
            Console.WriteLine("Entity Middle Name = " + entity.MiddleName);
            Console.WriteLine("Entity Sub Name = " + entity.SubName);

            entity.SuperName = "Updated Super";
            entity.MiddleName = "Updated Middle";
            entity.SubName = "Updated Sub";
            example.Persist(tx, entity);

            entity = example.Retrieve(tx);
            Console.WriteLine("Entity Super Name = " + entity.SuperName);
            Console.WriteLine("Entity Middle Name = " + entity.MiddleName);
            Console.WriteLine("Entity Sub Name = " + entity.SubName);

            entity.Status = EntityStatus.Deleted;
            example.Persist(tx, entity);

            entity = example.Retrieve(tx);
            Console.WriteLine("Entity = " + entity);

            ExampleBase.CloseDb();
        }
    }
⚠️ **GitHub.com Fallback** ⚠️