Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RavenDB#2144

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
stefaner wants to merge13 commits intofluentcms:dev
base:dev
Choose a base branch
Loading
fromstefaner:RavenDB
Open

RavenDB#2144

stefaner wants to merge13 commits intofluentcms:devfromstefaner:RavenDB

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. UseId.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 theApiToken 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.)

@pournasserianpournasserian self-assigned thisOct 14, 2024
@pournasserianpournasserian self-requested a reviewOctober 14, 2024 14:41
@pournasserianpournasserian added the enhancementNew feature or request labelOct 14, 2024
@pournasserianpournasserian added this to theMVP milestoneOct 14, 2024
@pournasserianpournasserian linked an issueOct 14, 2024 that may beclosed 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@pournasserianpournasserianAwaiting requested review from pournasserian

At least 1 approving review is required to merge this pull request.

Labels

enhancementNew feature or request

Projects

None yet

Milestone

MVP

Development

Successfully merging this pull request may close these issues.

Develop new repository to support RavenDB

2 participants

@stefaner@pournasserian

[8]ページ先頭

©2009-2025 Movatter.jp