MhLabs.DataAccessIoC
4.0.0
dotnet add package MhLabs.DataAccessIoC --version 4.0.0
NuGet\Install-Package MhLabs.DataAccessIoC -Version 4.0.0
<PackageReference Include="MhLabs.DataAccessIoC" Version="4.0.0" />
paket add MhLabs.DataAccessIoC --version 4.0.0
#r "nuget: MhLabs.DataAccessIoC, 4.0.0"
// Install MhLabs.DataAccessIoC as a Cake Addin #addin nuget:?package=MhLabs.DataAccessIoC&version=4.0.0 // Install MhLabs.DataAccessIoC as a Cake Tool #tool nuget:?package=MhLabs.DataAccessIoC&version=4.0.0
MhLabs.DataAccessIoC
Package to help with repository pattern IoC in .NET Core WebApi projects. So far only implementation for AWS backends. Azure and GCP is to be done.
Usage:
Install-Package MhLabs.DataAccessIoc
Repository
Declarations:
AWS repository
public class YourRepository : AWSRepositoryBase<IAWSDataStoreType>, IYourRepository
where IAWSDataStoreType
is derived from IAmazonServcie
Example:
public class ProductRepository : AWSRepositoryBase<IAmazonDynamoDB>, IProductRepository {
private IDynamoDBContext _dynamoDbContext;
public ProductRepository(IAmazonDynamoDB dataAccessClient) : base(dataAccessClient)
{
// We let AWS generate resoucre names, so we override the table name here instead of using the type name of the entity
_dynamoDbContext = new DynamoDBContext(DataAccessClient);
}
protected override string AWSResourceKey => "ProductTable"; // <-- the key with which to resolve table name. Typically the name of an environment variable that holds the table name
public async Task AddAsync(Product product)
{
await _dynamoDbContext.SaveAsync(product, new DynamoDBOperationConfig { OverrideTableName = ResourceName });
}
public async Task<Product> GetAsync(string id)
{
return await _dynamoDbContext.LoadAsync<Product>(id, new DynamoDBOperationConfig { OverrideTableName = ResourceName });
}
}
Handler / Service
Decalaration:
public class YourHandlerHandler : HandlerBase<IYourRepository>
Example:
public class ProductHandler : HandlerBase<IProductRepository>
{
private readonly ProductDetailHandler _productDetailHandler;
public ProductHandler(IProductRepository repository) : base(repository)
{ }
public async Task AddProductAsync(Product product)
{
return await Repository.AddAsync(product);
}
public async Task<Product> GetProductAsync(string id)
{
return await Repository.GetAsync(id);
}
}
Dependency injection
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
[...]
var awsOptions = Configuration.GetAWSOptions();
awsOptions.Region = RegionEndpoint.GetBySystemName(Environment.GetEnvironmentVariable("AWS_DEFAULT_REGION"));
services.AddDefaultAWSOptions(awsOptions);
services.AddAWSService<IAmazonDynamoDB>();
services.AddAWSRepositories<Startup>();
services.AddHandlers<Startup>();
[...]
}
Your handler and repository gets automatically registered in the IoC container. Currently only works with Asp.Net's built in ServiceColleton.
Unit testing:
Install-Package MhLabs.DataAccessIoC.Xunit
Base class HandlerTestBase<THandler>
auto registers your handlers in an IoC container held by the base class. Additionally it creates empty mocks of every repository in the project. Mocks are set up in constructr by calling base.SetUpMockFor<IRepositoryType>()
public class ProductHandlerTest : HandlerTestBase<ProductHandler>
{
public ProductHandlerTest()
{
SetupMockFor<IProductRepository>(mock => mock.Setup(p => p.GetAsync(It.IsAny<string>())).ReturnsAsync(new Product { Id = "123" }));
}
[Fact]
public async Task GetProductAsync_Test()
{
var product = await Handler.GetProductAsync("123");
Assert.Equal("123", product.Id);
}
}
Pushing a new version
Set the Version
number in the <a href="https://github.com/mhlabs/MhLabs.DataAccessIoC/blob/master/MhLabs.DataAccessIoC/MhLabs.DataAccessIoC.csproj"> .csproj-file</a> before pushing. If an existing version is pushed the <a href="https://github.com/mhlabs/MhLabs.DataAccessIoC/actions">build will fail</a>.
Publish pre-release packages on branches to allow us to test the package without merging to master
- Create a new branch
- Update
Version
number and add-beta
postfix (can have .1, .2 etc. at the end) - Make any required changes updating the version as you go
- Test beta package in solution that uses package
- Create PR and get it reviewed
- Check if there are any changes on the branch you're merging into. If there are you need to rebase those changes into yours and check that it still builds
- As the final thing before merging update version number and remove post fix
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- awssdk.core (>= 3.7.103.8)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on MhLabs.DataAccessIoC:
Package | Downloads |
---|---|
MhLabs.DataAccessIoC.Xunit
Package Description |
|
MhLabs.NotificationChannel
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.0.0 | 4,157 | 1/2/2023 |
3.0.0 | 8,587 | 10/21/2021 |
2.0.1 | 34,274 | 11/6/2019 |
2.0.0 | 34,240 | 1/30/2018 |
1.0.9 | 20,671 | 1/4/2018 |
1.0.8 | 2,003 | 12/13/2017 |
1.0.7 | 1,272 | 12/8/2017 |
1.0.6 | 3,482 | 11/6/2017 |
1.0.5 | 1,159 | 11/6/2017 |
1.0.4 | 901 | 11/6/2017 |
1.0.3 | 1,043 | 11/6/2017 |
1.0.2 | 1,066 | 11/6/2017 |
1.0.1 | 1,048 | 11/6/2017 |
1.0.0 | 7,204 | 11/6/2017 |