https://github.com/ffernandolima/extensions-dependency-injection
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.
https://github.com/ffernandolima/extensions-dependency-injection
Last synced: 9 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/ffernandolima/extensions-dependency-injection
- Owner: ffernandolima
- License: mit
- Created: 2020-06-24T22:28:39.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-07T17:37:35.000Z (over 2 years ago)
- Last Synced: 2025-08-31T03:00:44.365Z (10 months ago)
- Language: C#
- Homepage:
- Size: 1.78 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# extensions-dependency-injection
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.
[](https://github.com/ffernandolima/extensions-dependency-injection/actions/workflows/build-and-tests.yml?branch=master)
[](https://github.com/ffernandolima/extensions-dependency-injection/actions/workflows/build-and-publish.yml?branch=master)
| Package | NuGet |
| ------- | ----- |
| Extensions.DependencyInjection.Factories | [ ](https://www.nuget.org/packages/Extensions.DependencyInjection.Factories/2.3.0) |
| Extensions.DependencyInjection.Modules | [ ](https://www.nuget.org/packages/Extensions.DependencyInjection.Modules/2.3.0) |
| Extensions.DependencyInjection.Proxies | [ ](https://www.nuget.org/packages/Extensions.DependencyInjection.Proxies/2.3.0) |
## Installation
It is available on Nuget.
```
Install-Package Extensions.DependencyInjection.Factories -Version 2.3.0
Install-Package Extensions.DependencyInjection.Modules -Version 2.3.0
Install-Package Extensions.DependencyInjection.Proxies -Version 2.3.0
```
P.S.: There's no dependency among the packages. Which one has its own features.
## Usage
The following code demonstrates basic usage of service factory.
```C#
public interface IAckService
{ }
public interface IBazService
{ }
public interface IQuxService
{ }
public class AckService : IAckService
{
private readonly object _source;
public AckService(object source) => _source = source ?? throw new ArgumentNullException(nameof(source));
}
public class BazService : IBazService
{
public BazService()
{ }
}
public class QuxService : IQuxService
{
private readonly ILogger _logger;
private readonly object _source;
public QuxService(ILogger logger, object source)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_source = source ?? throw new ArgumentNullException(nameof(source));
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
Services.AddTransient();
Services.AddServiceFactory();
// Many ways of registering service factory:
// Services.AddServiceFactory(() => new BazService()); // Manual instantiation
// Services.AddServiceFactory((object[] args) => new BazService()); // Receives args
// Services.AddServiceFactory((IServiceProvider provider, object[] args) => new BazService()); // Receives ServiceProvider and args
// Services.AddServiceFactory((IServiceProvider provider, object[] args) => provider.GetServiceOrCreateInstance()); // Requires IBazService registration
// Services.AddServiceFactory((IServiceProvider provider, object[] args) => provider.GetServiceOrCreateInstance()); // No matter IBazService was registered or not into DI container
Services.AddServiceFactory((IServiceProvider provider, object[] args) => provider.CreateInstance(args)); // IAckService must not be registered into DI container
Services.AddServiceFactory((IServiceProvider provider, object[] args) => provider.CreateInstance(args)); // IQuxService must not be registered into DI container
}
}
public class DIController : Controller
{
private readonly IServiceFactory _bazServiceFactory;
private readonly IServiceFactory _ackServiceFactory;
private readonly IServiceFactory _quxServiceFactory;
public DIController(IServiceFactory bazServiceFactory, IServiceFactory ackServiceFactory, IServiceFactory quxServiceFactory)
{
_bazServiceFactory = bazServiceFactory ?? throw new ArgumentNullException(nameof(bazServiceFactory));
_ackServiceFactory = ackServiceFactory ?? throw new ArgumentNullException(nameof(ackServiceFactory));
_quxServiceFactory = quxServiceFactory ?? throw new ArgumentNullException(nameof(quxServiceFactory));
}
public IActionResult Get()
{
var bazService = _bazServiceFactory.GetService();
var ackService = _bazServiceFactory.GetService(new object());
var quxService = _bazServiceFactory.GetService(new object());
return Ok();
}
}
```
The following code demonstrates basic usage of modules.
```C#
public class ServicesModule : IModuleRegistry
{
public string ModuleName => nameof(ServicesModule);
public void Registry(IServiceCollection services, IConfiguration configuration = null, ILoggerFactory loggerFactory = null, IHostEnvironment hostEnvironment = null)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
// Register your services here.
}
}
public class MessagingModule : IModuleRegistry
{
public string ModuleName => nameof(MessagingModule);
public void Registry(IServiceCollection services, IConfiguration configuration = null, ILoggerFactory loggerFactory = null, IHostEnvironment hostEnvironment = null)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
// Register your messaging services here.
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
IModuleCollection moduleCollection = new ModuleCollection();
moduleCollection.AddModule()
.AddModule()
.Registry(services, Configuration); // Configuration, LoggerFactory and HostEnvironment are optional parameters.
}
}
```
The following code demonstrates basic usage of proxies.
```C#
public interface IBarService
{
string Execute();
}
public interface IFooService
{
string Execute();
}
public class FooService : IFooService
{
private readonly IBarService _barService;
// At this moment barService is just a proxy, it wasn't instantiated yet
public FooService(IBarService barService) => _barService = barService ?? throw new ArgumentNullException(nameof(barService));
// When invoked, it gets instantiated
public string Execute() => $"Foo-{_barService.Execute()}";
}
public class BarService : IBarService
{
public string Execute() => $"Bar";
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
// Many ways of registering services as proxies:
services.AddTransientProxy();
services.AddTransientProxy();
// You can also provide an ImplementationFactory that will be used to create the service.
// services.AddTransientProxy(() => new FooService(new BarService()));
// services.AddTransientProxy(() => new BarService());
// Or:
// services.AddTransient(provider => provider.CreateProxy());
// services.AddTransient(provider => provider.CreateProxy());
// You can also provide an ImplementationFactory that will be used to create the service.
// services.AddTransient(provider => provider.CreateProxy(() => new FooService(provider.GetService())));
// services.AddTransient(provider => provider.CreateProxy(() => new BarService()));
// All Lifetimes are available (Transient, Scoped and Singleton).
}
}
public class FooController : Controller
{
private readonly IFooService _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 too
public FooController(IFooService fooService) => _fooService = fooService ?? throw new ArgumentNullException(nameof(fooService));
public IActionResult Get()
{
// When invoked, it gets instantiated
// barService is still a proxy
var result = fooService.Execute();
return Ok(result);
}
}
```
## Support / Contributing
If you want to help with the project, feel free to open pull requests and submit issues.
## Donate
If you would like to show your support for this project, then please feel free to buy me a coffee.
