最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to configure service implementation in Api project without adding a project references to other project in asp.net 9.0? - St

programmeradmin13浏览0评论

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
  • The question is unclear because "Clean Architecture" is a book title, there's no relevant code, not even any projects named "services" so it's unclear what configure the services without make any reference or use reflection means. And the Repositories folder in RMS.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 the ModuleFactory 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:41
  • If you know the projects at compile time you don't need anything fancy. You can use a List<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 eg IServiceRegistration interface with a static 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:46
  • 1 without 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
Add a comment  | 

1 Answer 1

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
 }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论