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.

  • 相关阅读:
    oracle基于事件的调度简单学习
    ALTER SEQUENCE
    初识oracle嵌套表
    关于索引失效的一点学习
    Oracle的job用法简单小结
    oracle中的FLASHBACK TABLE 和查询闪回的一点认识
    数据库事务的学习(二)
    电动车驱动力与行驶阻力平衡图.png
    matlab 等高线contour
    IPMSM弱磁控制策略
  • 原文地址:https://www.cnblogs.com/eastson/p/4607606.html
Copyright © 2011-2022 走看看