いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

DevExpress奮闘記-079 (InMemoryDataStoreとXpCollection.LoadAsyncのサンプル)(LoadAsync, AsyncLoadObjectCallback, InMemoryDataStore)


以前に, InMemortyDataStoreとLoadAsyncの記事は記述したのですが
どちらもサンプルが若干ごちゃごちゃしているので、復習の意味も込めて再度サンプル
作成しました。


今回のサンプルでは、GridControlを配置して
InMemoryDataStoreとLoadAsyncを利用して非同期読み込みしています。


また、以前のサンプルのように真ん中にデカデカとラベルを表示するのではなく
EmbeddedNavigatorの部分にローディング中を表す文字列を表示するようにしています。


以下、サンプルです。

using System;
using System.Collections;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.XtraEditors;

namespace DevExpress_XPO_InMemoryDataStore
{
    public partial class Form1 : XtraForm
    {
        string _oldTextStringFormat;

        public Form1()
        {
            InitializeComponent();
            InitializeData();
        }

        void InitializeData()
        {
            CancellationToken       token      = CancellationToken.None;
            TaskContinuationOptions taskOption = TaskContinuationOptions.None;
            TaskScheduler           scheduler  = TaskScheduler.FromCurrentSynchronizationContext();

            Task.Factory.StartNew(InitializeDataLayer)
                        .ContinueWith(
                            prevTask => InitializeGridControl(),
                            token,
                            taskOption,
                            scheduler
                        )
                        .ContinueWith(prevTask => InitializeModel())
                        .ContinueWith(
                            prevTask => LoadData(),
                            token,
                            taskOption,
                            scheduler
                        );
        }

        void InitializeDataLayer()
        {
            XpoDefault.DataLayer = new SimpleDataLayer(new InMemoryDataStore());
            XpoDefault.Session.UpdateSchema(typeof(Model));
        }

        void InitializeModel()
        {
            using (UnitOfWork uow = new UnitOfWork())
            {
                for (int i = 0; i < 15000; i++)
                {
                    Model aModel = new Model(uow);

                    aModel.Id = i;
                    aModel.Name = string.Format("name-{0}", i);
                    aModel.Age = (i + 1);
                    aModel.Birthday = DateTime.Now.AddDays(i);
                }

                uow.CommitChanges();
            }
        }

        void InitializeGridControl()
        {
            _oldTextStringFormat = gridControl1.EmbeddedNavigator.TextStringFormat;

            gridControl1.EmbeddedNavigator.TextStringFormat = "Loading.....";
            gridControl1.EmbeddedNavigator.ForeColor = Color.Red;

            gridControl1.Enabled = false;
        }

        void LoadData()
        {
            xpCol.LoadAsync(xpCol_EndLoadAsync);
        }

        void xpCol_EndLoadAsync(ICollection[] result, Exception ex)
        {
            if (ex == null)
            {
                gridControl1.EmbeddedNavigator.ResetForeColor();
                gridControl1.EmbeddedNavigator.TextStringFormat = _oldTextStringFormat;
                
                gridControl1.Enabled = true;
            }
        }
        
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            DevExpress.Skins.SkinManager.EnableFormSkins();
            Application.Run(new Form1());
        }
    }
    
    public class Model : XPLiteObject
    {
        public Model() : base()
        {
        }

        public Model(Session session) : base(session)
        {
        }

        public override void AfterConstruction()
        {
            base.AfterConstruction();
            
            Id   = -1;
            Name = "None";
        }

        private int _Id;
        [Key]
        public int Id
        {
            get
            {
                return _Id;
            }
            set
            {
                SetPropertyValue("Id", ref _Id, value);
            }
        }

        private string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                SetPropertyValue("Name", ref _Name, value);
            }
        }

        private int _Age;
        public int Age
        {
            get
            {
                return _Age;
            }
            set
            {
                SetPropertyValue("Age", ref _Age, value);
            }
        }

        private DateTime _Birthday;
        public DateTime Birthday
        {
            get
            {
                return _Birthday;
            }
            set
            {
                SetPropertyValue("Birthday", ref _Birthday, value);
            }
        }
    }
}


起動すると、データがローディング中の状態で表示されます。

ローディングが完了すると、グリッドが利用可能になります。


サンプルを以下の場所にアップしてます。
試してみたい方はどうぞ。

================================
過去の記事については、以下のページからご参照下さい。