Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

cassandra Keyspaces repository .net

public class TestRepository : ITestRepository
{
    private readonly CassandraSessionCache _cassandra;
    private Mapper _mapper;

    public TestRepository(CassandraSessionCache cassandra)
    {
        _cassandra = cassandra;
    }

    public async Task DeleteAsync(Guid id, string keySpace)
    {
        SetSessionAndMapper(keySpace);

        await _mapper.DeleteAsync<Test>("WHERE id = ?", id);
    }

    public async Task<Install> GetAsync(string id, string keySpace)
    {
        SetSessionAndMapper(keySpace);

        return await _mapper.FirstOrDefaultAsync<Test>("SELECT * FROM "Test" WHERE id = ?", id);
    }

    public async Task PostAsync(Test data, string keySpace)
    {
        SetSessionAndMapper(keySpace);

        await _mapper.InsertAsync(data);
    }

    private void SetSessionAndMapper(string keySpace)
    {
        var session = _cassandra.GetSession(keySpace);
        _mapper = new Mapper(session);
    }
}
Comment

cassandra Keyspaces repository .net

My current solve:

Create Cluster:

cluster = Cluster.Builder()
    .AddContactPoint(cassandraData.ContactPoint)
    .WithPort(int.Parse(cassandraData.Port))
    .WithCredentials(cassandraData.User, cassandraData.Password)
    .Build();
After that i inject this by dependency injection to my Session Cache:

public CassandraSessionCache(Cluster cluster)
{
    _cluster = cluster;
    _sessions = new ConcurrentDictionary<string, Lazy<ISession>>();
}

public ISession GetSession(string keyspaceName)
{
    if (!_sessions.ContainsKey(keyspaceName))
        _sessions.GetOrAdd(keyspaceName, key => new Lazy<ISession>(() => 
    _cluster.Connect(key)));

    var result = _sessions[keyspaceName];

    return result.Value;
}
I Create also Map to Cassandra and use it my startup class.

After this configuration my repository look like this:

public class TestRepository : ITestRepository
{
    private readonly CassandraSessionCache _cassandra;
    private Mapper _mapper;

    public TestRepository(CassandraSessionCache cassandra)
    {
        _cassandra = cassandra;
    }

    public async Task DeleteAsync(Guid id, string keySpace)
    {
        SetSessionAndMapper(keySpace);

        await _mapper.DeleteAsync<Test>("WHERE id = ?", id);
    }

    public async Task<Install> GetAsync(string id, string keySpace)
    {
        SetSessionAndMapper(keySpace);

        return await _mapper.FirstOrDefaultAsync<Test>("SELECT * FROM "Test" WHERE id = ?", id);
    }

    public async Task PostAsync(Test data, string keySpace)
    {
        SetSessionAndMapper(keySpace);

        await _mapper.InsertAsync(data);
    }

    private void SetSessionAndMapper(string keySpace)
    {
        var session = _cassandra.GetSession(keySpace);
        _mapper = new Mapper(session);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Maximum Points You Can Obtain from Cards 
Csharp :: jtoken value is not exact double 
Csharp :: how to check if every element in array is true c# 
Csharp :: txtbox.fous in c# 
Csharp :: split nullable in c# 
Csharp :: ExpandoObject Syntax that Compile 
Csharp :: parse persian date string to datetime c# 
Csharp :: asp.net razor get list without refresh 
Csharp :: tee into file 
Csharp :: radio buttons into database stackoverflow 
Csharp :: how to backup terrain in unity 
Csharp :: replace update claims c# 
Csharp :: compass direction mobile unity 
Csharp :: json string to JObject object c# camelCasing key .net 
Csharp :: c# array does not contain a definition for cast 
Csharp :: eager loading singleton c# dependency injection 
Csharp :: Set orientation of moving object towards it movement direction 
Csharp :: RemoveClaim 
Csharp :: how to clear stackpanel wpf 
Csharp :: .net entities query multiple join condition 
Csharp :: asp validator check if textbox is empty 
Csharp :: percentage random c# 
Csharp :: photon2 what is a stream 
Csharp :: list remove positions c# 
Csharp :: palindromes 
Csharp :: qrcode c# 
Csharp :: timespan format string c# 
Csharp :: convert string to int tryparse c# 
Csharp :: c# jump space 
Csharp :: c# pass mouse events to parent 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =