diff --git a/FahrzeugeMVC/Controllers/FahrzeugController.cs b/FahrzeugeMVC/Controllers/FahrzeugController.cs index 344ed90..4613c12 100644 --- a/FahrzeugeMVC/Controllers/FahrzeugController.cs +++ b/FahrzeugeMVC/Controllers/FahrzeugController.cs @@ -7,6 +7,13 @@ namespace FahrzeugeMVC.Controllers; public class FahrzeugController : Controller { + private readonly IKonfigurationsleser _konfigurationsleser; + + public FahrzeugController(IKonfigurationsleser konfigurationsleser) + { + _konfigurationsleser = konfigurationsleser; + } + public IActionResult Index(string searchTerm) { string connectionString = this.GetConnectionString(); @@ -29,7 +36,7 @@ public class FahrzeugController : Controller public string GetConnectionString() { - return "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB"; + return _konfigurationsleser.LiesDBVerebindung(); } [HttpGet] diff --git a/FahrzeugeMVC/Models/IKonfigurationsleser.cs b/FahrzeugeMVC/Models/IKonfigurationsleser.cs new file mode 100644 index 0000000..18bf15e --- /dev/null +++ b/FahrzeugeMVC/Models/IKonfigurationsleser.cs @@ -0,0 +1,6 @@ +namespace FahrzeugeMVC.Models; + +public interface IKonfigurationsleser +{ + string LiesDBVerebindung(); +} \ No newline at end of file diff --git a/FahrzeugeMVC/Models/Konfigurationsleser.cs b/FahrzeugeMVC/Models/Konfigurationsleser.cs new file mode 100644 index 0000000..c97d74c --- /dev/null +++ b/FahrzeugeMVC/Models/Konfigurationsleser.cs @@ -0,0 +1,16 @@ +namespace FahrzeugeMVC.Models; + +public class Konfigurationsleser : IKonfigurationsleser +{ + private readonly IConfiguration _configuration; + + public Konfigurationsleser(IConfiguration configuration) + { + this._configuration = configuration; + } + + public string LiesDBVerebindung() + { + return _configuration.GetConnectionString("MariaDB"); + } +} diff --git a/FahrzeugeMVC/Program.cs b/FahrzeugeMVC/Program.cs index 1510d12..26ba440 100644 --- a/FahrzeugeMVC/Program.cs +++ b/FahrzeugeMVC/Program.cs @@ -1,7 +1,10 @@ +using FahrzeugeMVC.Models; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddScoped(); var app = builder.Build(); diff --git a/FahrzeugeMVC/appsettings.Development.json b/FahrzeugeMVC/appsettings.Development.json index 0c208ae..c039b49 100644 --- a/FahrzeugeMVC/appsettings.Development.json +++ b/FahrzeugeMVC/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "MariaDB": "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB" } } diff --git a/FahrzeugeMVC/appsettings.json b/FahrzeugeMVC/appsettings.json index 10f68b8..aa894ea 100644 --- a/FahrzeugeMVC/appsettings.json +++ b/FahrzeugeMVC/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "MariaDB": "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB" + } }