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

  • 相关阅读:
    Asp.net 弹出对话框基类 (输出alet警告框)
    Request.ServerVariables获取环境变量
    Asp.net 字符串操作基类(安全,替换,分解等)
    asp.net 取得远程的IP地址和浏览器类型
    存储过程得到三个值
    弹出无边框网页的Javscrpt代码
    .net打包自动安装数据库!
    精妙Sql语句
    关于跨语言站点搜索结果非预期的问题
    SharePoint服务器场环境安装升级补丁的详细步骤
  • 原文地址:https://www.cnblogs.com/eastson/p/4607606.html
Copyright © 2020-2023  润新知