Skip to content

Commit

Permalink
Merge pull request #14 from p-brito/dev
Browse files Browse the repository at this point in the history
Add dynamoDb provider, add logging to mongodb
  • Loading branch information
p-brito authored Mar 18, 2021
2 parents 79c0de1 + 3c94cf2 commit 7a9b6da
Show file tree
Hide file tree
Showing 16 changed files with 1,283 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
/MagicDb/MagicDb.Core/bin/Debug/net5.0
/MagicDb/MagicDb.Core/MagicDb.Core.csproj.user
/MagicDb/MagicDb.UnitTests/bin/Debug/net5.0
/MagicDb/MagicDb.Demo/bin/Debug/net5.0
/MagicDb/MagicDb.Demo/obj
38 changes: 31 additions & 7 deletions MagicDb/MagicDb.Core/DependencyInjection/MagicDbExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using MagicDb.Core.Entities;
using Amazon.DynamoDBv2;
using MagicDb.Core.Entities;
using MagicDb.Core.Extensions;
using MagicDb.Core.Providers;

using MagicDb.Core.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

using MongoDB.Driver;

namespace MagicDb.Core.DependencyInjection
{
{
/// <summary>
/// Defines the MagicDb extensions class.
/// </summary>
Expand All @@ -25,7 +26,7 @@ public static IServiceCollection AddMagicDb(this IServiceCollection services)
{
// Validate

if(services == null)
if (services == null)
{
return null;
}
Expand All @@ -36,6 +37,7 @@ public static IServiceCollection AddMagicDb(this IServiceCollection services)
.AddOptions()
.AddOptionsSnapshot<MagicDbOptions>();

services.AddLogging();

MagicDbOptions configuration = services.BuildServiceProvider().GetRequiredService<MagicDbOptions>();

Expand All @@ -46,14 +48,18 @@ public static IServiceCollection AddMagicDb(this IServiceCollection services)
case DbProvider.MongoDb:
services.AddMongoDbProvider(configuration);
break;
case DbProvider.DynamoDb:
services.AddDynamoDbProvider(configuration);
break;

default:
break;
}

return services;
}

#endregion
#endregion Public Methods

#region Private Methods

Expand All @@ -69,6 +75,24 @@ private static IServiceCollection AddMongoDbProvider(this IServiceCollection ser
return services;
}

#endregion
private static IServiceCollection AddDynamoDbProvider(this IServiceCollection services, MagicDbOptions configuration)
{
ConfigString dynamoDbConfig = new(configuration.ConnectionString);

services.TryAddSingleton<IAmazonDynamoDB>(c =>
{
return new AmazonDynamoDBClient(
new Amazon.Runtime.BasicAWSCredentials(
dynamoDbConfig.GetValueOrDefault("accesskey"),
dynamoDbConfig.GetValueOrDefault("secretkey")),
Amazon.RegionEndpoint.GetBySystemName(dynamoDbConfig.GetValueOrDefault("region")));
});

services.TryAddTransient(typeof(IMagicDbProvider<>), typeof(DynamoDbProvider<>));

return services;
}

#endregion Private Methods
}
}
}
5 changes: 0 additions & 5 deletions MagicDb/MagicDb.Core/Entities/MagicDbOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public sealed class MagicDbOptions
/// </summary>
public string ConnectionString { get; set; }

/// <summary>
/// Gets or sets the name of the database.
/// </summary>
public string DatabaseName { get; set; }

#endregion

#region Constructor
Expand Down
7 changes: 6 additions & 1 deletion MagicDb/MagicDb.Core/Exceptions/MagicDbError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public enum MagicDbError
/// <summary>
/// An error occurred while trying to update the specified entity.
/// </summary>
ErrorUpdate
ErrorUpdate,

/// <summary>
/// An error occurred while trying to initialize the provider.
/// </summary>
ErrorInitializeProvider
}
}
20 changes: 20 additions & 0 deletions MagicDb/MagicDb.Core/Extensions/ResourceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MagicDb.Core.Extensions
{
/// <summary>
/// Defines the resource extensions class.
/// </summary>
internal static class ResourceExtensions
{
public static string Format(this string resource, string param1)
{
return string.Format(CultureInfo.InvariantCulture, resource, param1);
}
}
}
7 changes: 6 additions & 1 deletion MagicDb/MagicDb.Core/MagicDb.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageId>MagicDb</PackageId>
<Authors>Pedro Brito</Authors>
<Version>1.0.1</Version>
<Version>1.1.1</Version>
<PackageDescription>Provides a fast and easy way to implement a NoSQL db provider in your project.</PackageDescription>
<RepositoryUrl>
https://github.com/p-brito/MagicDb
</RepositoryUrl>
<UserSecretsId>50577860-c1c4-45d8-a455-58d0001c0fdc</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.5.4.35" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.12.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
Expand Down
35 changes: 31 additions & 4 deletions MagicDb/MagicDb.Core/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions MagicDb/MagicDb.Core/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,25 @@
<data name="RES_Exception_ArgCannotBeNullOrEmpty" xml:space="preserve">
<value>Argument cannot be null or empty.</value>
</data>
<data name="RES_Exception_ConfigString_MissingKey" xml:space="preserve">
<value>The {0} does not exist in the provided configuration.</value>
</data>
<data name="RES_Exception_Delete" xml:space="preserve">
<value>An error occurred while trying to delete the specified entity.</value>
<value>An error occurred while trying to delete the entity the id {0}.</value>
</data>
<data name="RES_Exception_Get" xml:space="preserve">
<value>An error occurred while trying to get the specified entity.</value>
<value>An error occurred while trying to get the the entity the id {0}.</value>
</data>
<data name="RES_Exception_Initializing_Provider" xml:space="preserve">
<value>An error occurred while trying to initialize the {0} provider.</value>
</data>
<data name="RES_Exception_Insert" xml:space="preserve">
<value>An error occurred while trying to insert the specified entity.</value>
<value>An error occurred while trying to insert the the entity the id {0}.</value>
</data>
<data name="RES_Exception_Update" xml:space="preserve">
<value>An error occurred while trying to update the specified entity.</value>
<value>An error occurred while trying to update the entity the id {0}.</value>
</data>
<data name="RES_Exception_Update_EntityDoesNotExist" xml:space="preserve">
<value>An error occurred while trying to update the specified entity. The entity with the id {0} does not exist.</value>
</data>
</root>
Loading

0 comments on commit 7a9b6da

Please sign in to comment.