如何将这两种类似的方法重构为一个? [英] How to refactor these 2 similar methods into one?
本文介绍了如何将这两种类似的方法重构为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到一些使用'T'的样本来为不同类的泛型集合重用一个方法,但我从来没有真正理解它或理解样本。
我想知道是否可以将2种方法合并为一种,以及这样做的缺点是什么(性能方面)。 b
$ b
任何人?
[NonAction]
public List< SelectListItem> ToSelectList(IEnumerable< Department> departments,string defaultOption)
{
var items = departments.Select(d => new SelectListItem(){Text = d.Code + - + d.Description, Value = d.Id.ToString()})。ToList();
items.Insert(0,new SelectListItem(){Text = defaultOption,Value =-1});
退货项目;
}
[NonAction]
public List< SelectListItem> ToSelectList(IEnumerable< Function> functions,string defaultOption)
{
var items = functions.Select(f => new SelectListItem(){Text = f.Description,Value = f.Id.ToString )})。ToList();
items.Insert(0,new SelectListItem(){Text = defaultOption,Value =-1});
退货项目;
}
解决方案
我使用的解决方案:
用法
var departmentItems = departments.ToSelectList(d => d.Code + - + d.Description,d => d。 Id.ToString(), - );
var functionItems = customerFunctions.ToSelectList(f => f.Description,f => f.Id.ToString(), - );
with
public static class MCVExtentions
{
public static List< SelectListItem> ToSelectList< T>(此IEnumerable< T>可枚举,Func< T,字符串>文本,Func< T,字符串>值,字符串defaultOption)
{
var items = enumerable.Select(f => ; new SelectListItem(){Text = text(f),Value = value(f)})。ToList();
items.Insert(0,new SelectListItem(){Text = defaultOption,Value =-1});
退货项目;
}
}
解决方案
实现像@Grzenio建议的通用接口,你可以使用这样的通用方法:
public List< SelectListItem> ToSelectList< T>(IEnumerable< T> enumerable,Func< T,string>文本,Func< T,string>值,字符串defaultOption)
{
var items = enumerable.Select(f => new SelectListItem(){Text = text(f),Value = value(f)})。ToList();
items.Insert(0,new SelectListItem(){Text = defaultOption,Value =-1});
退货项目;
}
//使用像
t.ToSelectList(部门,d => d.Code + - + d.Description,d => ; d.Id.ToString(),default);
t.ToSelectList(functions,f => f.Description,f => f.Id.ToString(),default);
I've seen some samples of using 'T' to make a method reuseable for generic collections of different classes, but I've never really gotten into it or understood the samples.
I wonder if it would be possible to put the 2 methods below into one and what the downsides of doing this would be (performance-wise).
Anyone?
[NonAction]
public List<SelectListItem> ToSelectList(IEnumerable<Department> departments, string defaultOption)
{
var items = departments.Select(d => new SelectListItem() { Text = d.Code + " - " + d.Description, Value = d.Id.ToString() }).ToList();
items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
return items;
}
[NonAction]
public List<SelectListItem> ToSelectList(IEnumerable<Function> functions, string defaultOption)
{
var items = functions.Select(f => new SelectListItem() { Text = f.Description, Value = f.Id.ToString() }).ToList();
items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
return items;
}
SOLUTION
The solution that I used:
usage
var departmentItems = departments.ToSelectList(d => d.Code + " - " + d.Description, d => d.Id.ToString(), " - ");
var functionItems = customerFunctions.ToSelectList(f => f.Description, f => f.Id.ToString(), " - ");
with
public static class MCVExtentions
{
public static List<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> text, Func<T, string> value, string defaultOption)
{
var items = enumerable.Select(f => new SelectListItem() { Text = text(f), Value = value(f) }).ToList();
items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
return items;
}
}
解决方案
Without implementiong a common interface like @Grzenio suggested, you could use a generic method like this:
public List<SelectListItem> ToSelectList<T>(IEnumerable<T> enumerable, Func<T, string> text, Func<T, string> value, string defaultOption)
{
var items = enumerable.Select(f => new SelectListItem() { Text = text(f), Value = value(f) }).ToList();
items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
return items;
}
// use like
t.ToSelectList(departments, d => d.Code + " - " + d.Description, d => d.Id.ToString(), "default");
t.ToSelectList(functions, f => f.Description, f => f.Id.ToString(), "default");
这篇关于如何将这两种类似的方法重构为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文