guibranco / CrispyWaffle

🧰 🛠️ Crispy Waffle project - toolkit for .NET projects
https://guibranco.github.io/CrispyWaffle/
MIT License
23 stars 20 forks source link

[FEATURE] Add custom IoC Container support with `HostBuilder.UseServiceProviderFactory` method #170

Open guibranco opened 1 year ago

guibranco commented 1 year ago

Description

We must enhance our service registry to support the .NET method HostBuilder.UseServiceProviderFactory. This will involve creating our own IoC (Inversion of Control) container, following the standards and rules observed in popular libraries like Autofac, Castle Windsor, NInject, and SimpleInjector, but without introducing dependencies on these external libraries.

Problem Statement

Proposed Solution

Implementation Steps

  1. Design Custom IoC Container:

    • Develop a custom IoC container class that implements core IoC features:
      • Dependency registration
      • Service resolution
      • Lifecycle management (singleton, scoped, transient)
    • Follow best practices and design patterns used by existing libraries, such as:
      • Autofac: Modular registration and advanced lifetime scopes.
      • Castle Windsor: Dependency resolution and container configuration.
      • NInject: Contextual bindings and request-based resolution.
      • SimpleInjector: Performance-oriented registration and validation.
  2. Implement UseServiceProviderFactory:

    • Implement a service provider factory that uses the custom IoC container:

      public class CustomServiceProviderFactory : IServiceProviderFactory<CustomServiceCollection>
      {
       public CustomServiceCollection CreateBuilder(IServiceCollection services)
       {
           return new CustomServiceCollection(services);
       }
      
       public IServiceProvider CreateServiceProvider(CustomServiceCollection containerBuilder)
       {
           return new CustomServiceProvider(containerBuilder);
       }
      }
    • Update the Program.cs or Startup.cs file to use the custom service provider factory:

      public class Program
      {
       public static void Main(string[] args)
       {
           CreateHostBuilder(args).Build().Run();
       }
      
       public static IHostBuilder CreateHostBuilder(string[] args) =>
           Host.CreateDefaultBuilder(args)
               .UseServiceProviderFactory(new CustomServiceProviderFactory())
               .ConfigureWebHostDefaults(webBuilder =>
               {
                   webBuilder.UseStartup<Startup>();
               });
      }
  3. Configure Dependency Registration:

    • Implement methods for registering services with the custom IoC container. This includes handling various service lifetimes and managing dependencies:
      public class CustomServiceCollection
      {
       public void Register<TService, TImplementation>() where TImplementation : TService
       {
           // Implementation
       }
       // Other registration methods
      }
  4. Test Integration:

    • Ensure that the custom IoC container and UseServiceProviderFactory integration work as expected. Test service registration, resolution, and lifecycle management.
    • Verify compatibility with existing services and configurations.
  5. Documentation:

    • Document the new IoC container, including how to use it, its features, and how it integrates with HostBuilder.UseServiceProviderFactory.

Additional Notes

Mohammad-Haris commented 4 months ago

Hey guibranco, I would be happy to help here. Can you elaborate a bit more on "Add support to the built-in method of .NET UseServiceProviderFactory"? ServiceLocator currently works as an IoC container; do you have some kind of configuration/setting in mind, that the user can make at the start, depending on which the ServiceLocator uses current implementation or built in .NET implementation?

Mohammad-Haris commented 3 months ago

Any update on this?

guibranco commented 3 months ago

@Mohammad-Haris, not yet. I will look into this during the week!

gitauto-ai[bot] commented 3 weeks ago

Hey, I'm a bit lost here! Not sure which file I should be fixing. Could you give me a bit more to go on? Maybe add some details to the issue or drop a comment with some extra hints? Thanks!

Have feedback or need help? Feel free to email info@gitauto.ai.