diff --git a/src/SharpConnector/Operations/LiteDbOperations.cs b/src/SharpConnector/Operations/LiteDbOperations.cs index 4237e58..a7f4f06 100755 --- a/src/SharpConnector/Operations/LiteDbOperations.cs +++ b/src/SharpConnector/Operations/LiteDbOperations.cs @@ -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 { diff --git a/src/SharpConnector/Operations/MemcachedOperations.cs b/src/SharpConnector/Operations/MemcachedOperations.cs index e3d8050..684db5d 100755 --- a/src/SharpConnector/Operations/MemcachedOperations.cs +++ b/src/SharpConnector/Operations/MemcachedOperations.cs @@ -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; diff --git a/src/SharpConnector/Operations/MongoDbOperations.cs b/src/SharpConnector/Operations/MongoDbOperations.cs index 7909326..8e6c923 100755 --- a/src/SharpConnector/Operations/MongoDbOperations.cs +++ b/src/SharpConnector/Operations/MongoDbOperations.cs @@ -178,6 +178,10 @@ public override async Task UpdateAsync(string key, T value) return await _mongoDbWrapper.UpdateAsync(connectorEntity); } + /// + /// Get all values asynchronously from MongoDb. + /// + /// A task representing the asynchronous operation, which wraps an enumerable of all objects. public override async Task> GetAllAsync() { var connectorEntities = await _mongoDbWrapper.GetAllAsync(); @@ -186,6 +190,11 @@ public override async Task> GetAllAsync() .ToList(); } + /// + /// Insert multiple values asynchronously. + /// + /// The values to store as an enumerable. + /// A task representing the asynchronous operation, which returns true if the insertion was successful. public override async Task InsertManyAsync(IEnumerable values) { var connectorEntityList = values diff --git a/src/SharpConnector/Operations/OperationsFactory.cs b/src/SharpConnector/Operations/OperationsFactory.cs index 890bbce..5260fca 100755 --- a/src/SharpConnector/Operations/OperationsFactory.cs +++ b/src/SharpConnector/Operations/OperationsFactory.cs @@ -1,6 +1,7 @@ // (c) 2020 Francesco Del Re // 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; @@ -13,63 +14,43 @@ public class OperationsFactory : OperationFactory { private readonly IConfigurationSection _section; + private readonly Dictionary>> _strategies; + public OperationsFactory(IConfigurationSection section) { - this._section = section; + _section = section ?? throw new ArgumentNullException(nameof(section)); + + _strategies = new Dictionary>> + { + { ConnectorTypeEnums.Redis, config => new RedisOperations((RedisConfig)config) }, + { ConnectorTypeEnums.MongoDb, config => new MongoDbOperations((MongoDbConfig)config) }, + { ConnectorTypeEnums.LiteDb, config => new LiteDbOperations((LiteDbConfig)config) }, + { ConnectorTypeEnums.Memcached, config => new MemcachedOperations((MemcachedConfig)config) }, + { ConnectorTypeEnums.RavenDb, config => new RavenDbOperations((RavenDbConfig)config) }, + { ConnectorTypeEnums.Couchbase, config => new CouchbaseOperations((CouchbaseConfig)config) }, + { ConnectorTypeEnums.DynamoDb, config => new DynamoDbOperations((DynamoDbConfig)config) } + }; } /// /// Get a new Operations instance related to the connector type and config. /// - /// + /// Instance of IOperations<T>. public IOperations 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(redisConfig); - } - case ConnectorTypeEnums.MongoDb: - { - var mongoDbConfig = connectorConfig as MongoDbConfig; - return new MongoDbOperations(mongoDbConfig); - } - case ConnectorTypeEnums.LiteDb: - { - var liteDbConfig = connectorConfig as LiteDbConfig; - return new LiteDbOperations(liteDbConfig); - } - case ConnectorTypeEnums.Memcached: - { - var memcachedConfig = connectorConfig as MemcachedConfig; - return new MemcachedOperations(memcachedConfig); - } - case ConnectorTypeEnums.RavenDb: - { - var ravenDbConfig = connectorConfig as RavenDbConfig; - return new RavenDbOperations(ravenDbConfig); - } - case ConnectorTypeEnums.Couchbase: - { - var couchbaseDbConfig = connectorConfig as CouchbaseConfig; - return new CouchbaseOperations(couchbaseDbConfig); - } - case ConnectorTypeEnums.DynamoDb: - { - var dynamoDbConfig = connectorConfig as DynamoDbConfig; - return new DynamoDbOperations(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); } } } diff --git a/src/SharpConnector/Operations/RavenDbOperations.cs b/src/SharpConnector/Operations/RavenDbOperations.cs index e6e28b2..54771c9 100755 --- a/src/SharpConnector/Operations/RavenDbOperations.cs +++ b/src/SharpConnector/Operations/RavenDbOperations.cs @@ -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; @@ -178,6 +177,10 @@ public override async Task UpdateAsync(string key, T value) return await _ravenDbWrapper.UpdateAsync(connectorEntity); } + /// + /// Get all values asynchronously from RavenDb. + /// + /// A task representing the asynchronous operation, which wraps an enumerable of all objects. public override async Task> GetAllAsync() { var connectorEntities = await _ravenDbWrapper.GetAllAsync(); @@ -186,6 +189,11 @@ public override async Task> GetAllAsync() .ToList(); } + /// + /// Insert multiple values asynchronously. + /// + /// The values to store as an enumerable. + /// A task representing the asynchronous operation, which returns true if the insertion was successful. public override async Task InsertManyAsync(IEnumerable values) { var connectorEntityList = values diff --git a/src/SharpConnector/SharpConnector.csproj b/src/SharpConnector/SharpConnector.csproj index 15cff38..de32b0e 100755 --- a/src/SharpConnector/SharpConnector.csproj +++ b/src/SharpConnector/SharpConnector.csproj @@ -18,9 +18,9 @@ - + - +