简体   繁体   中英

How can I inject a service into a static method in .net core

I'm writing an html helper that will do some calculations for me. Now because it is a helper it is a static method. For example:

public class MyHelper
{
    public static HtmlString DoSomething()
    {
        //do some action and return;
    }
}

The problem I am facing is that I am calling this from the cshtml file I need to somehow calculate the file version inside this method. Typically I can call this in html with <asp-apend-verion="true"> but because I'm returning a string here it doesn't work if I just type it out.

Based on the research I've done I found out I can do the same thing with IFileVersionProvider.AddFileVersionToPath , however I can't figure out how to inject it so I can use it inside that method.

Edit:

I think I may need to provide more clear of a picture. Essentially I'm trying to make it so that when I'm debugging code, my javascript files give me full versions of the file and when in production they give me minified versions. As such, I have a modified version of the helper class I found online https://gist.github.com/mohamedmansour/cd50123f8575daba7a7f12847b12da5d to do that for me. My code is a bit different as i want some other things. One of those things is I want to still append file version. So I essentially call this from HTML file. If I just add <asp-append-verion="true"> then it doesn't work as this is parsed as a string. So I need to append the verison before I return that string. I found that I can do that via IFileVersionProvider but i need the implementation of that method in order to use it. This is my only bottleneck.

Edit 2:

My current solution is to create a static property in startup class and then inject it inside the Configure method. Then I can call it from anywhere. Wondering if there is a better solution?

I don't have enough reputation to write a comment, otherwise I'd ask more details, but I hope I can still be helpful.

If your helper class has evolved into something complex enough to require DI, then probably a static method is not a good fit. I usually write static methods for functions that do not rely on a state, ie pure functions, such as Math.Sqrt . If your method has a dependency on a class, then I would refactor it into an instance method and inject it into its consumers.

Said that, if you still want to use a static class, you could inject the dependency into the static method:

public class MyHelper
{
    public static HtmlString DoSomething(IFileVersionProvider provider, int otherParameter)
    {
        //do some action and return;
    }
}

If you don't want to inject IFileVersionProvider into the consumer class, you could do something like:

public class Consumer
{
    private readonly Func<int, HtmlString> doSomething;

    public Consumer(Func<int, HtmlString> doSomething)
    {
        this.doSomething = doSomething;
    }
}

// somewhere in the IoC container configuration
services.AddSingleton<Consumer>(
    s => new Consumer(p => MyHelper.DoSomething(s.GetService<IFileVersionProvider>(), p));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM