c# - 在构造函数中使用参数实现 Controller

标签 c# .net asp.net-mvc

我有很多不总是静态的值(口号、横幅、描述...)和 PartialViews(block_head、block_footer、block_right),我应该在哪里显示它。因此,我需要在每个 Action 中将这些值的大量集合传递给 Partial,这对我来说不是很好。

我在这里找到了有趣的解决方案:http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs ,部分是“好的解决方案”。我可以在 ApplicationController 中移动此值的所有初始化并从我的 Controller 中实现它。

但是,我也想继续在 ApplicationController 中初始化我的接口(interface) :) 如果我能做到,我认为它应该很棒。我使用 Ninject,所以,一些代码:

public abstract class ApplicationController : Controller
{
    //
    // GET: /Application/
    private readonly IModuleRepository _moduleRepository;
    public IModuleRepository moduleRepository
    {
        get { return _moduleRepository; }
    }

    public ApplicationController(IModuleRepository moduleRepository)
    {
        _moduleRepository = moduleRepository;

        foreach (var module in _moduleRepository.GetAllModules())
            ViewData[module.name] = module.value;
    }
}

实现:

public class HomeController : ApplicationController
{
    //I can use here moduleRepository without HomeController initialization
}

只有一个问题,如果它有参数,我不知道如何实现 ApplicationController。这是好方法吗?是否可以解决我的问题?将来我将设置 5-7 个接口(interface)并有大约 10-15 个 Controller ,因此最好在 ApplicationController 中初始化它们并在其他中实现。谢谢,抱歉,如果这个问题很愚蠢。


好的,添加:

现在,如果我有 10 个接口(interface),它应该是这样的:

public class HomeController
{
    private IModuleRepository _moduleRepository;
    private IBookRepository _bookRepository;
    private ITableRepository _tableRepository;
    private IClassRepository _classRepository;
    private IRoomRepository _roomRepository;
    private IUserRepository _userRepository;
    private IWindowRepository _windowRepository;
    private IChairRepository _chairRepository;
    private IDoorRepository _doorRepository;
    private IWCRepository _wcRepository;

    public HomeController(IModuleRepository moduleRepository, IBookRepository bookRepository, ITableRepository tableRepository, IClassRepository classRepository, IRoomRepository roomRepository, IUserRepository userRepository, IWindowRepository windowRepository, IChairRepository chairRepository, IDoorRepository doorRepository, IWCRepository wcRepository)
    {
        _moduleRepository = moduleRepository;
        _bookRepository = bookRepository;
        _tableRepository = tableRepository;
        _classRepository = classRepository;
        _roomRepository = roomRepository;
        _userRepository = userRepository;
        _windowRepository = windowRepository;
        _chairRepository = chairRepository;
        _doorRepository = doorRepository;
        _wcRepository = wcRepository;
    }

    public ActionResult Index()
    {
        ViewBag.Windows = _windowRepository.GetAllWindows();
        ViewBag.Doors = _doorRepository.GetAllDoors();
        // e.t.c.

        return View();
    }
} 

而且我必须在我的每个 Controller 中初始化它,我需要在其中使用这个存储库(主页、管理员...)。

所以,如果我能做出这样的东西:

public class HomeController : ApplicationController
{
    public ActionResult Index()
    {
        ViewBag.Windows = windowRepository.GetAllWindows();
        ViewBag.Doors = doorRepository.GetAllDoors();

        return View();
    }
}

这里只初始化一次:

public abstract class ApplicationController : Controller
{
    public ApplicationController(IModuleRepository moduleRepository, IBookRepository bookRepository, ITableRepository tableRepository, IClassRepository classRepository, IRoomRepository roomRepository, IUserRepository userRepository, IWindowRepository windowRepository, IChairRepository chairRepository, IDoorRepository doorRepository, IWCRepository wcRepository)
    {
        // Initialize repositories just one time here
    }
}

它可能很好,但我需要在实现类的构造函数中传递参数

最佳答案

看起来您可能在谈论 Constructor Injection .您的子类型的构造函数可以调用您的基类型的构造函数以注入(inject) IModuleRepository

public class HomeController : ApplicationController
{

    public HomeController(IModuleRepository moduleRepository) : base(moduleRepository)
    {
      //other constructor code here
    }

    public HomeController() : this(null)
    {
      //default constructor
    }

}


public abstract class ApplicationController : Controller
{

  public IModuleRepoistory _moduleRepository { get; private set; }

  public ApplicationController(IModuleRepository moduleRepository)
  {
    _moduleRepository = moduleRepository ?? new DefaultModuleRepository();

    //...
  }
}

如果将来你打算注入(inject)很多接口(interface),那么你可能会更好 using Setter Injection

关于c# - 在构造函数中使用参数实现 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5231365/

相关文章:

c# - 让 OnMouseDown 与 2D 多边形碰撞器一起使用

c# - .Net继承和方法重载

.net - 如何在 .csproj 文件中为 PackageReference 指定 targetFramework?

asp.net-mvc - 迁移 asp.net core Identity 以将数据保护与 SQL DB 结合使用

c# - 我如何通过 Linq 实现它?

c# - 将 C# 中的任务调度程序替换为定制的任务调度程序

c# - 如何在我的 ASP.Net MVC 5 应用程序的同一页面中编辑和删除 POST 请求

asp.net - 如何构建新的 ASP MVC 应用程序?

c# - 如何从外部文件读取 XML appSettings?

c# - 通过在 C# 中添加空字符串将对象转换为字符串