如果我在我的Asp.Net Core 2.0 Web应用程序中创建一个BaseController,封装了一些常见的依赖项,它们是否仍然需要在实际的控制器中使用?
例如,默认的MVC 6 Web应用程序中的标准账户(Account)和管理(Manage)控制器。
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger _logger;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<AccountController> logger)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
}
//rest of code removed
}
public class ManageController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger _logger;
private readonly UrlEncoder _urlEncoder;
private const string AuthenicatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6";
public ManageController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<ManageController> logger,
UrlEncoder urlEncoder)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
_urlEncoder = urlEncoder;
}
// rest of code removed
}
在我正在构建的自定义Web应用程序模板中,我将Account Controller重构为三个不同的Controller:RegisterController(处理用户注册的所有内容),LoginController(处理登录和注销),以及第三个Controller负责余额。我将Manage Controller拆分为两个Controller:ManagePasswordController(与密码相关的所有内容)和UserManageController(其他所有内容)。每个Controller的依赖注入要求有很多共同点,我想把它们放在一个BaseController中。看起来像这样?
public abstract class BaseController : Controller
{
private readonly IConfiguration _config;
private readonly IEmailSender _emailSender;
private readonly ILogger _logger;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
protected BaseController(IConfiguration iconfiguration,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<ManageController> logger)
{
_config = iconfiguration;
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
}
//rest of code removed
}
但是似乎这样做没有任何作用?因为在我看来,我仍然必须注入所有东西。我可能错了(我对DI还很陌生),但BaseController应该允许我在BaseController和RegisterController之间不进行常见的DI。我错了吗?我该如何实现我想做的事情?
public class RegisterController : BaseController
{
private const string ConfirmedRegistration = "User created a new account with password.";
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger _logger;
private readonly IConfiguration _config;
public RegisterController(
IConfiguration config,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<AccountController> logger) : base(config, userManager, signInManager, emailSender, logger)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
_config = config;
}
//rest of code removed
}
更新
按照Rufo大人的建议
public abstract class BaseController : Controller
{
protected UserManager<ApplicationUser> UserManager { get; }
protected SignInManager<ApplicationUser> SignInManager { get; }
protected IConfiguration Config { get; }
protected IEmailSender EmailSender { get; }
protected ILogger AppLogger { get; }
protected BaseController(IConfiguration iconfiguration,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<ManageController> logger)
{
AppLogger = logger;
EmailSender = emailSender;
Config = iconfiguration;
SignInManager = signInManager;
UserManager = userManager;
}
}
继承的控制器
public class TestBaseController : BaseController
{
public TestBaseController() : base()
{
}
}
这不起作用。 Resharper告诉我必须在TestBaseController构造函数中添加参数到基本构造函数调用中。
此外,BaseController在.NET Core 2.0中应该从Controller或ControllerBase继承?
BaseController
要求您在构造函数调用中提供所有这些内容,那么是的。但是,您可以重构BaseController
以不需要所有这些内容。另外,为什么在BaseController
和其子类中都保存引用? - fredrik