ServiceStack Funq MVC html助手扩展

时间:2013-03-22 13:10:39

标签: asp.net-mvc servicestack helper funq

我正在尝试创建一个MVC html帮助扩展,必须将其声明为静态类,如下所示:

public static class PhotoExtension
{
    public static IPhotoService PhotoService { get; set; }
    public static IGalleryService GalleryService { get; set; }

    public static MvcHtmlString Photo(this HtmlHelper helper, int photoId, string typeName)
    {
         //[LOGIC GOES HERE]
         return new MvcHtmlString(..some resulting Html...);
    }
}

现在,我想在Photo()方法中使用IPhotoService和IGalleryService。到目前为止,我在AppHost.Configure()中找到了如何注入这些服务的唯一方法:

PhotoExtension.PhotoService = container.Resolve<IPhotoService>();
PhotoExtension.GalleryService = container.Resolve<IGalleryService>();

虽然我很好奇是否有更好的方法来实现这一点,但这很有效。

IPhotoServiceIGalleryService都以AppHost.Configure()的标准方式注册。

谢谢,Antonin

1 个答案:

答案 0 :(得分:2)

更容易阅读/遵循,在静态构造函数中连接它们?

using ServiceStack.WebHost.Endpoints;

public static class PhotoExtension
{
    public static IPhotoService PhotoService { get; set; }
    public static IGalleryService GalleryService { get; set; }

    static PhotoExtension()
    {
        PhotoService = EndpointHost.AppHost.TryResolve<IPhotoService>();
        GalleryService  = EndpointHost.AppHost.TryResolve<IGalleryService>();
    }

    public static MvcHtmlString Photo(this HtmlHelper helper, int photoId, string typeName)
    {
     //[LOGIC GOES HERE]
     return new MvcHtmlString(..some resulting Html...);
    }
}