2013-03-22 99 views
2

我試圖創建必須聲明爲靜態類MVC HTML輔助擴展,這樣的事情:ServiceStack 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...); 
    } 
} 

現在,我想用IPhotoService和IGalleryService裏面那Photo()方法。到目前爲止,唯一的辦法,我發現如何注入這些服務,內AppHost.Configure():

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

這工作,雖然我好奇是否有一些更好的方式來實現這一目標。

IPhotoServiceIGalleryService都是在AppHost.Configure()中註冊的標準方式。

感謝,安東尼

回答

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