代码之家  ›  专栏  ›  技术社区  ›  Mike Roosa

如何在ASP.NET MVC中使用DRY原则重构此代码?

  •  4
  • Mike Roosa  · 技术社区  · 16 年前

    ViewData["Customers"] = LoadCustomers();
    ViewData["Employees"] = LoadEmployees();
    ViewData["Statuses"] = LoadStatuses();
    etc......
    

    这里是LoadCustomers(),但LoadEmployees、loadStatus和所有其他的逻辑实际上完全相同:

    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");
        }
    

    如何才能更好地编写此代码,以便每次添加新的选择列表时都不需要新方法?

    3 回复  |  直到 16 年前
        1
  •  5
  •   John Feminella    16 年前

    这看起来可能是泛型的一个很好的候选:

    private static SelectList LoadItems<T>() where T : new, ... 
    {                                                // Add any additional interfaces
                                                     // that need to be supported by T
                                                     // for your Load method to work,
                                                     // as appropriate.
        IList<T> items;
        try
        {
            IServiceCallService scService = new ServiceCallService();
            results = scService.Get<T>();  // You'll need to replace GetCustomers() with
                                           //   a generic Get<T> method.
    
            // ...
        }
        catch         // Really needed? What are you trying to catch here? (This catches
        {             //   everything silently. I suspect this is overkill.)
            // ...
        }
    
        return new SelectList(items, "ID", "Name");
    }
    
        2
  •  0
  •   Jonathan Parker    16 年前

    你也可以尝试一种更实用的方法。

    public IList<T> Load<T>(Func<IList<T>> getList, T prependItem)
    {
        var list = getList();
        list.Insert(0, prependItem);
        return list;
    }
    

    用法:

    var prependItem = new Customer { ID = "", Name = "-- Select a Facility --" };
    ViewData["Customers"] = new SelectList(
        Load(new ServiceCallService().GetCustomers(), prependItem),
        "ID", "Name");
    

    这种方法还将列表的构造与正在构造的细节(SelectList)分离。

        3
  •  0
  •   Alexander Taran    16 年前

    我会更进一步,在控制器代码中写下:

     ViewData["Customers"] = new SelectList(
          new ServiceCallService().GetCustomers(),
          "ID","Name")
    

    <%= Html.DropDownList("Customers",
         ((SelectList)ViewData["Customers"]).Count() > 0 ? 
        "-- Select a Facility --" : "-- No Facilities on File --" ) %>
    
    推荐文章