I have a few helper classes that are along similar lines:
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;
}
}
Where the _contoller
const and "Video Library" text are the only things that changes between the helpers. I thought about making these into properties so that I could have one centralised helper class rather than multiple:
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;
}
}
My question is in the second class I would want to make the controller and link text required when you create it so you would have to use a constructor like
new VideoLibraryFactoryHelper("VideoController", "Link Text");
Is this possible with dependency injection or would I just need to throw a null argument exception if they tried to use it without those properties populated - or how should I be doing this sort of thing?
We are using autofac for the dependency stuff