【问题标题】:DRY principle with controller methods带有控制器方法的 DRY 原理
【发布时间】:2016-09-30 13:51:31
【问题描述】:

我的 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,而不是正确的做事方式。 我还考虑了当用户登录时我创建一个用户对象并通过会话持久化它的想法。 任何帮助表示赞赏

【问题讨论】:

    标签: c# asp.net-mvc dry


    【解决方案1】:

    一种选择是编写一个继承 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多