C
C#7mo ago
Alex

Help with API structure

I want to create modular API structure https://timdeschryver.dev/blog/maybe-its-time-to-rethink-our-project-structure-with-dot-net-6#a-domain-driven-api like in this article. I split solution into .API and .Modules project. The problem is that I'm using MVC in my project and I don't how to apply it for MVC. For example where should I create controllers? In API project or Modules project?
Vocabify.API/
- Controllers/
Controller1.cs
Vocabify.Modules/
IModule.cs
- Module1
- Module2
Vocabify.API/
- Controllers/
Controller1.cs
Vocabify.Modules/
IModule.cs
- Module1
- Module2
Maybe it's time to rethink our project structure with .NET 6
Challenging the status quo with some thoughts on the new .NET Minimal Web API to keep code simple
23 Replies
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
in the article it's used to inherit from so that each module has RegisterModule method
public interface IModule
{
IServiceCollection RegisterModule(IServiceCollection builder);
}

public static class ModuleExtension
{
public static IServiceCollection RegisterModules(this IServiceCollection services)
{
foreach (var module in FindModules())
{
module.RegisterModule(services);
}

return services;
}

private static IEnumerable<IModule> FindModules()
{
return typeof(IModule).Assembly
.GetTypes()
.Where(p => p.IsClass && p.IsAssignableTo(typeof(IModule)))
.Select(Activator.CreateInstance)
.Cast<IModule>();
}
}
public interface IModule
{
IServiceCollection RegisterModule(IServiceCollection builder);
}

public static class ModuleExtension
{
public static IServiceCollection RegisterModules(this IServiceCollection services)
{
foreach (var module in FindModules())
{
module.RegisterModule(services);
}

return services;
}

private static IEnumerable<IModule> FindModules()
{
return typeof(IModule).Assembly
.GetTypes()
.Where(p => p.IsClass && p.IsAssignableTo(typeof(IModule)))
.Select(Activator.CreateInstance)
.Cast<IModule>();
}
}
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
no I'll have 5-10 modules at max
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
That means I just need to create static class Module1.cs that will have extension for IServiceCollection contains method AddModule1() where I register all dependencies?.
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
yes, but I use Controllers instead of Minimal API and I don't have endpoints like in the example
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
yes, I understand, but I don't know what project I should create Controller.cs, in the .Modules ClassLibrary project or ASP.NET .API project
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
Isn't it how application in .NET are created?
No description
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
I watched people create solutions in .NET and they split them into separate projects. .API, .Database, .Core and etc
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
I’m currently doing projects so that I can be hired as an intern at some company and I want to show that I can structure code, etc. I thought it's a good idea to split it for starters I can do it as one project, shouldn't have overthink it
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
that's a good advice, thank you. I got a lot of difficulties because I split my solution into separate projects and sometimes it happens that I needed "circular" reference between them and I had to create one more project and move classes there and reference it, which would be much easier if it wasn't split
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
But the application wasn't that big and I had a separate project with 1-2 classes in it
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP7mo ago
No I won't do the "repository" because I'll use EF. Last time I did it and it was over engineering as it completely resembled "service". I'll keep it simple this time
MODiX
MODiX7mo ago
TeBeCo
Embed Type
Video
Quoted by
<@689473681302224947> from #advanced (click here)
From TeBeCo
React with ❌ to remove this embed.
Want results from more Discord servers?
Add your server