- Notifications
You must be signed in to change notification settings - Fork1
Exposes service factories. Exposes dependency injection modularization. Also, exposes some AOP (Aspect Oriented Programming) extensions which help registering and resolving proxies instead of concrete implementations through Microsoft built-in container with the main purpose of providing lazy loading/instantiation of resources.
License
ffernandolima/extensions-dependency-injection
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Exposes service factories.
Exposes dependency injection modularization.
Also, exposes some AOP (Aspect Oriented Programming) extensions which help registering and resolving proxies instead of concrete implementations through Microsoft built-in container with the main purpose of providing lazy loading/instantiation of resources.
| Package | NuGet |
|---|---|
| Extensions.DependencyInjection.Factories | |
| Extensions.DependencyInjection.Modules | |
| Extensions.DependencyInjection.Proxies |
It is available on Nuget.
Install-Package Extensions.DependencyInjection.Factories -Version 2.3.0Install-Package Extensions.DependencyInjection.Modules -Version 2.3.0Install-Package Extensions.DependencyInjection.Proxies -Version 2.3.0P.S.: There's no dependency among the packages. Which one has its own features.
The following code demonstrates basic usage of service factory.
publicinterfaceIAckService{}publicinterfaceIBazService{}publicinterfaceIQuxService{}publicclassAckService:IAckService{privatereadonlyobject_source;publicAckService(objectsource)=>_source=source??thrownewArgumentNullException(nameof(source));}publicclassBazService:IBazService{publicBazService(){}}publicclassQuxService:IQuxService{privatereadonlyILogger<QuxService>_logger;privatereadonlyobject_source;publicQuxService(ILogger<QuxService>logger,objectsource){_logger=logger??thrownewArgumentNullException(nameof(logger));_source=source??thrownewArgumentNullException(nameof(source));}}publicclassStartup{publicStartup(IConfigurationconfiguration){Configuration=configuration;}publicIConfigurationConfiguration{get;}publicvoidConfigureServices(IServiceCollectionservices){if(services==null){thrownewArgumentNullException(nameof(services));}Services.AddTransient<IBazService,BazService>();Services.AddServiceFactory<IBazService,BazService>();// Many ways of registering service factory:// Services.AddServiceFactory<IBazService, BazService>(() => new BazService()); // Manual instantiation// Services.AddServiceFactory<IBazService, BazService>((object[] args) => new BazService()); // Receives args// Services.AddServiceFactory<IBazService, BazService>((IServiceProvider provider, object[] args) => new BazService()); // Receives ServiceProvider and args// Services.AddServiceFactory<IBazService, BazService>((IServiceProvider provider, object[] args) => provider.GetServiceOrCreateInstance<IBazService>()); // Requires IBazService registration// Services.AddServiceFactory<IBazService, BazService>((IServiceProvider provider, object[] args) => provider.GetServiceOrCreateInstance<BazService>()); // No matter IBazService was registered or not into DI containerServices.AddServiceFactory<IAckService,AckService>((IServiceProviderprovider,object[]args)=>provider.CreateInstance<AckService>(args));// IAckService must not be registered into DI containerServices.AddServiceFactory<IQuxService,QuxService>((IServiceProviderprovider,object[]args)=>provider.CreateInstance<QuxService>(args));// IQuxService must not be registered into DI container}}publicclassDIController:Controller{privatereadonlyIServiceFactory<IBazService>_bazServiceFactory;privatereadonlyIServiceFactory<IAckService>_ackServiceFactory;privatereadonlyIServiceFactory<IQuxService>_quxServiceFactory;publicDIController(IServiceFactory<IBazService>bazServiceFactory,IServiceFactory<IAckService>ackServiceFactory,IServiceFactory<IQuxService>quxServiceFactory){_bazServiceFactory=bazServiceFactory??thrownewArgumentNullException(nameof(bazServiceFactory));_ackServiceFactory=ackServiceFactory??thrownewArgumentNullException(nameof(ackServiceFactory));_quxServiceFactory=quxServiceFactory??thrownewArgumentNullException(nameof(quxServiceFactory));}publicIActionResultGet(){varbazService=_bazServiceFactory.GetService();varackService=_bazServiceFactory.GetService(newobject());varquxService=_bazServiceFactory.GetService(newobject());returnOk();}}
The following code demonstrates basic usage of modules.
publicclassServicesModule:IModuleRegistry{publicstringModuleName=>nameof(ServicesModule);publicvoidRegistry(IServiceCollectionservices,IConfigurationconfiguration=null,ILoggerFactoryloggerFactory=null,IHostEnvironmenthostEnvironment=null){if(services==null){thrownewArgumentNullException(nameof(services));}// Register your services here.}}publicclassMessagingModule:IModuleRegistry{publicstringModuleName=>nameof(MessagingModule);publicvoidRegistry(IServiceCollectionservices,IConfigurationconfiguration=null,ILoggerFactoryloggerFactory=null,IHostEnvironmenthostEnvironment=null){if(services==null){thrownewArgumentNullException(nameof(services));}// Register your messaging services here.}}publicclassStartup{publicStartup(IConfigurationconfiguration){Configuration=configuration;}publicIConfigurationConfiguration{get;}publicvoidConfigureServices(IServiceCollectionservices){if(services==null){thrownewArgumentNullException(nameof(services));}IModuleCollectionmoduleCollection=newModuleCollection();moduleCollection.AddModule<ServicesModule>().AddModule<MessagingModule>().Registry(services,Configuration);// Configuration, LoggerFactory and HostEnvironment are optional parameters.}}
The following code demonstrates basic usage of proxies.
publicinterfaceIBarService{stringExecute();}publicinterfaceIFooService{stringExecute();}publicclassFooService:IFooService{privatereadonlyIBarService_barService;// At this moment barService is just a proxy, it wasn't instantiated yetpublicFooService(IBarServicebarService)=>_barService=barService??thrownewArgumentNullException(nameof(barService));// When invoked, it gets instantiatedpublicstringExecute()=>$"Foo-{_barService.Execute()}";}publicclassBarService:IBarService{publicstringExecute()=>$"Bar";}publicclassStartup{publicStartup(IConfigurationconfiguration){Configuration=configuration;}publicIConfigurationConfiguration{get;}publicvoidConfigureServices(IServiceCollectionservices){if(services==null){thrownewArgumentNullException(nameof(services));}// Many ways of registering services as proxies:services.AddTransientProxy<IFooService,FooService>();services.AddTransientProxy<IBarService,BarService>();// You can also provide an ImplementationFactory that will be used to create the service.// services.AddTransientProxy<IFooService, FooService>(() => new FooService(new BarService()));// services.AddTransientProxy<IBarService, BarService>(() => new BarService());// Or:// services.AddTransient<IFooService>(provider => provider.CreateProxy<IFooService, FooService>());// services.AddTransient<IBarService>(provider => provider.CreateProxy<IBarService, BarService>());// You can also provide an ImplementationFactory that will be used to create the service.// services.AddTransient<IFooService>(provider => provider.CreateProxy<IFooService, FooService>(() => new FooService(provider.GetService<IBarService>())));// services.AddTransient<IBarService>(provider => provider.CreateProxy<IBarService, BarService>(() => new BarService()));// All Lifetimes are available (Transient, Scoped and Singleton).}}publicclassFooController:Controller{privatereadonlyIFooService_fooService;// At this moment fooService is just a proxy, it wasn't instantiated yet// Also its dependency barService is just a proxy, it wasn't instantiated toopublicFooController(IFooServicefooService)=>_fooService=fooService??thrownewArgumentNullException(nameof(fooService));publicIActionResultGet(){// When invoked, it gets instantiated// barService is still a proxyvarresult=fooService.Execute();returnOk(result);}}
If you want to help with the project, feel free to open pull requests and submit issues.
If you would like to show your support for this project, then please feel free to buy me a coffee.
About
Exposes service factories. Exposes dependency injection modularization. Also, exposes some AOP (Aspect Oriented Programming) extensions which help registering and resolving proxies instead of concrete implementations through Microsoft built-in container with the main purpose of providing lazy loading/instantiation of resources.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
