Skip to content

Commit

Permalink
chore: apply minor changes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
engineering87 committed Dec 10, 2024
1 parent a884126 commit 54713e4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 52 deletions.
2 changes: 0 additions & 2 deletions src/SharpConnector/Operations/LiteDbOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
using SharpConnector.Configuration;
using SharpConnector.Entities;
using SharpConnector.Utilities;
using SharpConnector.Connectors.Redis;
using System.Linq;
using SharpConnector.Connectors.Memcached;

namespace SharpConnector.Operations
{
Expand Down
1 change: 0 additions & 1 deletion src/SharpConnector/Operations/MemcachedOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading.Tasks;
using SharpConnector.Configuration;
using SharpConnector.Connectors.Memcached;
using SharpConnector.Connectors.Redis;
using SharpConnector.Entities;
using SharpConnector.Utilities;

Expand Down
9 changes: 9 additions & 0 deletions src/SharpConnector/Operations/MongoDbOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ public override async Task<bool> UpdateAsync(string key, T value)
return await _mongoDbWrapper.UpdateAsync(connectorEntity);
}

/// <summary>
/// Get all values asynchronously from MongoDb.
/// </summary>
/// <returns>A task representing the asynchronous operation, which wraps an enumerable of all objects.</returns>
public override async Task<IEnumerable<T>> GetAllAsync()
{
var connectorEntities = await _mongoDbWrapper.GetAllAsync();
Expand All @@ -186,6 +190,11 @@ public override async Task<IEnumerable<T>> GetAllAsync()
.ToList();
}

