Skip to content

Commit

Permalink
Merge pull request DevArchitecture#110 from burakhalefoglu/cassandra-…
Browse files Browse the repository at this point in the history
…branch

cassandra added
  • Loading branch information
keremvaris authored Jun 30, 2024
2 parents b7c3ce3 + 57626b7 commit 204304a
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="6.0.1" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="CassandraCSharpDriver" Version="3.20.1" />
<PackageReference Include="Confluent.Kafka" Version="2.4.0" />
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="Hangfire" Version="1.8.11" />
Expand Down
155 changes: 155 additions & 0 deletions Core/DataAccess/Cassandra/CassandraRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Cassandra;
using Cassandra.Data.Linq;
using Cassandra.Mapping;
using Core.DataAccess.Cassandra.Configurations;
using Core.Entities;
using Core.Utilities.IoC;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Core.DataAccess.Cassandra;

// https://github.com/datastax/csharp-driver
public class CassandraRepositoryBase<T>
: ICassRepository<T>
where T : CassDbEntity
{
private readonly IMapper _mapper;
private readonly MappingConfiguration _mappingConfiguration;
private readonly Table<T> _table;

protected CassandraRepositoryBase(MappingConfiguration mappingConfiguration)
{
_mappingConfiguration = mappingConfiguration;
var configuration = ServiceTool.ServiceProvider.GetService<IConfiguration>();
var cassandraConnectionSettings =
configuration.GetSection("CassandraConnectionSettings").Get<CassandraConnectionSettings>();
var cluster = Cluster.Builder()
.AddContactPoints(cassandraConnectionSettings.Host)
.WithCredentials(cassandraConnectionSettings.UserName, cassandraConnectionSettings.Password)
.WithApplicationName("CustomerProjectServer")
.WithCompression(CompressionType.Snappy)
.Build();
var session = cluster.Connect();
session.CreateKeyspaceIfNotExists(cassandraConnectionSettings.Keyspace);
_table = new Table<T>(session, mappingConfiguration);
_table.CreateIfNotExists();
_mapper = new Mapper(session, mappingConfiguration);
}

public IQueryable<T> GetList(Expression<Func<T, bool>> predicate = null)
{
return predicate == null
? _table.Execute().AsQueryable()
: _table.Where(predicate)
.Execute().AsQueryable();
}

public T GetById(long id)
{
return _table.FirstOrDefault(u => u.Id == id).Execute();
}

public long GetCount(Expression<Func<T, bool>> predicate = null)
{
return predicate == null
? _table.Count().Execute()
: _table.Where(predicate)
.Count<T>().Execute();
}

public async Task<long> GetCountAsync(Expression<Func<T, bool>> predicate = null)
{
return await Task.Run(() => predicate == null
? _table.Count().Execute()
: _table.Where(predicate)
.Count<T>().Execute());
}

public async Task<IQueryable<T>> GetListAsync(Expression<Func<T, bool>> predicate = null)
{
return await Task.Run(() => predicate == null
? _table.Execute().AsQueryable()
: _table.Where(predicate)
.Execute().AsQueryable());
}

public async Task<T> GetByIdAsync(long id)
{
return await Task.Run(() =>
{
return _table.FirstOrDefault(u => u.Id == id).Execute();
});
}

public async Task<T> GetAsync(Expression<Func<T, bool>> predicate)
{
return (await Task.Run(() => _table.Where(predicate).FirstOrDefault<T>().Execute()))!;
}

public bool Any(Expression<Func<T, bool>> predicate = null)
{
var data = predicate == null
? _table.FirstOrDefault().Execute()
: _table.Where(predicate).FirstOrDefault<T>().Execute();

return data != null;
}

public async Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null)
{
return await Task.Run(() =>
{
var data = predicate == null
? _table.FirstOrDefault().Execute()
: _table.Where(predicate).FirstOrDefault<T>().Execute();

return data != null;
});
}

public void Add(T entity)
{
var filter = _table.Execute().MaxBy(e => e.Id);
var id = filter?.Id ?? 0;
entity.Id = id + 1;
_table.Insert(entity).Execute();
}

public async Task AddAsync(T entity)
{
await Task.Run(() =>
{
var filter = _table.Execute().MaxBy(e => e.Id);
var id = filter?.Id ?? 0;
entity.Id = id + 1;
_table.Insert(entity).Execute();
});
}

public async Task UpdateAsync(T entity)
{
await _mapper.DeleteAsync(entity);
await _mapper.InsertAsync(entity);
}

public void Update(T entity)
{
_mapper.Delete(entity);
_mapper.Insert(entity);
}

public void Delete(T entity)
{
_mapper.Delete(entity);
}

public async Task DeleteAsync(T entity)
{
await _mapper.DeleteAsync(entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Core.DataAccess.Cassandra.Configurations;

public class CassandraConnectionSettings
{
public string Host { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Keyspace { get; set; }
}
37 changes: 37 additions & 0 deletions Core/DataAccess/Cassandra/ICassRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Core.Entities;

namespace Core.DataAccess.Cassandra;

public interface ICassRepository<T> where T : class, IEntity
{
void Add(T entity);

IQueryable<T> GetList(Expression<Func<T, bool>> predicate = null);

Task UpdateAsync(T record);

void Update(T record);
Task DeleteAsync(T record);

void Delete(T record);

T GetById(long id);

Task AddAsync(T entity);

Task<IQueryable<T>> GetListAsync(Expression<Func<T, bool>> predicate = null);

Task<T> GetByIdAsync(long id);

Task<T> GetAsync(Expression<Func<T, bool>> predicate);

bool Any(Expression<Func<T, bool>> predicate = null);
Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null);

long GetCount(Expression<Func<T, bool>> predicate = null);
Task<long> GetCountAsync(Expression<Func<T, bool>> predicate = null);
}
6 changes: 6 additions & 0 deletions Core/Entities/CassDbEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Core.Entities;

public abstract class CassDbEntity: IEntity
{
public int Id { get; set; }
}
6 changes: 6 additions & 0 deletions WebAPI/appsettings.Production.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"Services": {

},
"CassandraConnectionSettings": {
"Host": "localhost",
"UserName": "test",
"Password": "test",
"Keyspace": "test"
},
"ConnectionStrings": {
"DArchPgContext": "Host=localhost;Port=5432;Database=TestDb;Username=postgres;Password=test",
Expand Down
6 changes: 6 additions & 0 deletions WebAPI/appsettings.Staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"Services": {

},
"CassandraConnectionSettings": {
"Host": "localhost",
"UserName": "test",
"Password": "test",
"Keyspace": "test"
},
"ConnectionStrings": {
"DArchPgContext": "Host=localhost;Port=5432;Database=TestDb;Username=postgres;Password=test",
"DArchMsContext": "data source=(localdb)\\MSSQLLocalDB;initial catalog=TestDb;persist security info=False;user id=sa;password=test;",
Expand Down
6 changes: 6 additions & 0 deletions WebAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
"ConnectionString": "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false",
"DatabaseName": "customerdb"
},
"CassandraConnectionSettings": {
"Host": "localhost",
"UserName": "test",
"Password": "test",
"Keyspace": "test"
},
"SeriLogConfigurations": {
"PostgreConfiguration": {
"ConnectionString": "Host=localhost;Port=5432;Database=TestDb;Username=postgres;Password=test;"
Expand Down

0 comments on commit 204304a

Please sign in to comment.