在构造函数中使用参数实现控制器

时间:2011-03-08 10:44:11

标签: 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中移动这些值的所有初始化,并从我的控制器实现它。

但是,我想继续前进并在ApplicationController中初始化我的接口:)如果我能做到,我认为它应该是太棒了。我使用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个接口并拥有大约10-15个控制器,因此在ApplicationController中初始化它们并在其他中实现它们可能非常好。谢谢,如果问题是愚蠢的,那就很抱歉。


好的,添加:

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

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中初始化这个,我需要使用这个存储库(Home,Admin,...)。

所以,如果我能做出这样的事情:

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
    }
}

它可能非常好,但我需要在实现类

的构造函数中传递参数

3 个答案:

答案 0 :(得分:3)

看起来你可能在谈论Constructor Injection。子类型的构造函数可以调用基类型的构造函数,以便注入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();

    //...
  }
}

如果您将来打算注入大量接口,那么您可能会更好using Setter Injection

答案 1 :(得分:1)

您始终可以使用Ninject依赖项解析程序来获取实例,因此不使用_:

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
    }

你可以做到

protected readonly IModuleRepository ModuleRepository;
// same for the rest...    

public ApplicationController()
{
    this.ModuleRepository = MvcApplication.Container.Get<IModuleRepository>();
    // same for rest or your modules.
}

如果您使用的是MVC3,那将是:

protected readonly IModuleRepository ModuleRepository;

public ApplicationController()
{
    this.ModuleRepository = DependencyResolver.Current.GetService<IModuleRepository>();
    // same for rest or your modules.
}

答案 2 :(得分:1)

是的,这比第一次回答要好,因为现在我有:

public abstract class ApplicationController : Controller
{
    protected readonly IModuleRepository _moduleRepository;
    public IModuleRepository moduleRepository
    {
        get { return _moduleRepository; }
    }

    public ApplicationController()
    {
        this._moduleRepository = DependencyResolver.Current.GetService<IModuleRepository>();

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

    }
}

我不需要对控制器进行任何更改。只需实施并可以使用存储库:

public class HomeController : ApplicationController
{
    public ActionResult Index()
    {
        ViewBag.Modules = moduleRepository.GetAllModules();
        return View();
    }
}

在我的所有控制器中,我都有完整的ViewData值集合,它们传入PartialViews。很多,真棒:)!