IoC和ASP.NET MVC视图

时间:2021-06-10 16:38:14

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.

有没有一种方法可以让我的IoC解决视图的依赖关系?我有自己的基于Castle Windsor的IoC解析器。我有一个IResourceService,我想在我的视图中访问以解决一些字符串。

5 个解决方案

#1


1  

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>

#2


0  

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

您可以在控制器中解析它,然后将其放在ViewModel中以获取对已解析实例的访问权限吗?

#3


0  

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.

我不使用Winsor,但我认为使用带有代码隐藏的视图时可以使用setter注入。为什么要解析视图的依赖关系?这听起来很奇怪。

#4


0  

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.

您可以使用包装器,并在该包装器内直接请求Windsor为您解析服务。那么从您的角度来看,只需使用ResourceHelper类将所有内容传递给已解析的服务?您可以像在视图中的HtmlHelper类一样使用ResourceHelper类。

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);
    }
}

#5


0  

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.

您是否愿意下载源代码,进行少量修改并使用它?如果是这样,您可以通过对Web窗体视图引擎进行一些小的更改来完成它,以便在构建管理器创建编译的页面对象之后,您可以执行属性注入。

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.

是处理创建页面实例的文件,在对viewInstance进行null检查之后,您可以要求服务定位器执行属性注入。

#1


1  

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>

#2


0  

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

您可以在控制器中解析它,然后将其放在ViewModel中以获取对已解析实例的访问权限吗?

#3


0  

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.

我不使用Winsor,但我认为使用带有代码隐藏的视图时可以使用setter注入。为什么要解析视图的依赖关系?这听起来很奇怪。

#4


0  

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.

您可以使用包装器,并在该包装器内直接请求Windsor为您解析服务。那么从您的角度来看,只需使用ResourceHelper类将所有内容传递给已解析的服务?您可以像在视图中的HtmlHelper类一样使用ResourceHelper类。

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);
    }
}

#5


0  

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.

您是否愿意下载源代码,进行少量修改并使用它?如果是这样,您可以通过对Web窗体视图引擎进行一些小的更改来完成它,以便在构建管理器创建编译的页面对象之后,您可以执行属性注入。

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.

是处理创建页面实例的文件,在对viewInstance进行null检查之后,您可以要求服务定位器执行属性注入。