“Cassandra KeysPaces Repository .NET” Kod odpowiedzi

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);
    }
}
DreamCoder

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);
    }
}
DreamCoder

Odpowiedzi podobne do “Cassandra KeysPaces Repository .NET”

Pytania podobne do “Cassandra KeysPaces Repository .NET”

Więcej pokrewnych odpowiedzi na “Cassandra KeysPaces Repository .NET” w C#

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu