• 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.

  • 相关阅读:
    python bif 如何自学
    python萌新应知应会
    Animation
    响应式布局
    浏览器兼容
    HTML基础
    SublimeText 3 Emmet Hot Keys
    Web大前端环境搭建
    Sublime Text 运行js
    bash脚本编程基础
  • 原文地址:https://www.cnblogs.com/eastson/p/4607606.html
Copyright © 2020-2023  润新知