/// <summary>
/// Insert multiple values asynchronously.
/// </summary>
/// <param name="values">The values to store as an enumerable.</param>
/// <returns>A task representing the asynchronous operation, which returns true if the insertion was successful.</returns>
public override async Task<bool> InsertManyAsync(IEnumerable<T> values)
{
var connectorEntityList = values
Expand Down
73 changes: 27 additions & 46 deletions src/SharpConnector/Operations/OperationsFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// (c) 2020 Francesco Del Re <[email protected]>
// This code is licensed under MIT license (see LICENSE.txt for details)
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using SharpConnector.Configuration;
Expand All @@ -13,63 +14,43 @@ public class OperationsFactory<T> : OperationFactory
{
private readonly IConfigurationSection _section;

private readonly Dictionary<ConnectorTypeEnums, Func<object, IOperations<T>>> _strategies;

public OperationsFactory(IConfigurationSection section)
{
this._section = section;
_section = section ?? throw new ArgumentNullException(nameof(section));

_strategies = new Dictionary<ConnectorTypeEnums, Func<object, IOperations<T>>>
{
{ ConnectorTypeEnums.Redis, config => new RedisOperations<T>((RedisConfig)config) },
{ ConnectorTypeEnums.MongoDb, config => new MongoDbOperations<T>((MongoDbConfig)config) },
{ ConnectorTypeEnums.LiteDb, config => new LiteDbOperations<T>((LiteDbConfig)config) },
{ ConnectorTypeEnums.Memcached, config => new MemcachedOperations<T>((MemcachedConfig)config) },
{ ConnectorTypeEnums.RavenDb, config => new RavenDbOperations<T>((RavenDbConfig)config) },
{ ConnectorTypeEnums.Couchbase, config => new CouchbaseOperations<T>((CouchbaseConfig)config) },
{ ConnectorTypeEnums.DynamoDb, config => new DynamoDbOperations<T>((DynamoDbConfig)config) }
};
}

/// <summary>
/// Get a new Operations instance related to the connector type and config.
/// </summary>
/// <returns></returns>
/// <returns>Instance of IOperations&lt;T&gt;.</returns>
public IOperations<T> GetStrategy()
{
var dbType = _section.GetChildren().FirstOrDefault(s => s.Key.ToLower() == "instance")?.Value;
var dbType = _section
.GetChildren()
.FirstOrDefault(s => s.Key.Equals("instance", StringComparison.OrdinalIgnoreCase))?.Value;

if (!Enum.TryParse(dbType, true, out ConnectorTypeEnums connectorTypes))
throw new Exception("Instance section for SharpConnector was not found.");
if (!Enum.TryParse(dbType, true, out ConnectorTypeEnums connectorType))
throw new InvalidOperationException("Instance section for SharpConnector was not found.");

var connectorConfig = GetConfigurationStrategy(_section, connectorTypes);
switch (connectorTypes)
{
case ConnectorTypeEnums.Redis:
{
var redisConfig = connectorConfig as RedisConfig;
return new RedisOperations<T>(redisConfig);
}
case ConnectorTypeEnums.MongoDb:
{
var mongoDbConfig = connectorConfig as MongoDbConfig;
return new MongoDbOperations<T>(mongoDbConfig);
}
case ConnectorTypeEnums.LiteDb:
{
var liteDbConfig = connectorConfig as LiteDbConfig;
return new LiteDbOperations<T>(liteDbConfig);
}
case ConnectorTypeEnums.Memcached:
{
var memcachedConfig = connectorConfig as MemcachedConfig;
return new MemcachedOperations<T>(memcachedConfig);
}
case ConnectorTypeEnums.RavenDb:
{
var ravenDbConfig = connectorConfig as RavenDbConfig;
return new RavenDbOperations<T>(ravenDbConfig);
}
case ConnectorTypeEnums.Couchbase:
{
var couchbaseDbConfig = connectorConfig as CouchbaseConfig;
return new CouchbaseOperations<T>(couchbaseDbConfig);
}
case ConnectorTypeEnums.DynamoDb:
{
var dynamoDbConfig = connectorConfig as DynamoDbConfig;
return new DynamoDbOperations<T>(dynamoDbConfig);
}
default:
throw new ArgumentOutOfRangeException();
}
var connectorConfig = GetConfigurationStrategy(_section, connectorType);

if (!_strategies.TryGetValue(connectorType, out var strategy))
throw new ArgumentOutOfRangeException(nameof(connectorType), "Unsupported connector type.");

return strategy(connectorConfig);
}
}
}
10 changes: 9 additions & 1 deletion src/SharpConnector/Operations/RavenDbOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This code is licensed under MIT license (see LICENSE.txt for details)
using SharpConnector.Configuration;
using SharpConnector.Connectors.RavenDb;
using SharpConnector.Connectors.Redis;
using SharpConnector.Entities;
using SharpConnector.Utilities;
using System;
Expand Down Expand Up @@ -178,6 +177,10 @@ public override async Task<bool> UpdateAsync(string key, T value)
return await _ravenDbWrapper.UpdateAsync(connectorEntity);
}

/// <summary>
/// Get all values asynchronously from RavenDb.
/// </summary>
/// <returns>A task representing the asynchronous operation, which wraps an enumerable of all objects.</returns>
public override async Task<IEnumerable<T>> GetAllAsync()
{
var connectorEntities = await _ravenDbWrapper.GetAllAsync();
Expand All @@ -186,6 +189,11 @@ public override async Task<IEnumerable<T>> GetAllAsync()
.ToList();
}

/// <summary>
/// Insert multiple values asynchronously.
/// </summary>
/// <param name="values">The values to store as an enumerable.</param>
/// <returns>A task representing the asynchronous operation, which returns true if the insertion was successful.</returns>
public override async Task<bool> InsertManyAsync(IEnumerable<T> values)
{
var connectorEntityList = values
Expand Down
4 changes: 2 additions & 2 deletions src/SharpConnector/SharpConnector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.404" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.404.1" />
<PackageReference Include="CouchbaseNetClient" Version="3.6.4" />
<PackageReference Include="EnyimMemcachedCore" Version="3.3.0" />
<PackageReference Include="EnyimMemcachedCore" Version="3.3.1" />
<PackageReference Include="LiteDB" Version="5.0.21" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0" />
Expand Down

0 comments on commit 54713e4

Please sign in to comment.