【发布时间】:2009-03-25 01:41:25
【问题描述】:
我的一个控制器中有几种方法可以做到这一点:
ViewData["Customers"] = LoadCustomers();
ViewData["Employees"] = LoadEmployees();
ViewData["Statuses"] = LoadStatuses();
etc......
这里是 LoadCustomers(),但 LoadEmployees、LoadStatuses 和所有其他的逻辑几乎完全相同:
private static SelectList LoadCustomers()
{
IList<Customer> customers;
try
{
IServiceCallService scService = new ServiceCallService();
customers = scService.GetCustomers();
Customer c = new Customer
{
ID = "",
Name = "-- Select a Facility --"
};
customers.Insert(0, c);
}
catch
{
customers = new List<Customer>();
Customer c = new Customer
{
ID = "",
Name = "-- No Facilities on File --"
};
customers.Insert(0, c);
}
return new SelectList(customers, "ID", "Name");
}
我怎样才能更好地编写这段代码,这样我就不需要每次添加新的选择列表时都需要新的方法?
【问题讨论】:
标签: c# asp.net-mvc coding-style dry