1

I am trying to run non static method from static and use dependency Injection inside non static method. (I am trying to do this inside one class)

My code looks like this:

public class Tokens
{
    private IRefreshTokenRepository refreshTokenRepository;

    public Tokens(IRefreshTokenRepository refreshTokenRepository)
    {
        this.refreshTokenRepository = refreshTokenRepository;
    }

    // I understand that problem is there, but I should to write this
    //constructor because otherwise I can not use class variable in static method 
    public Tokens()
    {
    }


    public static async Task<string> GenerateJwt()
    {
        RefreshToken rf = new RefreshToken{...};

        Tokens t = new Tokens();
        t.StoreRefToken(rf);

        return JsonConvert.SerializeObject(...);
    }


    public async void StoreRefToken(RefreshToken reft)
    {
        this.refreshTokenRepository.InsertRefreshToken(reft);
        await refreshTokenRepository.SaveAsync();
    }


}

As you understand from code, when I wrote "Tokens t = new Tokens();" this code had used constructor without importing repository. What to do? Can I fix it inside single class?

Thank you

P.S. if question is silly I'm sorry

4
  • Why did you tag with java? Commented Sep 1, 2018 at 6:35
  • Because in Java dependency injection and "run non static method from static one" works in same way and I think Java developers could be answer. If I'm wrong please say why and I will remove tag. Commented Sep 1, 2018 at 6:41
  • Why using static to begin with? This appears to be an XY problem. A minimal reproducible example should help clarify the actual problem Commented Sep 1, 2018 at 6:55
  • Thank you Nkosi, I will definitely will read this article and question. Commented Sep 1, 2018 at 7:03

1 Answer 1

1

Static and Dependency Injection do not play well together.

Keep Tokens as an instance class and abstract it

public interface ITokenService {
    Task<string> GenerateJwt();
}

so that it can be injected as a dependency.

derive Tokens from the abstraction

public class Tokens: ITokensService {
    private readonly IRefreshTokenRepository refreshTokenRepository;

    public Tokens(IRefreshTokenRepository refreshTokenRepository) {
        this.refreshTokenRepository = refreshTokenRepository;
    }

    public async Task<string> GenerateJwt() {
        RefreshToken rf = new RefreshToken{...};

        await StoreRefToken(rf);

        return JsonConvert.SerializeObject(...);
    }

    private async Task StoreRefToken(RefreshToken reft) {
        this.refreshTokenRepository.InsertRefreshToken(reft);
        await refreshTokenRepository.SaveAsync();
    }
}

Now you have access to the desired members where ever it is needed as a dependency

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Nkosi. 1 question: If I made static method non static. Code will work anyway. Why I need to use Interface? Is it depend on business logic? Or it has some benefits in performance, security, etc.?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.