简体   繁体   English

重构WPF代码

[英]Refactoring WPF code behind

I have a method called get Data which executes my SQL and returns some rows of ContactLists containing Aggregated Labels.At the moment this method is in my code behind and would like to move it to a separate Data Access class. 我有一个名为get Data的方法,该方法执行我的SQL并返回包含聚合标签的ContactLists的某些行。此刻此方法在我的代码中,现在想将其移动到单独的Data Access类中。 I would appreciate your assistance. 多谢您的协助。 Thanks! 谢谢!

Is normal, if i understand your code, you do this operation after ContactList initialization: 正常,如果我理解您的代码,则在ContactList初始化后执行此操作:

contactList.Labels = new ObservableCollection<Label>()
{ 
   new Label() { 
      Name = dr["LABEL_NAME"].ToString(), 
       Count = Convert.ToInt32(dr["LABEL_COUNT"]) 
   } 
};

For each ContactList is always added one item, you will do something like this: 对于每个ContactList总是添加一个项目,您将执行以下操作:

contactList.Labels = new ObservableCollection<Label>();
foreach(var item in <yourLabelDataSource>)
    contactList.Labels.Add(new Label(...));

The solution is like this: 解决方案是这样的:

    Dictionary<int, ContactList> myContactDictionary = new Dictionary<int, ContactList>();

    using (DB2DataReader dr = command.ExecuteReader())
    {

        while (dr.Read())
        {

            int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);
            if (!myContactDictionary.ContainsKey(id))
            {

                ContactList contactList = new ContactList();

                contactList.ContactListID = id;
                contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();

                contactList.Labels = new ObservableCollection<Label>() 
                { 
                    new Label() 
                    { 
                        Name = dr["LABEL_NAME"].ToString(), 
                        Count = Convert.ToInt32(dr["LABEL_COUNT"]) 
                    } 
                };

                myContactDictionary.Add(id, contactList);
            }

            else 
            {
                //Add new label because CONTACT_LIST_ID Exists 
                ContactList contactList = myContactDictionary[id];
                contactList.Labels.Add(
                    new Label()
                        {
                            Name = dr["LABEL_NAME"].ToString(), 
                            Count = Convert.ToInt32(dr["LABEL_COUNT"])                                         
                        }
                    );
            }

        }
    }

Ben, for your last question you can use this solution: Ben,对于您的最后一个问题,您可以使用以下解决方案:

            else 
            {
                //Add new label because CONTACT_LIST_ID Exists 
                ContactList contactList = myContactDictionary[id];
                string name = dr["LABEL_NAME"].ToString();
                var label = contactList.Labels.Where(l => l.Name == name).FirstOrDefault();
                if( label != null )
                    label.Count += Convert.ToInt32(dr["LABEL_COUNT"]);
                else
                {
                    contactList.Labels.Add(
                        new Label()
                        {
                            Name = dr["LABEL_NAME"].ToString(), 
                            Count = Convert.ToInt32(dr["LABEL_COUNT"])                                         
                        }
                    );
                }

I hope this code is readable and helpfulL! 我希望这段代码可读且有用! } }

This is other response: 这是其他回应:

  1. Create and Object Model that can contain your required data: 创建和对象模型可以包含所需的数据:

     public class DataResult { public ObservableCollection<AggregatedLabel> AggregatedLabels { get; set; } public ObservableCollection<ContactList> ContactLists { get; set; } } 
  2. You can build a method that return DataResult object, in your method (GetData()), you can valorize the two different properties (AggregatedLabels and ContactsList) with your DB Result. 您可以构建一个返回DataResult对象的方法,在您的方法(GetData())中,可以使用数据库结果对两个不同的属性(AggregatedLabels和ContactsList)进行赋值。 In the and you can return DataResult Object. 在中,您可以返回DataResult对象。

A little example here: 这里有个小例子:

public DataResult GetData()
{
   DataResult result = new DataResult();
   result.AggregatedLabels = new ObservableCollection<AggregatedLabel>();
   result.ContactLists = new ObservableCollection<ContactList>();

   // Manipulate data result with your method logic like in this examle:
   foreach(var something in dbResult)
   {
       ContactList cl = new ContactList() { 
       //Binding from something
       }
       result.ContactLists.Add(cl);
   }
   return result; //return your Object Model with required Data!
}

I hope it is conceptually clear 我希望它在概念上清晰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM