Skip to content

Commit

Permalink
💾 Feat: Basic structure for Dependency Injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Sep 20, 2024
1 parent e380d0e commit c56a8df
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using KitX_Website_Backend.Api.Modules.DataBase;
using KitX_Website_Backend.Api.Modules.Interfaces;

namespace KitX_Website_Backend.Api.Modules.Accounts;

public class AccountsManager : IManager
{
public ILogger<IManager> Logger { get; init; }

public WebApplication? App { get; init; }

public AccountsManager(ILogger<AccountsManager> logger, WebApplication app)
{
Logger = logger;

App = app;
}

public AccountsManager Initialize()
{
using var scope = App!.Services.CreateScope();

var provider = scope.ServiceProvider;

var dbConnector = provider.GetRequiredService<IConnector>();



return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using KitX_Website_Backend.Api.Modules.Accounts;
using KitX_Website_Backend.Api.Modules.Interfaces;

namespace KitX_Website_Backend.Api.Modules.DataBase;

public class AccountsModule : IModule
{
public string Name { get; init; }

public string Description { get; init; }

public string Version { get; init; }

public AccountsModule()
{
Name = "Accounts";

Description = "Manage accounts automatically";

Version = "v0.0.1";
}

public Type? GetManagerType() => typeof(AccountsManager);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using KitX_Website_Backend.Api.Modules.Interfaces;

namespace KitX_Website_Backend.Api.Modules.DataBase;

public class DataBaseModule : IModule
{
public string Name { get; init; }

public string Description { get; init; }

public string Version { get; init; }

public DataBaseModule()
{
Name = "DataBase";

Description = "Connects to DataBase and provides methods to operate it";

Version = "v0.0.1";
}

public Type? GetManagerType() => null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace KitX_Website_Backend.Api.Modules.DataBase;

public interface IConnector
{
public ILogger<IConnector> Logger { get; init; }

public string ConnectionString { get; init; }

public bool IsConnected { get; }

public IConnector Initialize();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace KitX_Website_Backend.Api.Modules.DataBase;

public class MongoDbConnector(ILogger<IConnector> logger, string connectionString) : IConnector
{
public ILogger<IConnector> Logger { get; init; } = logger;

public string ConnectionString { get; init; } = connectionString;

private MongoClient? mongoClient;

public bool IsConnected => mongoClient is null;

public IConnector Initialize()
{
mongoClient = new(ConnectionString);

Logger.LogInformation(
"@Init: {name}, Connected: {isConnected}, Connection String: {str}",
nameof(MongoDbConnector),
IsConnected,
ConnectionString
);

return this;
}

public IMongoCollection<T>? GetCollection<T>(string dbName, string colName)
{
return mongoClient?.GetDatabase(dbName).GetCollection<T>(colName);
}

public IMongoQueryable<T> QueryCollection<T>(string dbName, string colName)
{
var collection = GetCollection<T>(dbName, colName);
return collection.AsQueryable();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace KitX_Website_Backend.Api.Modules.Interfaces;

public interface IManager
{
public ILogger<IManager> Logger { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace KitX_Website_Backend.Api.Modules.Interfaces;

public interface IModule
{
public string Name { get; init; }

public string Description { get; init; }

public string Version { get; init; }

public Type? GetManagerType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using KitX_Website_Backend.Api.Modules.Interfaces;

namespace KitX_Website_Backend.Api.Modules.Kxp;

public class KxpModule : IModule
{
public string Name { get; init; }

public string Description { get; init; }

public string Version { get; init; }

public KxpModule()
{
Name = "Kxp";

Description = "To manage KitX Extension Packages";

Version = "v0.0.1";
}

public Type? GetManagerType() => null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using KitX_Website_Backend.Api.Modules.DataBase;
using KitX_Website_Backend.Api.Modules.Interfaces;
using KitX_Website_Backend.Api.Modules.Kxp;

namespace KitX_Website_Backend.Api.Modules;

public class ModulesManager
{
public ILogger<ModulesManager> Logger { get; init; }

public List<IModule> ModulesPool { get; init; }

public ModulesManager(ILogger<ModulesManager> logger)
{
Logger = logger;

ModulesPool = [
new DataBaseModule(),
new AccountsModule(),
new KxpModule(),
];
}
}
Empty file.
7 changes: 7 additions & 0 deletions KitX Website Backend/KitX Website Backend Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using KitX_Website_Backend.Api.Modules;
using KitX_Website_Backend.Api.Modules.DataBase;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -7,6 +10,10 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddScoped<IConnector, MongoDbConnector>();

builder.Services.AddSingleton<ModulesManager>();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
15 changes: 10 additions & 5 deletions KitX Website.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KitX Website Backend", "KitX Website Backend\KitX Website Backend.csproj", "{48370848-561E-482F-AFF7-3927EF7F1F7F}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "KitX Website Backend", "KitX Website Backend", "{7F5DD801-36D3-4C19-A8DD-51EC4913A7C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KitX Website Backend Api", "KitX Website Backend\KitX Website Backend Api\KitX Website Backend Api.csproj", "{351EA9CD-EB24-47DD-91D2-205DCE86EBE4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{48370848-561E-482F-AFF7-3927EF7F1F7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48370848-561E-482F-AFF7-3927EF7F1F7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48370848-561E-482F-AFF7-3927EF7F1F7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48370848-561E-482F-AFF7-3927EF7F1F7F}.Release|Any CPU.Build.0 = Release|Any CPU
{351EA9CD-EB24-47DD-91D2-205DCE86EBE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{351EA9CD-EB24-47DD-91D2-205DCE86EBE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{351EA9CD-EB24-47DD-91D2-205DCE86EBE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{351EA9CD-EB24-47DD-91D2-205DCE86EBE4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DD4793C4-DEBC-4A00-ADDA-901D84CD7480}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{351EA9CD-EB24-47DD-91D2-205DCE86EBE4} = {7F5DD801-36D3-4C19-A8DD-51EC4913A7C4}
EndGlobalSection
EndGlobal

0 comments on commit c56a8df

Please sign in to comment.