-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Added DI overloads for custom config type #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), | ||
| and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| ## [1.11.0] - 2026-02-06 | ||
|
|
||
| ### Added | ||
|
|
||
| - Added two overloads for Dependency injection offering the use of a custom configuration type for Table and View repositories respectively. | ||
| - This can be useful if you want to add extra configuration to your repos and don't want to deal with the overload taking a constructor delegate. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| <!-- Directory.Build.props --> | ||
| <Project> | ||
| <PropertyGroup> | ||
| <Version>1.10.3</Version> | ||
| <Version>1.11.0</Version> | ||
| </PropertyGroup> | ||
| </Project> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/Dapper.DDD.Repository.DependencyInjection/DapperRepositoryDependencyInjection_Table.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| namespace Dapper.DDD.Repository.DependencyInjection; | ||
|
|
||
| public delegate TRepository TableRepositoryConstructorDelegate<TAggregate, out TRepository>( | ||
| IOptions<TableAggregateConfiguration<TAggregate>> options, IOptions<DefaultConfiguration>? defaultOptions, IServiceProvider provider) | ||
| where TAggregate : notnull; | ||
|
|
||
| public static partial class DapperRepositoryDependencyInjection | ||
| { | ||
| /// <summary> | ||
| /// Add a table repository for the given aggregate type to the dependency injection system. | ||
| /// You can request an ITableRepository<TAggregate, TAggregateId> through the dependency injection system afterwards. | ||
| /// </summary> | ||
| public static IServiceCollection AddTableRepository<TAggregate, TAggregateId>(this IServiceCollection services, | ||
| Action<TableAggregateConfiguration<TAggregate>> configureOptions) | ||
| where TAggregate : notnull | ||
| where TAggregateId : notnull | ||
| { | ||
| services.Configure(configureOptions); | ||
| services | ||
| .AddSingleton<ITableRepository<TAggregate, TAggregateId>, TableRepository<TAggregate, TAggregateId>>(); | ||
| return services; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add a table repository for the given aggregate type to the dependency injection system. | ||
| /// Uses a custom class and interface, allowing you to inherit from the built-in TableRepository type. | ||
| /// </summary> | ||
| /// <typeparam name="TAggregate">Type of your aggregate</typeparam> | ||
| /// <typeparam name="TAggregateId">Type of your aggregate's Id</typeparam> | ||
| /// <typeparam name="TRepositoryInterface">Interface type of your repository</typeparam> | ||
| /// <typeparam name="TRepositoryClass">Actual implementation type of your repository</typeparam> | ||
| /// <param name="configureOptions">Used to configure the repository via lambda</param> | ||
| public static IServiceCollection AddTableRepository<TAggregate, TAggregateId, TRepositoryInterface, | ||
| TRepositoryClass>(this IServiceCollection services, | ||
| Action<TableAggregateConfiguration<TAggregate>> configureOptions) | ||
| where TAggregate : notnull | ||
| where TAggregateId : notnull | ||
| where TRepositoryInterface : class | ||
| where TRepositoryClass : TableRepository<TAggregate, TAggregateId>, TRepositoryInterface | ||
| { | ||
| services.Configure(configureOptions); | ||
| services.AddSingleton<TRepositoryInterface, TRepositoryClass>(); | ||
| return services; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add a table repository for the given aggregate type to the dependency injection system. | ||
| /// Uses a custom class and interface, allowing you to inherit from the built-in TableRepository type. | ||
| /// </summary> | ||
| /// <typeparam name="TAggregate">Type of your aggregate</typeparam> | ||
| /// <typeparam name="TAggregateId">Type of your aggregate's Id</typeparam> | ||
| /// <typeparam name="TRepositoryInterface">Interface type of your repository</typeparam> | ||
| /// <typeparam name="TRepositoryClass">Actual implementation type of your repository</typeparam> | ||
| /// <typeparam name="TConfiguration"> | ||
| /// Custom type of configuration for your repository, useful if you want to append custom | ||
| /// configuration. Your repository constructor should expect this type instead of TableAggregateConfiguration< | ||
| /// TAggregate>. | ||
| /// </typeparam> | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <param name="configureOptions">Used to configure the repository via lambda</param> | ||
| public static IServiceCollection AddTableRepository<TAggregate, TAggregateId, TRepositoryInterface, | ||
| TRepositoryClass, TConfiguration>(this IServiceCollection services, | ||
| Action<TConfiguration> configureOptions) | ||
| where TAggregate : notnull | ||
| where TAggregateId : notnull | ||
| where TRepositoryInterface : class | ||
| where TRepositoryClass : TableRepository<TAggregate, TAggregateId>, TRepositoryInterface | ||
| where TConfiguration : TableAggregateConfiguration<TAggregate>, new() | ||
| { | ||
| var ctorInfo = typeof(TRepositoryClass).GetConstructor([typeof(IOptions<TConfiguration>), typeof(IOptions<DefaultConfiguration>)]); | ||
| if (ctorInfo is null || ctorInfo.GetParameters()[0].ParameterType != typeof(IOptions<TConfiguration>)) | ||
| { | ||
| throw new ArgumentException($"The constructor for {typeof(TRepositoryClass).Name} does not take IOptions<{typeof(TConfiguration).Name}> as argument!", nameof(configureOptions)); | ||
| } | ||
|
|
||
| services.Configure(configureOptions); | ||
| services.AddSingleton<TRepositoryInterface, TRepositoryClass>(); | ||
| return services; | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Add a table repository for the given aggregate type to the dependency injection system. | ||
| /// Uses a custom class and interface, allowing you to inherit from the built-in TableRepository type. | ||
| /// Takes a custom function to create the actual instance, allowing you to set any instance-specific state. | ||
| /// </summary> | ||
| /// <typeparam name="TAggregate">Type of your aggregate</typeparam> | ||
| /// <typeparam name="TAggregateId">Type of your aggregate's Id</typeparam> | ||
| /// <typeparam name="TRepositoryInterface">Interface type of your repository</typeparam> | ||
| /// <typeparam name="TRepositoryClass">Actual implementation type of your repository</typeparam> | ||
| /// <param name="configureOptions">Used to configure the repository via lambda</param> | ||
| public static IServiceCollection AddTableRepository<TAggregate, TAggregateId, TRepositoryInterface, | ||
| TRepositoryClass>(this IServiceCollection services, | ||
| Action<TableAggregateConfiguration<TAggregate>> configureOptions, | ||
| TableRepositoryConstructorDelegate<TAggregate, TRepositoryClass> constructor) | ||
| where TAggregate : notnull | ||
| where TAggregateId : notnull | ||
| where TRepositoryInterface : class | ||
| where TRepositoryClass : TableRepository<TAggregate, TAggregateId>, TRepositoryInterface | ||
| { | ||
| services.Configure(configureOptions); | ||
| services.AddSingleton<TRepositoryInterface>(provider => | ||
| { | ||
| var configuration = provider.GetRequiredService<IOptions<TableAggregateConfiguration<TAggregate>>>(); | ||
| var defaultConfiguration = provider.GetService<IOptions<DefaultConfiguration>>(); | ||
| return constructor(configuration, defaultConfiguration, provider); | ||
| }); | ||
| return services; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.