views:

300

answers:

5

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.

A: 

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

NerdFury
No, that would add a lot of redundant code. I just want to use my IResourceService to load localized strings.
Daniel A. White
Try using AutoMapper and create a custom view model that allows you to pass in your dependancies. AutoMapper will get rid of a lot of that redundant code.
Andrew Siemer
A: 

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.

Paco
A: 

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);
    }
}
NerdFury
I already have this.
Daniel A. White
A: 

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.

meandmycode
A: 

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>
Thomas Eyde