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

  • 相关阅读:
    修改linux的ssh默认端口号22的方法
    centos6 配置静态IP
    homebrew 无法安装提示不能在根目录下使用
    MySQL Replication 详解MySQL数据库设置主从同步的方法
    两台Mysql数据库数据同步实现
    MySql主从同步和延迟同步
    c#委托和事件
    C# Out和Ref区别
    MVC与三层架构
    经典数据库题
  • 原文地址:https://www.cnblogs.com/eastson/p/4607606.html
Copyright © 2020-2023  润新知