Skip to content

SimpleEntityExample

adipa edited this page Aug 8, 2018 · 5 revisions

Simple Example

Example is created using SQLite as the database engine. This demonstrates basic features like persisting, retrieval, change tracking

Details

Create the entity

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

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

Base class for examples

    public class ExampleBase
    {
        private static DefaultTransactionFactory _transactionFactory;

        public static ITransaction SetupDb()
        {
            try
            {
                if (_transactionFactory == null)
                {
                    var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
                    log4net.Config.XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

                    LogManager.GetLogger(typeof(ExampleBase)).Info("Starting in-memory database for unit tests");
                    _transactionFactory = new DefaultTransactionFactory(
                        () => new SQLiteConnection(
                            "Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1;foreign_keys = ON"),
                        DefaultTransactionFactory.DbSqllite);
                }
                return _transactionFactory.CreateTransaction();
            }
            catch (Exception ex)
            {
                LoggerManager.GetLogger(Assembly.GetExecutingAssembly(), typeof(ExampleBase)).Log(typeof(ExampleBase), Level.Fatal, "Exception during database startup.", ex);
                return null;
            }
        }

        
        public static void CloseDb()
        {
            try
            {
                ITransaction connection = _transactionFactory.CreateTransaction();
                connection.Close();

                _transactionFactory = null;
            }
            catch (Exception ex)
            {
                LoggerManager.GetLogger(Assembly.GetExecutingAssembly(), typeof(ExampleBase)).Log(typeof(ExampleBase), Level.Fatal, "Exception during database cleanup.", ex);
            }
        }
    }

Class to do the operation

    public class SimpleExample
    {
        private const int Id = 43;

        public SimpleEntity CreateEntity()
        {
            SimpleEntity entity = new SimpleEntity();
            entity.Id = Id;
            entity.Name = "Entity";
            return entity;
        }

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

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

        public SimpleEntity RetrieveWithQuery(ITransaction tx)
        {
            ISelectionQuery query = new SelectionQuery()
                .From(QueryFrom.EntityType(typeof(SimpleEntity)))
                .Select(QuerySelection.EntityType(typeof(SimpleEntity)));

            var result = query.ToList(tx).FirstOrDefault();
            if (result == null)
                return null;

            var retrieved = (SimpleEntity)result;
            return retrieved;
        }

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

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

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

            entity.Name = "Updated";
            example.Persist(tx, entity);

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

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

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

            ExampleBase.CloseDb();
        }
    }

Clone this wiki locally