zoukankan      html  css  js  c++  java
  • Need help with design ReadOnlyListBase (Insert, Update, Delete from ReadOnlyListBase)

    原文地址:http://forums.lhotka.net/forums/p/3166/21214.aspx

    My task is: 
    For select client, I have a modal form (frmSelectClient) with grid. Datasourse for grid is root readonly list of Clients (ReadOnlyListBase). If user can't find client in the list, he would like to add new. I create and show a modal form (frmEditClient) with editable root (BusinessBase) for add a new Client to DB. This frmEditClient can reuse also to edit existing Client. 

    My question: 
    What is the best solution to update readonly clients list on frmSelectClient, that a user can see their newly inserted clients or updated clients info. Now I make "fullrefresh" data in root readonly list of Clients, but think that is a bad approach :-( 


    Answer:
    The general answer is to use the Observer design pattern.

    Set up an Observer to watch for new items being added. The new item is, by definition, an editable root, or was saved through an editable root, which means that a Saved event is raised when the object is saved.

    The Observer handles that event, and relays the information to anyone who cares - such as your ROL.

    The ROL can then just add that one new item.

    The easiest way to do this is to use .NET events as the "Observer".

    In your editable root:

     public static event EventHandler ProjectSaved;
    
        protected static void OnProjectSaved(Project project)
        {
          if (ProjectSaved != null)
            ProjectSaved(project, EventArgs.Empty);
        }
    
        public override Project Save()
        {
          Project result = base.Save();
          OnProjectSaved(result);
          return result;
        }

    The Save() override raises the static event any time any Project object is inserted or updated.
    And in your ROL you handle that event:

     private ProjectList()
        {
          Project.ProjectSaved += new EventHandler(Project_ProjectSaved);
        }
    
        void Project_ProjectSaved(object sender, EventArgs e)
        {
          Project project = sender as Project;
          if (project != null)
          {
            IsReadOnly = false;
            try
            {
              for (int i = 0; i < this.Count -1; i++)
              {
                if (thisIdea [I].Id.Equals(project.Id))
                {
                  // item exists - update with new data
                  thisIdea [I] = new ProjectInfo(project.Id, project.Name);
                  return;
                }
              }
              // insert item
              this.Add(new ProjectInfo(project.Id, project.Name));
            }
            finally
            {
              IsReadOnly = true;
            }
          }
        }

    When the event is handled, sender is the new item. You can search the current list to see if that item already exists, and then replace it with an updated version. If it doesn't exist you simply add a new item.

  • 相关阅读:
    [工作札记]01: CS系统中分页控件的制作
    【非技术】试占新型肺炎的情况与发展趋势
    给培训学校讲解ORM框架的课件
    SAAS云平台搭建札记: (二) Linux Ubutu下.Net Core整套运行环境的搭建
    SAAS云平台搭建札记: (一) 浅论SAAS多租户自助云服务平台的产品、服务和订单
    开源三维地球GIS引擎Cesium常用功能的开发
    Asp.net管理信息系统中数据统计功能的实现
    [Thrift]学习使用Thrift
    使用Netty实现一下简单RPC
    [Redis]Redis做简单的分布式限流
  • 原文地址:https://www.cnblogs.com/eastson/p/4607606.html
Copyright © 2011-2022 走看看