DRY原理与控制器方法



我在MVC5项目的几个方法中有这个逻辑。这很有效,但我一直在重复自己的话。

private PersonnelManagementEntities db = new PersonnelManagementEntities();
        private ActiveDirectoryTools adt = new ActiveDirectoryTools();
        private ManagerService ms = new ManagerService();
        private UserService us = new UserService();
        private CompanyService cs = new CompanyService();
public ActionResult CompanySummary(int id = 0)
        {
            //Repeating logic begins here
            int setID = 0;
            adt.GetUserInfo(User.Identity.Name);
            //Fetch current users company
            User currentUser = us.getUser(adt.adUserName);
            //Determine if user is a global manager or not. If global display all companies
            ViewBag.Company = cs.FetchCompanies(currentUser);
            //You can only see the companies you're assigned to, in the AllRequests window. Unless manually overwritten in the URL
            if (currentUser.GlobalUser == true && id > 0)
            {
                setID = id;
            }
            else
            {
                setID = (int)currentUser.CompanyID;
            }
            //End of repeating logic
            var resultSet = db.UserTimeSummaryUpdated(setID);
            return View(resultSet.ToList());
        }

你们觉得减少我重复这句话的次数的最好方法是什么?

你可以在这里看到我重用这段代码的另一个方法:

 public ActionResult AllRequests(int id = 0)
        {
            int setID = 0;
            adt.GetUserInfo(User.Identity.Name);
            User currentUser = us.getUser(adt.adUserName);
            ViewBag.Company = cs.FetchCompanies(currentUser);
            //You can only see the companies you're assigned to, in the AllRequests window. Unless manually overwritten in the URL
            if (id > 0)
            {
                setID = id;
            }
            else
            {
                setID = (int)currentUser.CompanyID;
            }
            ViewBag.EmployeeList = db.Users
                                     .Where(x => x.disabled == false)
                                     .Where(x => x.CompanyID == setID)
                                     .OrderBy(x => x.FullName)
                                     .ToList();
            IQueryable timeRequests = db.TimeRequests
                                 .Include(t => t.ApproveDenyReason)
                                 .Include(t => t.DayType)
                                 .Include(t => t.User)
                                 .Include(t => t.User1)
                                 .Include(t => t.User2)
                                 .OrderByDescending(t => t.sDateTime)
                                 .Where(t => t.User.CompanyID == setID);
            return View(timeRequests);
        }

我正在考虑创建一个ActionFilter并这样做,但它似乎是一种hack而不是正确的做事方式。我还考虑过这样一个想法:当用户登录时,我创建一个用户对象,并在会话中持久化它。

一个选择是编写一个继承Controller的CustomController。我这样做是为了添加成员会话数据和一个可以写入LayoutView的消息输出系统。对于下面的例子,我假设FetchCompanies返回一个列表…

public class CustomController : Controller
{
    private ActiveDirectoryTools _adt = new ActiveDirectoryTools();
    private UserService _us = new UserService();
    private CompanyService _cs = new CompanyService();
    public List<Company> UserCompanies;
    public ApplicationController()
        : base()
    { 
     _adt.GetUserInfo(User.Identity.Name);
     User currentUser = _us.getUser(adt.adUserName);
     UserCompanies = _cs.FetchCompanies(currentUser);
    }
}

当你创建你的控制器继承自这个CustomController。然后在ActionResult中使用UserCompanies.

public AccountController:CustomController
{
    public ActionResult Index()
    {
        ViewBag.Company = UserCompanies;
        return View();
    }
}

最新更新