C
C#2y ago
null

Refactor constructor to be used with DI [Answered]

Hello, Is it possible to refactor the following constructor in order to be available for DI without the need to create a new instance?
DatabaseCaller.cs
namespace WebApplication1.Database { public class DatabaseCaller : IDatabaseCaller { private readonly MyDbContext myDbContext; private readonly int someValueFromConfig; public DatabaseCaller(MyDbContext myDbContext, int someValueFromConfig) { this.myDbContext = myDbContext; this.someValueFromConfig = someValueFromConfig; } public async Task GetValueFromDb() { //query the database, code omitted await Task.CompletedTask; } } public interface IDatabaseCaller { Task GetValueFromDb(); } }
Program.cs
//... builder.Services.AddScoped<IDatabaseCaller, DatabaseCaller>();
WeatherForecastController.cs
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private readonly AppSettings appSettings; private readonly DatabaseCaller databaseCaller; private readonly MyDbContext myDbContext; public WeatherForecastController( IOptions<AppSettings> appSettings, MyDbContext myDbContext) { this.myDbContext = myDbContext; this.appSettings = appSettings.Value; this.databaseCaller = new DatabaseCaller(myDbContext, this.appSettings.MyValue); } [HttpGet(Name = "GetWeatherForecast")] public async Task GetAsync() { await databaseCaller.GetValueFromDb(); await Task.CompletedTask; } }
11 Replies
null
null2y ago
my goal is to inject the database caller, without needing to create a new instance in the WeatherForecastController but I need the ability to retrieve a value from the appsettings
Kouhai
Kouhai2y ago
Use a factory
null
null2y ago
Could you elaborate on that? Any resources or documentation?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tvde1
Tvde12y ago
this may cause issues if you
public Task<Something> GetData()
{
using var context = CreateContext();
return context.Stuff.ToListAsync();
}
public Task<Something> GetData()
{
using var context = CreateContext();
return context.Stuff.ToListAsync();
}
This could dispose Context before ToListAsync() completes and it may crash stuff
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tvde1
Tvde12y ago
yeah specifically awaiting a Task.CompletedTask is useless and adds overhead, but don't always remove async and await and return a Task directly it may cause issues
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
null
null2y ago
to be honest, the code await Task.CompletedTask is just a placeholder, not actually used.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
System.NullReference Exception Adding to a Dictionary [Answered]I'm really confused why this is happening. The dictionary in question is an attribute of a class thUpload a Stream (file) to an Azure Function that uploads it to the Blob StorageSay I wanted to upload a file from a Static Web App, so I get the file as a Stream and toss it towarDeclare method using T and where [Answered]This is some code from a reddit post i saw and there are some thing i dont understand. This is the IAsyncEnumerable taking to much proccess memory [Answered]Is there a way to free up memory after each iteration? ```cs public async Task TransferFilTrying to wrap my head around singletonsI'm confused by the techopedia definition being a "global variable" whereas wikipedia states (contraScaffolding Error - Requires a primary key to be definedHere's the class it's referring to ``` public class ImageUris { [JsonPropertyName("smAsync program executionHey, in Program.cs i'd like to execute some code after StartAsync, how can i achieve that please witProgress bar in console app to show download progressWanting to create a very simple console application (.NET 7) which downloads a few files and installHello guys, can someone help me out with this simple project? [Answered]I need to make a Running program for a user and to do that, I need to create a list of exercises. htHow to edit a sln file in visual studio 2022can't believe that i have to ask this question.... guess it comes to show another example of microso