Would like to make reusable functions/methods in separate .cs files and/or in a separate library. Of course i know how to do this, my problem is i don't know how to do this in the case where i need to use dependency injected elements. For example, here is a very easy function about getting a user's property:
[Inject]
public UserManager<IdentityUser> UserManager { get; set; }
public async Task<string> GetUserId(string emailName)
{
var user = await userManager.FindByNameAsync(emailName);
if (user == null)
return null;
return user.Id;
}
This is working in every razor file/component, if (!) the component initialized. If not, the injected services also not initialized and get a null-exception error.
I don't want to rewrite/copy this code-snippet into every component where i would like to use, so i would like to create a class or library for it. What should be the right way to do it? The best thing would be if i can move these kind of functions into a separate Class Library or Razor Class Library.
UPDATE:
@Nkosi provide the prefect solution, but i would like to think forward a little bit. That previous code sample was really small, so what's about if i have 2-3-4 or more DI needed for my custom method? For example (in the Razor component):
[Inject]
public UserManager<IdentityUser> UserManager { get; set; }
[Inject]
public SignInManager<IdentityUser> SignInManager { get; init; }
[Inject]
public IJSRuntime jsRuntime { get; init; }
[Inject]
public CookieAuthenticationOptions cookieAuthenticationOptions { get; set; }
[Inject]
public IOptionsMonitor<CookieAuthenticationOptions> c_options { get; set; }
public async Task<string> GetUserWithOtherStuff(string email, string psw)
{
cookieAuthenticationOptions = c_options.Get("schema");
var user = await UserManager.FindByNameAsync(email);
var valid = await SignInManager.UserManager.CheckPasswordAsync(user, psw);
// etc..
return something;
}