MhLabs.DataAccessIoC 4.0.0

dotnet add package MhLabs.DataAccessIoC --version 4.0.0
NuGet\Install-Package MhLabs.DataAccessIoC -Version 4.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MhLabs.DataAccessIoC" Version="4.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MhLabs.DataAccessIoC --version 4.0.0
#r "nuget: MhLabs.DataAccessIoC, 4.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

  1. Create a new branch
  2. Update Version number and add -beta postfix (can have .1, .2 etc. at the end)
  3. Make any required changes updating the version as you go
  4. Test beta package in solution that uses package
  5. Create PR and get it reviewed
  6. 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
  7. As the final thing before merging update version number and remove post fix
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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,095 1/2/2023
3.0.0 8,535 10/21/2021
2.0.1 34,220 11/6/2019
2.0.0 34,182 1/30/2018
1.0.9 20,607 1/4/2018
1.0.8 1,947 12/13/2017
1.0.7 1,226 12/8/2017
1.0.6 3,423 11/6/2017
1.0.5 1,112 11/6/2017
1.0.4 858 11/6/2017
1.0.3 991 11/6/2017
1.0.2 1,026 11/6/2017
1.0.1 996 11/6/2017
1.0.0 7,130 11/6/2017