C# 是否使用BaseController,即使未使用所有依赖项?
我已使用以下构造函数创建了一个基本控制器:C# 是否使用BaseController,即使未使用所有依赖项?,c#,asp.net-mvc,unity-container,C#,Asp.net Mvc,Unity Container,我已使用以下构造函数创建了一个基本控制器: public BaseController(ICustomer customer, ISiteSettings siteSettings, ILogger logger, ILocalizer localizer) 我的所有控制器都在使用基础,因为它们都使用所有依赖项。现在我正在创建一个新的控制器ErrorController,它只使用siteSettings,而不使用其他依赖项 我想知道不使用BaseController作为ErrorControl
public BaseController(ICustomer customer, ISiteSettings siteSettings, ILogger logger, ILocalizer localizer)
我的所有控制器都在使用基础,因为它们都使用所有依赖项。现在我正在创建一个新的控制器ErrorController
,它只使用siteSettings
,而不使用其他依赖项
我想知道不使用BaseController
作为ErrorController
中的继承,只为SiteSettings
创建依赖项是否有意义。另一方面,如果使用BaseController
的成本不太高,我是否应该保持与其他控制器的一致性
我的感觉是,由于依赖项已经在Unity注册,因此访问基本控制器将是一个非常便宜的过程。只是一个想法。如果你不这么认为,请告诉我
BaseController代码供您查看:
public class BaseController : Controller
{
#region Private Members
/// <summary>
/// Factory to obtain customer object
/// </summary>
protected readonly ICustomer Customer;
/// <summary>
/// Site Settings
/// </summary>
protected readonly ISiteSettings SiteSettings;
/// <summary>
/// The Localization interface
/// </summary>
protected readonly ILocalizer Localizer;
/// <summary>
/// The Logger is used for exception handling
/// </summary>
protected readonly ILogger Logger;
#endregion
#region Ctor
/// <summary>
/// <para>Initializes a new instance of the <see cref="BaseController"/> class.</para>
/// <para>Parameters are injected via Unity.</para>
/// </summary>
/// <param name="customer">The customer.</param>
/// <param name="siteSettings">The site settings.</param>
/// <param name="logger">The logger.</param>
/// <param name="localizer">The localizer.</param>
public BaseController(ICustomer customer, ISiteSettings siteSettings, ILogger logger, ILocalizer localizer)
{
if (customer == null)
{
throw new ArgumentNullException("customer");
}
Customer = customer;
if (siteSettings == null)
{
throw new ArgumentNullException("siteSettings");
}
SiteSettings = siteSettings;
if (logger == null)
{
throw new ArgumentNullException("logger");
}
Logger = logger;
if (localizer == null)
{
throw new ArgumentNullException("localizer");
}
Localizer = localizer;
}
#endregion
}
公共类BaseController:控制器
{
#区域私人成员
///
///工厂获取客户对象
///
受保护的只读ICustomer客户;
///
///网站设置
///
受保护的只读ISiteSettings站点设置;
///
///本地化界面
///
受保护的只读ILocalizer定位器;
///
///记录器用于异常处理
///
受保护的只读ILogger记录器;
#端区
#区域导体
///
///初始化类的新实例。
///参数通过Unity注入。
///
///顾客。
///网站设置。
///记录器。
///航道。
公共BaseController(ICustomer客户、ISiteSettings站点设置、ILogger记录器、ILocalizer定位器)
{
如果(客户==null)
{
抛出新异常(“客户”);
}
客户=客户;
如果(siteSettings==null)
{
抛出新的ArgumentNullException(“siteSettings”);
}
SiteSettings=站点设置;
如果(记录器==null)
{
抛出新的ArgumentNullException(“记录器”);
}
记录器=记录器;
如果(定位器==null)
{
抛出新的异常(“定位器”);
}
定位器=定位器;
}
#端区
}
除非在这些依赖项上有一些毛茸茸的构造函数(不是BaseController构造函数本身),否则这不应该是一个问题。您的容器应该处理这些文件的繁重工作和生命周期。将使用BaseController
以实现一致性和易于进一步扩展。除非您在这些依赖项上有一些毛茸茸的构造函数,这应该不是问题。所以你的想法是保持一致性并使用BaseController?我也添加了BaseController代码。构造函数的代码粘贴在问题主体的上面。是的,该构造函数很好(简单),我指的是依赖项的各种构造函数。如果你遵循同样的模式,你应该是好的!