0

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

Pete
  • 57,112
  • 28
  • 117
  • 166
  • I dont think this is a dependency injection question, I think it has more to do with your IOC container supports this feature. Unless I am misunderstanding – maccettura Mar 23 '18 at 16:45
  • Ah ok let me change the tags when I figure out which one we are using – Pete Mar 23 '18 at 16:47
  • I am wondering why couldn't you make the base helper class abstract and derive the multiple type where you only need to override the 2 string properties in them. no ? – Franck Mar 23 '18 at 17:02

0 Answers0