Skip to content

Commit

Permalink
chore: apply general improvements and update packages
Browse files Browse the repository at this point in the history
- Improved code quality and structure.
- Updated dependencies to their latest versions.
  • Loading branch information
engineering87 committed Dec 14, 2024
1 parent 4759621 commit cf6ec9b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/SharpConnector/Connectors/MongoDb/MongoDbAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SharpConnector.Connectors.MongoDb
{
public class MongoDbAccess : IDisposable
{
public IMongoCollection<ConnectorEntity> Collection { get; }
public IMongoCollection<MongoConnectorEntity> Collection { get; }
private readonly MongoClient _client;

/// <summary>
Expand All @@ -25,7 +25,7 @@ public MongoDbAccess(MongoDbConfig mongoDbConfig)

_client = new MongoClient(mongoDbConfig.ConnectionString);
var database = _client.GetDatabase(mongoDbConfig.DatabaseName);
Collection = database.GetCollection<ConnectorEntity>(mongoDbConfig.CollectionName);
Collection = database.GetCollection<MongoConnectorEntity>(mongoDbConfig.CollectionName);
}

/// <summary>
Expand Down
42 changes: 21 additions & 21 deletions src/SharpConnector/Connectors/MongoDb/MongoDbWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public MongoDbWrapper(MongoDbConfig mongoDbConfig)
/// </summary>
/// <param name="key">The key of the object.</param>
/// <returns></returns>
public ConnectorEntity Get(string key)
public MongoConnectorEntity Get(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);
var filter = Builders<MongoConnectorEntity>.Filter.Eq("Key", key);

return _mongoDbAccess.Collection
.Find(filter)
Expand All @@ -40,9 +40,9 @@ public ConnectorEntity Get(string key)
/// </summary>
/// <param name="key">The key of the object.</param>
/// <returns></returns>
public async Task<ConnectorEntity> GetAsync(string key)
public async Task<MongoConnectorEntity> GetAsync(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, key);
var filter = Builders<MongoConnectorEntity>.Filter.Eq(x => x.Key, key);
var cursor = await _mongoDbAccess.Collection.FindAsync(filter).ConfigureAwait(false);

return await cursor.FirstOrDefaultAsync().ConfigureAwait(false);
Expand All @@ -52,7 +52,7 @@ public async Task<ConnectorEntity> GetAsync(string key)
/// Get all the values.
/// </summary>
/// <returns></returns>
public List<ConnectorEntity> GetAll()
public List<MongoConnectorEntity> GetAll()
{
return _mongoDbAccess.Collection
.Find(x => true)
Expand All @@ -63,10 +63,10 @@ public List<ConnectorEntity> GetAll()
/// Asynchronously get all the values.
/// </summary>
/// <returns></returns>
public async Task<List<ConnectorEntity>> GetAllAsync()
public async Task<List<MongoConnectorEntity>> GetAllAsync()
{
return await _mongoDbAccess.Collection
.Find(FilterDefinition<ConnectorEntity>.Empty)
.Find(FilterDefinition<MongoConnectorEntity>.Empty)
.ToListAsync()
.ConfigureAwait(false);
}
Expand All @@ -76,9 +76,9 @@ public async Task<List<ConnectorEntity>> GetAllAsync()
/// </summary>
/// <param name="connectorEntity">The ConnectorEntity to store.</param>
/// <returns></returns>
public bool Insert(ConnectorEntity connectorEntity)
public bool Insert(MongoConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var filter = Builders<MongoConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var options = new ReplaceOptions { IsUpsert = true };

var result = _mongoDbAccess.Collection.ReplaceOne(filter, connectorEntity, options);
Expand All @@ -90,9 +90,9 @@ public bool Insert(ConnectorEntity connectorEntity)
/// </summary>
/// <param name="connectorEntity">The ConnectorEntity to store.</param>
/// <returns></returns>
public async Task<bool> InsertAsync(ConnectorEntity connectorEntity)
public async Task<bool> InsertAsync(MongoConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
var filter = Builders<MongoConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
var options = new ReplaceOptions { IsUpsert = true };

var result = await _mongoDbAccess.Collection.ReplaceOneAsync(filter, connectorEntity, options).ConfigureAwait(false);
Expand All @@ -104,7 +104,7 @@ public async Task<bool> InsertAsync(ConnectorEntity connectorEntity)
/// </summary>
/// <param name="connectorEntities">The ConnectorEntities to store.</param>
/// <returns></returns>
public bool InsertMany(List<ConnectorEntity> connectorEntities)
public bool InsertMany(List<MongoConnectorEntity> connectorEntities)
{
var options = new InsertManyOptions { IsOrdered = false };

Expand All @@ -118,7 +118,7 @@ public bool InsertMany(List<ConnectorEntity> connectorEntities)
/// </summary>
/// <param name="connectorEntities">The list of ConnectorEntities to store.</param>
/// <returns>A boolean indicating if the operation completed successfully.</returns>
public async Task<bool> InsertManyAsync(List<ConnectorEntity> connectorEntities)
public async Task<bool> InsertManyAsync(List<MongoConnectorEntity> connectorEntities)
{
var options = new InsertManyOptions { IsOrdered = false };

Expand All @@ -135,7 +135,7 @@ await _mongoDbAccess.Collection
/// <returns></returns>
public bool Delete(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, key);
var filter = Builders<MongoConnectorEntity>.Filter.Eq(x => x.Key, key);

var result = _mongoDbAccess.Collection.DeleteOne(filter);
return result.IsAcknowledged && result.DeletedCount > 0;
Expand All @@ -148,7 +148,7 @@ public bool Delete(string key)
/// <returns>A boolean indicating if the deletion was acknowledged.</returns>
public async Task<bool> DeleteAsync(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, key);
var filter = Builders<MongoConnectorEntity>.Filter.Eq(x => x.Key, key);

var result = await _mongoDbAccess.Collection
.DeleteOneAsync(filter)
Expand All @@ -161,10 +161,10 @@ public async Task<bool> DeleteAsync(string key)
/// </summary>
/// <param name="connectorEntity">The ConnectorEntity to store.</param>
/// <returns></returns>
public bool Update(ConnectorEntity connectorEntity)
public bool Update(MongoConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var update = Builders<ConnectorEntity>.Update
var filter = Builders<MongoConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var update = Builders<MongoConnectorEntity>.Update
.Set(x => x.Payload, connectorEntity.Payload);

var result = _mongoDbAccess.Collection.UpdateOne(filter, update);
Expand All @@ -176,10 +176,10 @@ public bool Update(ConnectorEntity connectorEntity)
/// </summary>
/// <param name="connectorEntity">The ConnectorEntity to store.</param>
/// <returns>A boolean indicating if the update was acknowledged.</returns>
public async Task<bool> UpdateAsync(ConnectorEntity connectorEntity)
public async Task<bool> UpdateAsync(MongoConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var update = Builders<ConnectorEntity>.Update
var filter = Builders<MongoConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var update = Builders<MongoConnectorEntity>.Update
.Set(x => x.Payload, connectorEntity.Payload);

var result = await _mongoDbAccess.Collection.UpdateOneAsync(filter, update).ConfigureAwait(false);
Expand Down
2 changes: 0 additions & 2 deletions src/SharpConnector/Entities/ConnectorEntity.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 Newtonsoft.Json;
using System;
using MongoDB.Bson.Serialization.Attributes;
using SharpConnector.Utilities;

namespace SharpConnector.Entities
Expand All @@ -12,7 +11,6 @@ namespace SharpConnector.Entities
/// This class encapsulates a key-value pair with optional time-based expiration.
/// </summary>
[Serializable]
[BsonIgnoreExtraElements]
public class ConnectorEntity
{
/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions src/SharpConnector/Entities/MongoDbConnectorEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using MongoDB.Bson.Serialization.Attributes;
using System;

namespace SharpConnector.Entities
{
/// <summary>
/// A MongoDB-specific wrapper for ConnectorEntity to include BSON annotations.
/// </summary>
[BsonIgnoreExtraElements]
public class MongoConnectorEntity : ConnectorEntity
{
// Inherits all properties from ConnectorEntity and includes BSON-specific behavior.

public MongoConnectorEntity() { }

public MongoConnectorEntity(string key, object payload, TimeSpan? expiration)
: base(key, payload, expiration)
{

}
}
}
19 changes: 9 additions & 10 deletions src/SharpConnector/Operations/MongoDbOperations.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.MongoDb;
using SharpConnector.Connectors.Redis;
using SharpConnector.Entities;
using SharpConnector.Utilities;

Expand Down Expand Up @@ -71,7 +70,7 @@ public override IEnumerable<T> GetAll()
/// <returns></returns>
public override bool Insert(string key, T value)
{
var connectorEntity = new ConnectorEntity(key, value, null);
var connectorEntity = new MongoConnectorEntity(key, value, null);
return _mongoDbWrapper.Insert(connectorEntity);
}

Expand All @@ -84,7 +83,7 @@ public override bool Insert(string key, T value)
/// <returns></returns>
public override bool Insert(string key, T value, TimeSpan expiration)
{
var connectorEntity = new ConnectorEntity(key, value, expiration);
var connectorEntity = new MongoConnectorEntity(key, value, expiration);
return _mongoDbWrapper.Insert(connectorEntity);
}

Expand All @@ -96,7 +95,7 @@ public override bool Insert(string key, T value, TimeSpan expiration)
/// <returns></returns>
public override async Task<bool> InsertAsync(string key, T value)
{
var connectorEntity = new ConnectorEntity(key, value, null);
var connectorEntity = new MongoConnectorEntity(key, value, null);
return await _mongoDbWrapper.InsertAsync(connectorEntity);
}

Expand All @@ -109,7 +108,7 @@ public override async Task<bool> InsertAsync(string key, T value)
/// <returns></returns>
public override async Task<bool> InsertAsync(string key, T value, TimeSpan expiration)
{
var connectorEntity = new ConnectorEntity(key, value, expiration);
var connectorEntity = new MongoConnectorEntity(key, value, expiration);
return await _mongoDbWrapper.InsertAsync(connectorEntity);
}

Expand All @@ -120,7 +119,7 @@ public override async Task<bool> InsertAsync(string key, T value, TimeSpan expir
/// <returns></returns>
public override bool InsertMany(Dictionary<string, T> values)
{
return _mongoDbWrapper.InsertMany(values.ToConnectorEntityList());
return _mongoDbWrapper.InsertMany(values.ToMongoDbConnectorEntityList());
}

/// <summary>
Expand All @@ -131,7 +130,7 @@ public override bool InsertMany(Dictionary<string, T> values)
/// <returns></returns>
public override bool InsertMany(Dictionary<string, T> values, TimeSpan expiration)
{
return _mongoDbWrapper.InsertMany(values.ToConnectorEntityList(expiration));
return _mongoDbWrapper.InsertMany(values.ToMongoDbConnectorEntityList(expiration));
}

/// <summary>
Expand Down Expand Up @@ -162,7 +161,7 @@ public override async Task<bool> DeleteAsync(string key)
/// <returns></returns>
public override bool Update(string key, T value)
{
var connectorEntity = new ConnectorEntity(key, value, null);
var connectorEntity = new MongoConnectorEntity(key, value, null);
return _mongoDbWrapper.Update(connectorEntity);
}

Expand All @@ -174,7 +173,7 @@ public override bool Update(string key, T value)
/// <returns></returns>
public override async Task<bool> UpdateAsync(string key, T value)
{
var connectorEntity = new ConnectorEntity(key, value, null);
var connectorEntity = new MongoConnectorEntity(key, value, null);
return await _mongoDbWrapper.UpdateAsync(connectorEntity);
}

Expand All @@ -198,7 +197,7 @@ public override async Task<IEnumerable<T>> GetAllAsync()
public override async Task<bool> InsertManyAsync(IEnumerable<T> values)
{
var connectorEntityList = values
.Cast<ConnectorEntity>()
.Cast<MongoConnectorEntity>()
.ToList();
return await _mongoDbWrapper.InsertManyAsync(connectorEntityList);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SharpConnector/SharpConnector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.404.3" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.404.5" />
<PackageReference Include="CouchbaseNetClient" Version="3.6.4" />
<PackageReference Include="EnyimMemcachedCore" Version="3.3.1" />
<PackageReference Include="LiteDB" Version="5.0.21" />
Expand Down
22 changes: 22 additions & 0 deletions src/SharpConnector/Utilities/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,27 @@ public static List<LiteDbConnectorEntity> ToLiteDbConnectorEntityList<T>(this Di

return connectorEntities;
}

/// <summary>
/// Returns a list of MongoConnectorEntity.
/// </summary>
/// <typeparam name="T">The generic type to store.</typeparam>
/// <param name="values">The dictionary with <key, object> elements to store.</param>
/// <returns></returns>
public static List<MongoConnectorEntity> ToMongoDbConnectorEntityList<T>(this Dictionary<string, T> values, TimeSpan? expiration = null)
{
if (values == null)
return [];

var connectorEntities = new List<MongoConnectorEntity>();
foreach (var entry in values)
{
if (string.IsNullOrEmpty(entry.Key) || entry.Value == null)
continue;
connectorEntities.Add(new MongoConnectorEntity(entry.Key, entry.Value, expiration));
}

return connectorEntities;
}
}
}

0 comments on commit cf6ec9b

Please sign in to comment.