我正在尝试创建一个MVC html helper扩展,它必须声明为静态类,如下所示:
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>();这是有效的,尽管我好奇是否有更好的方法来实现这一点。
IPhotoService和IGalleryService都是在AppHost.Configure()中以标准方式注册的。
谢谢,安东宁
发布于 2013-05-31 06:07:43
更容易阅读/遵循,将它们连接到静态构造函数中?
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...);
}
}https://stackoverflow.com/questions/15571190
复制相似问题