2

I'm trying to create a MVC html helper extension that have to be declared as a static class, something like this:

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

Now, I want to use IPhotoService and IGalleryService inside that Photo() method. So far the only way I found how to inject these services, within AppHost.Configure():

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

This works, though I curious whether there is some better way to achieve this.

Both IPhotoService and IGalleryService are registered the standard way in AppHost.Configure().

Thanks, Antonin

tereško
  • 58,060
  • 25
  • 98
  • 150
Antonin Jelinek
  • 2,277
  • 2
  • 20
  • 25

1 Answers1

2

A bit easier to read/follow, wire them up in static constructor?

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...);
    }
}
Gavin Faux
  • 481
  • 2
  • 8