Asp.net mvc 我可以从HomeController中的另一个控制器获取简单的布尔值吗?
我在我的注册控制器中有一张注册支票:Asp.net mvc 我可以从HomeController中的另一个控制器获取简单的布尔值吗?,asp.net-mvc,model-view-controller,Asp.net Mvc,Model View Controller,我在我的注册控制器中有一张注册支票: public class RegistrationController : Controller { private readonly IAmARegistrationRepository _RegistrationRepository; public RegistrationController(IAmARegistrationRepository registrationRepository) { _Registr
public class RegistrationController : Controller
{
private readonly IAmARegistrationRepository _RegistrationRepository;
public RegistrationController(IAmARegistrationRepository registrationRepository)
{
_RegistrationRepository = registrationRepository;
}
public bool IsRegistered(string userName)
{
return _RegistrationRepository.IsRegistered(userName);
}
}
如何从HomeController中检查此项?是否有方法访问ControllerBuilder以拉入当前打开的控制器(如果有)?或者至少能够使用我在Global.asax.cs中加载到ControllerBuilder中的customer controller工厂生成一个控制器?我想你想得太多了。您的存储库中已经有了可以使用的方法。为什么创建另一个控制器只是为了调用一个简单地遵从存储库方法的方法?在HomeController中创建存储库的实例,并在其上使用IsRegistered方法。许多解决方案中的一个是在BaseController中实现公共功能IsRegistered,并使HomeController和RegistrationControlle都继承自BaseController。我正在考虑将关注点分离开来最适合注册方法存在于注册控制器中,并且检查注册的单个调用不足以作为构造函数注入RegistrationRepository的理由。也许注册库应该是某个地方的单例存储库。我不会选择单例存储库。很明显,您需要在HomeController中注册数据,所以我会将其注入其中。我不认为存储库是一个很好的单例测试候选库,使其成为单例测试候选库会使测试更加困难。根据应用程序的大小,这是一个很好的依赖项注入候选库。+1我没有想到这一点,但我只会赞同tvanfosson的想法。