Question

Is there a good way to get my IoC to resolve dependencies on views? I have my own IoC resolver based on Castle Windsor. I have a IResourceService that I would like to have access in my views to resolve some strings.

Was it helpful?

Solution

I would go for an extention method, then resolve my dependencies within that:

public static class LocalizationExtentions
{
  public static string Localize(this HtmlHelper html, string resource)
  {
    var localize = IoC.Resolve<ILocalize>();
    return localize.For(resource);
  }
}

In my view:

<h1><%= Html.Localize("MainTitle") %></h1>

OTHER TIPS

Could you resolve it in your controller, and then put it in the ViewModel to get access to the resolved instance?

I don't use Winsor, but I think it's possible with setter injection when you use views with code-behind. Why do you want to resolve dependencies on views? It sounds like a strange thing to do.

Could you use a wrapper, and inside that wrapper directly ask Windsor to resolve the service for you. Then from your view, just use the ResourceHelper class which passes everything to your resolved service? You could use the ResourceHelper class just like an HtmlHelper class in your view.

public interface IInjectionWrapper
{
    T Resolve<T>();
    object Resolve(Type service, object view);
    object Resolve(Type service);
}

public class WindsorWrapper: IInjectionWrapper
{
    private readonly static IWindsorContainer windsor;

    static WindsorWrapper()
    {
        string config = ConfigurationManager.AppSettings["WindsorConfig"];
        FileResource resource = new FileResource(config);

        windsor = new WindsorContainer(new XmlInterpreter(resource));
    }

    public T Resolve<T>()
    {
        T result = windsor.Resolve<T>();

        return result;
    }

    public object Resolve(Type service)
    {
        return windsor.Resolve(service);
    }
}

public interface IResourceService
{
    string LookupString(string key);
}

public class ResourceHelper : IResourceService
{
    private IResourceService _resources;

    public ResourceHelper()
    {
        IInjectionWrapper ioc = new WindsorWrapper();
        _resources = ioc.Reslove<IResourceService>();
    }

    public string LookupString(string key)
    {
        return _resources.LookupString(key);
    }
}

Would you be willing to download the source, do small modifications and use that? if so you could do it by some small changes to the web forms view engine, so that after the build manager has created a compiled page object, you could do property injection.

Otherwise, it gets ugly unless you create a base controller and override the action executing method and inject into the view data.


Edit

http://aspnet.codeplex.com/sourcecontrol/changeset/view/23011?projectName=aspnet#266535

Is the file that handles creating the page instances, right after the null check on viewInstance you could ask your service locator to do property injection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top