C
C#11mo ago
ohm

ConnectionStrings passed from class to class

So, how does it work? from here, the connectionstring is obtained without problems: https://paste.mod.gg/nrvllbxtcqrq/0 but when this method is called from another method from here https://paste.mod.gg/xccaugdrlvfw/0 it does not get the connectionstring, even just the processGameJson class when it's run it cannot get the connection string by itself, why?
BlazeBin - nrvllbxtcqrq
A tool for sharing your source code with the world!
BlazeBin - xccaugdrlvfw
A tool for sharing your source code with the world!
7 Replies
Omnissiah
Omnissiah11mo ago
uhm first doesn't work (also are you not using a DbContext?)
z0mb
z0mb11mo ago
Not a direct answer to your question, but might I suggest just using dependency injection to inject an IDbConnection into the class? Much cleaner approach. Program.cs
builder.Services.AddScoped<IDbConnection>(serviceProvider =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var connectionString = configuration.GetConnectionString("db");
return new NpgsqlConnection(connectionString);
});
builder.Services.AddScoped<IDbConnection>(serviceProvider =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var connectionString = configuration.GetConnectionString("db");
return new NpgsqlConnection(connectionString);
});
Also as a side note, for your query I recommend using actual parameters to protect against any sort of SQL injection, or other mishaps. https://www.npgsql.org/doc/basic-usage.html#parameters
cap5lut
cap5lut11mo ago
can u show how u register ur services to the container? they dont use ef core, but a plain old ado.net driver
ohm
ohmOP11mo ago
yeah sure!
public static class DepedencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
services.AddScoped<IGamesRepo, GamesRepo>();
services.AddScoped<IPlayersRepo, PlayersRepo>();
services.AddScoped<ITeamsRepo, TeamsRepo>();
services.AddScoped<IProcessGameJson, ProcessGameJson>();

return services;
}
}
public static class DepedencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
services.AddScoped<IGamesRepo, GamesRepo>();
services.AddScoped<IPlayersRepo, PlayersRepo>();
services.AddScoped<ITeamsRepo, TeamsRepo>();
services.AddScoped<IProcessGameJson, ProcessGameJson>();

return services;
}
}
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddScoped<IGameService, GameService>();
services.AddScoped<IPlayerService, PlayerService>();
services.AddScoped<ITeamService, TeamService>();
services.AddScoped<IProcessGameJsonService, ProcessGameJsonService>();

return services;
}
}
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddScoped<IGameService, GameService>();
services.AddScoped<IPlayerService, PlayerService>();
services.AddScoped<ITeamService, TeamService>();
services.AddScoped<IProcessGameJsonService, ProcessGameJsonService>();

return services;
}
}
not really sure what i'm doing tbh, just have to do this thing for an internship and it's just a mere prototype, so even if it doesn't have any security check it's fine actually it's the first one that works ahah, when it's called from another program if i'm running just the repos through swagger they all register fine the moment i'm trying to call them through the ProcessGameJson all hell breaks loose
z0mb
z0mb11mo ago
what .net core version are you using? I assume you have a Program.cs file, where you are doing something like builder.Services.AddApplication() based off of the extensions you provided. So to expand upon my original point in that Program.cs add the code I provided:
builder.Services.AddScoped<IDbConnection>(serviceProvider =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var connectionString = configuration.GetConnectionString("db");
return new NpgsqlConnection(connectionString);
});
builder.Services.AddScoped<IDbConnection>(serviceProvider =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var connectionString = configuration.GetConnectionString("db");
return new NpgsqlConnection(connectionString);
});
Then go back to your PlayersRepo class and rework it to something like this:
internal class PlayersRepo : IPlayersRepo
{
private readonly ILogger<PlayersRepo> _logger;
private readonly IDbConnection _connection;

public PlayersRepo(ILogger<PlayersRepo> logger,
IDbConnection connection)
{
_logger = logger;
_connection = connection
}

public async Task InsertPlayerAsync(Player player)
{
const string query = @"
INSERT INTO public.players(
nickname,
first_name,
last_name)
VALUES(
@Nickname,
@FirstName,
@LastName);
";

using (var cmd = new NpgsqlCommand(query, _connection))
{
cmd.Parameters.AddWithValue("Nickname", player.Nickname);
cmd.Parameters.AddWithValue("FirstName", player.FirstName);
cmd.Parameters.AddWithValue("LastName", player.LastName);

await cmd.ExecuteNonQueryAsync();
}

}
}
internal class PlayersRepo : IPlayersRepo
{
private readonly ILogger<PlayersRepo> _logger;
private readonly IDbConnection _connection;

public PlayersRepo(ILogger<PlayersRepo> logger,
IDbConnection connection)
{
_logger = logger;
_connection = connection
}

public async Task InsertPlayerAsync(Player player)
{
const string query = @"
INSERT INTO public.players(
nickname,
first_name,
last_name)
VALUES(
@Nickname,
@FirstName,
@LastName);
";

using (var cmd = new NpgsqlCommand(query, _connection))
{
cmd.Parameters.AddWithValue("Nickname", player.Nickname);
cmd.Parameters.AddWithValue("FirstName", player.FirstName);
cmd.Parameters.AddWithValue("LastName", player.LastName);

await cmd.ExecuteNonQueryAsync();
}

}
}
also note: you are using an internal class. be sure that where you inject/use this player repo is in the same assembly. If you wish to use it any other way, consider using a public class for your player repo One final callout: In your ProcessGameJson class you are passing in a new player to the InsertPlayerAsync method, but you only populated the ID property when you passed that object in, that will not work well with the way the InsertPlayerAsync method is setup.
if (existingPlayer == null)
{
Player newPlayer = new Player { Id = playerId }; // You WILL need to adjust this with the appropriate properties and values to save to the DB for the player.
await _playersRepo.InsertPlayerAsync(newPlayer);
}
if (existingPlayer == null)
{
Player newPlayer = new Player { Id = playerId }; // You WILL need to adjust this with the appropriate properties and values to save to the DB for the player.
await _playersRepo.InsertPlayerAsync(newPlayer);
}
cap5lut
cap5lut10mo ago
it does not get the connectionstring
by that i assume that u get null, tbh i dont see anything that would cause that difference actually, explain exactly how/what u observe, maybe its hidden somewhere there
Omnissiah
Omnissiah10mo ago
i meant the pastebin links but apparently now i can open them
Want results from more Discord servers?
Add your server