forked from DevArchitecture/DevArchitecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DevArchitecture#110 from burakhalefoglu/cassandra-…
…branch cassandra added
- Loading branch information
Showing
8 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters