我有一些类似的帮助类:
public class VideoLibraryFactoryHelper : IVideoLibraryFactoryHelper
{
private const string _controller = "VideoLibrary";
private readonly UrlHelper _urlHelper;
public VideoLibraryFactoryHelper(UrlHelper urlHelper)
{
_urlHelper = urlHelper;
}
public BreadcrumbModel GetBreadcrumb(string name, string action, object routeValues)
{
UrlHelper urlHelper = DependencyResolver.Current.GetService<UrlHelper>();
BreadcrumbModel model = new BreadcrumbModel();
model.AddItem("Video Library", _urlHelper.Action("Index", _controller).ToLower());
if (!string.IsNullOrWhiteSpace(action) && !string.IsNullOrWhiteSpace(action))
{
string url = routeValues == null ? _urlHelper.Action(action, _controller) : urlHelper.Action(action, _controller, routeValues);
model.AddItem(name, url.ToLower());
}
return model;
}
}
_contoller
const和“视频库”文本是帮助者之间唯一发生变化的内容。我想过把它们变成属性,这样我就可以有一个集中的助手类而不是多个:
public class CoreFactoryHelper : ICoreFactoryHelper
{
private readonly UrlHelper _urlHelper;
public CoreFactoryHelper(UrlHelper urlHelper)
{
_urlHelper = urlHelper;
}
public string Controller { get; set; }
public string LinkText { get; set; }
public BreadcrumbModel GetBreadcrumb(string name, string action, object routeValues)
{
UrlHelper urlHelper = DependencyResolver.Current.GetService<UrlHelper>();
BreadcrumbModel model = new BreadcrumbModel();
model.AddItem(LinkText, _urlHelper.Action("Index", _controller).ToLower());
if (!string.IsNullOrWhiteSpace(action) && !string.IsNullOrWhiteSpace(action))
{
string url = routeValues == null ? _urlHelper.Action(action, _controller) : urlHelper.Action(action, Controller, routeValues);
model.AddItem(name, url.ToLower());
}
return model;
}
}
我的问题是在第二节课中我想创建控制器和链接文本,所以你必须使用像
这样的构造函数 new VideoLibraryFactoryHelper("VideoController", "Link Text");
这可能是依赖注入吗?或者如果他们试图在没有填充这些属性的情况下尝试使用它,我只需要抛出一个null参数异常 - 或者我应该怎么做这类事情?
我们正在使用autofac作为依赖项内容