如何避免代码重复 [英] How can I avoid code duplication

查看:142
本文介绍了如何避免代码重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我都有以下代码,我想它写的方式,我有最少的代码行和工作做了同样的方式。我该怎么办呢?

 列表<类别及GT;所属分类=新的List<类别及GT;(); 
所属分类= Category.LoadForProject(project.ID).ToList();
名单,LT;字符串>类别=新的List<串GT;(类别);
&IList的LT;类别> currentCategories = Category.LoadForProject(project.ID).ToList();
如果(currentCategories!= NULL)
{
的foreach(在currentCategories VAR existingCategories)
{
如果(categories.Contains(existingCategories.Name))
categories.Remove(existingCategories.Name);
,否则
existingCategories.Delete(Services.UserServices.User);
}
的foreach(按类别字符串项)
{
区分类别=新类别(项目,item.ToString());
category.Project =项目;
category.Save();
}
}






 列表<串GT;优先级=新的List<串GT;(优先); 
&IList的LT;优先> currentPriorities = Priority.LoadForProject(project.ID).ToList();
如果(currentPriorities!= NULL)
{
的foreach(在currentPriorities VAR existingPriorities)
{
如果(priorities.Contains(existingPriorities.Name))
priorities.Remove(existingPriorities.Name);
,否则
existingPriorities.Delete(Services.UserServices.User);
}
的foreach(在优先字符串项)
{
优先级的优先级=新的优先级(项目,item.ToString());
priority.Project =项目;
priority.Save();
}
}


解决方案

东西像这样应该这样做:

 公开的IList< T> DoYourThing< T>(IList的< T>项目的IList< T> currentItems,工程项目),其中T:CommonBaseType 
{
如果(!currentItems = NULL)
{
的foreach (在currentItems VAR existingItem)
{
如果(items.Contains(existingItem.Name))
items.Remove(existingItem.Name);
,否则
existingItems.Delete(Services.UserServices.User);
}
的foreach(在项目串项)
{$ B $(B T)的newitem = Activator.CreateInstance(typeof运算(T),新的对象[] {项目,item.ToString()} )为T;
newItem.Project =项目;
newItem.Save();
}
}

返回currentItems;
}



然后,你可以这样调用它:

  VAR currentCategories = DoYourThing(Categories.ToList(),Category.LoadForProject(project.ID).ToList()); 
变种currentProjects = DoYourThing(Priorities.ToList(),Priority.LoadForProject(project.ID).ToList());



最后,你应该特别注意到两件事:
首先,有一个通用的条件在功能上其中T:CommonBaseType 。我假设类和项目都有一个共同的基本类型或接口,包括名称。如果没有,你应该摆脱的条件,并使用动态获取的名称。



二,我使用Activator.Create创建类为您服务。这是一个让人难以棘手的问题搞清楚,如果你不知道那招



祝你好运!


I have the follwoing code and I would like to write it in a way that I have minimum lines of code and the work is done the same way. How can I do that?

List<Category> categoryList = new List<Category>();
categoryList = Category.LoadForProject(project.ID).ToList();
List<string> categories = new List<string>(Categories);
IList<Category> currentCategories = Category.LoadForProject(project.ID).ToList();
if (currentCategories != null)
{
    foreach (var existingCategories in currentCategories)
    {
        if (categories.Contains(existingCategories.Name))
           categories.Remove(existingCategories.Name);
        else
            existingCategories.Delete(Services.UserServices.User);
    }
    foreach (string item in categories)
    {
        Category category = new Category(project, item.ToString());
        category.Project = project;
        category.Save();
   }
}


List<string> priorities = new List<string>(Priorities);
IList<Priority> currentPriorities = Priority.LoadForProject(project.ID).ToList();
if (currentPriorities != null)
{
   foreach (var existingPriorities in currentPriorities)
   {
       if (priorities.Contains(existingPriorities.Name))
           priorities.Remove(existingPriorities.Name);
       else
           existingPriorities.Delete(Services.UserServices.User);
   }
   foreach (string item in priorities)
   {
       Priority priority = new Priority(project, item.ToString());
       priority.Project = project;
       priority.Save();
   }
}

解决方案

Something like this should do it:

public IList<T> DoYourThing<T>(IList<T> items, IList<T> currentItems, Project project) where T : CommonBaseType
{
  if (currentItems != null)
  {
    foreach (var existingItem in currentItems)
    {
      if (items.Contains(existingItem.Name))
        items.Remove(existingItem.Name);
      else
        existingItems.Delete(Services.UserServices.User);
    }
    foreach (string item in items)
    {
      T newItem = Activator.CreateInstance(typeof(T), new object[] {project, item.ToString()}) as T;
      newItem.Project = project;
      newItem.Save();
    }
  }

  return currentItems;
}

Then you can call it like this:

var currentCategories = DoYourThing(Categories.ToList(), Category.LoadForProject(project.ID).ToList());
var currentProjects = DoYourThing(Priorities.ToList(), Priority.LoadForProject(project.ID).ToList());

Finally, you should note two things in particular: First, there is a generic condition on the function where T : CommonBaseType. I am assuming that Category and Project have a common base type or interface that includes Name. If not, you should get rid of the condition and use Dynamic to get at Name.

Second, I am using Activator.Create to create the class for you. This is the tricky part that makes it difficult to figure out, if you don't know that trick

Good luck!

这篇关于如何避免代码重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