Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RavenDB #2144

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
Open

RavenDB #2144

wants to merge 11 commits into from

Conversation

stefaner
Copy link

Hi,

I wrote a repository for RavenDB. Maybe not the most elegant, but it works. Any suggestions and/or corrections are welcome.

I needed to do some minor changes to the Entity class, but I don't think it will effect any of the other repositories.
It turns out that RavenDB doesn't like Guid as Id property. So I introduced a second property just for RavenDB. The system still use Id and Guid, l but RavenDB use RavenId as an internal identifier.

@pournasserian
Copy link
Contributor

Thank you @stefaner

We are not allowed to add any property (RavenId) to the main model. There are some other approaches to implement:

  1. Use Id.ToString() for RavenDB document
  2. Develop internal classes to store/retrieve in RavenDB

For the first approach here is a simple example:

public class RavenDbEntityRepository<TEntity> : IEntityRepository<TEntity> where TEntity : class, IEntity
{
    private readonly IAsyncDocumentSession _session;

    public RavenDbEntityRepository(IAsyncDocumentSession session)
    {
        _session = session;
    }

    public async Task<TEntity?> Create(TEntity entity, CancellationToken cancellationToken = default)
    {
        // Convert Guid to string for RavenDB document ID
        await _session.StoreAsync(entity, entity.Id.ToString(), cancellationToken);
        await _session.SaveChangesAsync(cancellationToken);
        return entity;
    }

    public async Task<IEnumerable<TEntity>> CreateMany(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
    {
        foreach (var entity in entities)
        {
            await _session.StoreAsync(entity, entity.Id.ToString(), cancellationToken);
        }
        await _session.SaveChangesAsync(cancellationToken);
        return entities;
    }

    public async Task<TEntity?> Update(TEntity entity, CancellationToken cancellationToken = default)
    {
        // In RavenDB, StoreAsync updates an entity if it already exists
        await _session.StoreAsync(entity, entity.Id.ToString(), cancellationToken);
        await _session.SaveChangesAsync(cancellationToken);
        return entity;
    }

    public async Task<TEntity?> Delete(Guid id, CancellationToken cancellationToken = default)
    {
        // Convert Guid to string for querying
        var entity = await _session.LoadAsync<TEntity>(id.ToString(), cancellationToken);
        if (entity != null)
        {
            _session.Delete(entity);
            await _session.SaveChangesAsync(cancellationToken);
        }
        return entity;
    }

    public async Task<IEnumerable<TEntity>> DeleteMany(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
    {
        var deletedEntities = new List<TEntity>();
        foreach (var id in ids)
        {
            var entity = await _session.LoadAsync<TEntity>(id.ToString(), cancellationToken);
            if (entity != null)
            {
                _session.Delete(entity);
                deletedEntities.Add(entity);
            }
        }
        await _session.SaveChangesAsync(cancellationToken);
        return deletedEntities;
    }

    public async Task<IEnumerable<TEntity>> GetAll(CancellationToken cancellationToken = default)
    {
        return await _session.Query<TEntity>().ToListAsync(cancellationToken);
    }

    public async Task<TEntity?> GetById(Guid id, CancellationToken cancellationToken = default)
    {
        // Convert Guid to string for querying
        return await _session.LoadAsync<TEntity>(id.ToString(), cancellationToken);
    }

    public async Task<IEnumerable<TEntity>> GetByIds(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
    {
        // Convert Guid list to string list for querying
        var stringIds = ids.Select(id => id.ToString());
        return await _session.LoadAsync<TEntity>(stringIds, cancellationToken);
    }
}

For the second approach, for the ApiToken entity, you may have the below class:

public class RavenApiToken : ApiToken
{
    public string RavenId { get; set; } = default!;
}

And map the the main model ApiKey before storing into the RavenDB model RavenApiToken (using AutoMapper, manual mapping, etc.)

@pournasserian pournasserian self-assigned this Oct 14, 2024
@pournasserian pournasserian self-requested a review October 14, 2024 14:41
@pournasserian pournasserian added the enhancement New feature or request label Oct 14, 2024
@pournasserian pournasserian added this to the MVP milestone Oct 14, 2024
@pournasserian pournasserian linked an issue Oct 14, 2024 that may be closed by this pull request
@stefaner
Copy link
Author

Hi,

I've tested the first approach. It didn't work.
https://ravendb.net/docs/article-page/6.2/Csharp/client-api/document-identifiers/working-with-document-identifiers#identities
"Identifiers of documents in RavenDB database are always strings, so take this into consideration when you model your entities."

I will test your second suggestion.

@stefaner
Copy link
Author

Restored the Entity class and made a wrapper class to use in RavenDB. Perhaps not the cleanest solution. But it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Develop new repository to support RavenDB
2 participants