here is my clean arch projects
so how can i configure the services without make any reference or use reflection?
i try to make a module factory in base app but it cannot access the non-shared project
public static class ModuleFactory
{
private static readonly List<IModuleBase> Modules = [];
public static void RegisterModules(IServiceCollection services)
{
foreach (var module in Modules)
{
module.ConfigureServices(services);
}
}
public static void LoadModules()
{
// ApplicationModule.Initialize
}
public static void AddModule(IModuleBase module)
{
Modules.Add(module);
}
}
here is my clean arch projects
so how can i configure the services without make any reference or use reflection?
i try to make a module factory in base app but it cannot access the non-shared project
public static class ModuleFactory
{
private static readonly List<IModuleBase> Modules = [];
public static void RegisterModules(IServiceCollection services)
{
foreach (var module in Modules)
{
module.ConfigureServices(services);
}
}
public static void LoadModules()
{
// ApplicationModule.Initialize
}
public static void AddModule(IModuleBase module)
{
Modules.Add(module);
}
}
Share
Improve this question
asked Feb 5 at 7:47
AhmadAhmad
276 bronze badges
3
|
1 Answer
Reset to default -2// Core.Interfaces
public interface IMyService
{
void Execute();
}
// Infrastructure.Services
public class MyService : IMyService
{
public void Execute()
{
// Implementation code
}
}
// API Project
public void ConfigureServices(IServiceCollection services)
{
// Scan assemblies and register services
var serviceAssembly = Assembly.Load("Infrastructure.Services");
var serviceTypes = serviceAssembly.GetTypes()
.Where(t => typeof(IMyService).IsAssignableFrom(t) && t.IsClass);
foreach (var type in serviceTypes)
{
var interfaceType = type.GetInterface(nameof(IMyService));
if (interfaceType != null)
{
services.AddTransient(interfaceType, type);
}
}
// Other service configurations
}
"Clean Architecture"
is a book title, there's no relevant code, not even any projects named "services" so it's unclear whatconfigure the services without make any reference or use reflection
means. And theRepositories
folder inRMS.EFCore
suggests you're ready to implement the BAD practice of wrapping the multi-entity Unit-of-Work that DbContext is into low-level CRUD classes. Judging from theModuleFactory
code you're trying to find and call the service registration methods in all projects. That usually requires reflection – Panagiotis Kanavos Commented Feb 5 at 8:41List<Action<IServiceCollection>>
filled with calls to static registration methods. That list can be maintained manually, or it could be generated by a code or source generator that uses reflection at compile time to find the registration methods. Now that interfaces can have static methods, you could define an egIServiceRegistration
interface with astatic IServiceCollection ConfigureServices(IServiceCollection)
method to make it easier to find the registration methods. Or you could use an attrribute – Panagiotis Kanavos Commented Feb 5 at 8:46without make any reference or use reflection?
Generally, there are two methods to use service project in the main project: project reference and dynamically load service implementations. Without project references, we have to dynamic loading those DLLs at runtime and uses reflection to instantiate types. So, the reflection is required without using project reference. – Zhi Lv Commented Feb 6 at 5:27