EPR类企业管理系统

在我们现有系统基础上或全新开发,提供定制服务
为您的企业高效快速实施ERP,WMS,MES,CRM管理系统
全面管控物料仓库、销售业务、采购业务、仓库业务
生产过程、质量检验、组织架构、业务报表


定制
QQ:460-3528

开发
QQ群:3360-90194

源码
微信:136-3650-3721

如何:对非持久对象执行CRUD操作

此示例演示如何创建,读取,更新和删除非持久对象。在此示例中,静态缓存用作非持久对象的临时存储。

  1. 实现以下非持久类:

    using System;
    using System.ComponentModel;
    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Data;
    using DevExpress.ExpressApp.DC;
    using DevExpress.Persistent.Base;
    // ...
    [DomainComponent, DefaultClassOptions]
    public class NonPersistentClass : IObjectSpaceLink, INotifyPropertyChanged {
        private Int32 id;
        private String name;
        private IObjectSpace objectSpace;
        protected void RaisePropertyChanged(String propertyName) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public NonPersistentClass(Int32 id, String name)
            : base() {
            this.id = id;
            this.name = name;
        }
        public NonPersistentClass()
            : this(0, "") {
        }
        [Key]
        public Int32 ID {
            get { return id; }
            set { id = value; }
        }
        public String Name {
            get { return name; }
            set {
                if (name != value) {
                    name = value;
                    RaisePropertyChanged(nameof(Name));
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        [Browsable(false)]
        public IObjectSpace ObjectSpace {
            get { return objectSpace; }
            set { objectSpace = value; }
        }
    }
    
  2. 在C#/ VB编辑器中打开WinApplication.csWinApplication.vb),WebApplication.csWebApplication.vb)和/或MobileApplication.csMobileApplication.vb)文件。确保NonPersistentObjectSpaceProvider已在重写的CreateDefaultObjectSpaceProvider方法中注册(除了现有的XPObjectSpaceProviderEFObjectSpaceProvider之外)。该解决方案向导会自动添加该代码。请注意,如果您在较旧的XAF版本中创建了项目,则该代码可能会丢失。

    protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
        // ...
        args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
    }
    
  3. 实施以下控制器:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using DevExpress.ExpressApp;
    // ...
    public class NonPersistentClassWindowController : WindowController {
        private static List<NonPersistentClass> objectsCache;
        static NonPersistentClassWindowController() {
            objectsCache = new List<NonPersistentClass>();
            objectsCache.Add(new NonPersistentClass(1, "A"));
            objectsCache.Add(new NonPersistentClass(2, "B"));
            objectsCache.Add(new NonPersistentClass(3, "C"));
            objectsCache.Add(new NonPersistentClass(4, "D"));
            objectsCache.Add(new NonPersistentClass(5, "E"));
        }
        private void NonPersistentObjectSpace_ObjectsGetting(Object sender, ObjectsGettingEventArgs e) {
            if (e.ObjectType == typeof(NonPersistentClass)) {
                IObjectSpace objectSpace = (IObjectSpace)sender;
                BindingList<NonPersistentClass> objects = new BindingList<NonPersistentClass>();
                objects.AllowNew = true;
                objects.AllowEdit = true;
                objects.AllowRemove = true;
                foreach (NonPersistentClass obj in objectsCache) {
                    objects.Add(objectSpace.GetObject<NonPersistentClass>(obj));
                }
                e.Objects = objects;
            }
        }
        private void NonPersistentObjectSpace_ObjectByKeyGetting(object sender, ObjectByKeyGettingEventArgs e) {
            IObjectSpace objectSpace = (IObjectSpace)sender;
            foreach (Object obj in objectsCache) {
                if (obj.GetType() == e.ObjectType && Equals(objectSpace.GetKeyValue(obj), e.Key)) {
                    e.Object = objectSpace.GetObject(obj);
                    break;
                }
            }
        }
        private void NonPersistentObjectSpace_ObjectGetting(object sender, ObjectGettingEventArgs e) {
            if (e.SourceObject is IObjectSpaceLink) {
                ((IObjectSpaceLink)e.TargetObject).ObjectSpace = (IObjectSpace)sender;
            }
        }
        private void NonPersistentObjectSpace_Committing(Object sender, CancelEventArgs e) {
            IObjectSpace objectSpace = (IObjectSpace)sender;
            foreach (Object obj in objectSpace.ModifiedObjects) {
                if (obj is NonPersistentClass) {
                    if (objectSpace.IsNewObject(obj)) {
                        objectsCache.Add((NonPersistentClass)obj);
                    } else if (objectSpace.IsDeletedObject(obj)) {
                        objectsCache.Remove((NonPersistentClass)obj);
                    }
                }
            }
        }
        private void Application_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
            if (e.ObjectSpace is NonPersistentObjectSpace nonPersistentObjectSpace) {
                nonPersistentObjectSpace.ObjectsGetting  = NonPersistentObjectSpace_ObjectsGetting;
                nonPersistentObjectSpace.ObjectByKeyGetting  = NonPersistentObjectSpace_ObjectByKeyGetting;
                nonPersistentObjectSpace.ObjectGetting  = NonPersistentObjectSpace_ObjectGetting;
                nonPersistentObjectSpace.Committing  = NonPersistentObjectSpace_Committing;
            }
        }
        protected override void OnActivated() {
            base.OnActivated();
            Application.ObjectSpaceCreated  = Application_ObjectSpaceCreated;
        }
        protected override void OnDeactivated() {
            base.OnDeactivated();
            Application.ObjectSpaceCreated -= Application_ObjectSpaceCreated;
        }
        public NonPersistentClassWindowController()
             : base() {
            TargetWindowType = WindowType.Main;
        }
    }
    

    该控制器处理以下事件:

相关文章

转载保留此链接,注明出处