-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathEntityRepository.cs
More file actions
168 lines (126 loc) · 5.51 KB
/
Copy pathEntityRepository.cs
File metadata and controls
168 lines (126 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using Raven.Client.Documents.Linq;
namespace FluentCMS.Repositories.RavenDB;
public abstract class EntityRepository<TEntity> : IEntityRepository<TEntity> where TEntity : IEntity
{
protected readonly IDocumentStore Store;
public EntityRepository(IRavenDBContext RavenDbContext)
{
Store = RavenDbContext.Store;
// Ensure index on Id field
// var indexKeysDefinition = Builders<TEntity>.IndexKeys.Ascending(x => x.Id);
// Collection.Indexes.CreateOne(new CreateIndexModel<TEntity>(indexKeysDefinition));
}
public virtual async Task<IEnumerable<TEntity>> GetAll(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
var entities = await session.Query<RavenEntity<TEntity>>()
.Select(x => x.Data)
.ToListAsync(cancellationToken);
return entities.AsEnumerable();
}
}
public virtual async Task<TEntity?> GetById(Guid id, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
var entity = await session.Query<RavenEntity<TEntity>>().SingleOrDefaultAsync(x => x.Data.Id == id, cancellationToken);
return entity.Data;
}
}
public virtual async Task<IEnumerable<TEntity>> GetByIds(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
var entities = await session.Query<RavenEntity<TEntity>>().Where(x => ids.Contains(x.Data.Id))
.Select(x => x.Data)
.ToListAsync(cancellationToken);
return entities.AsEnumerable();
}
}
public virtual async Task<TEntity?> Create(TEntity entity, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
if (entity.Id == Guid.Empty)
{
entity.Id = Guid.NewGuid();
}
await session.StoreAsync(new RavenEntity<TEntity>(entity), cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}
return entity;
}
public virtual async Task<IEnumerable<TEntity>> CreateMany(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
foreach (var entity in entities)
{
cancellationToken.ThrowIfCancellationRequested();
if (entity.Id == Guid.Empty)
{
entity.Id = Guid.NewGuid();
}
await session.StoreAsync(new RavenEntity<TEntity>(entity), cancellationToken);
}
await session.SaveChangesAsync(cancellationToken);
}
return entities;
}
public virtual async Task<TEntity?> Update(TEntity entity, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
var id = entity.Id; // Needs to be extracted to guid to avoid type casts in query.
var dbEntity = await session.Query<RavenEntity<TEntity>>().SingleOrDefaultAsync(x => x.Data.Id == id, cancellationToken);
if (dbEntity == null)
{
if (entity.Id == Guid.Empty)
{
entity.Id = Guid.NewGuid();
}
dbEntity = new RavenEntity<TEntity>(entity);
await session.StoreAsync(dbEntity, cancellationToken);
}
else
{
entity.CopyProperties(dbEntity.Data);
}
await session.SaveChangesAsync(cancellationToken);
return dbEntity.Data;
}
}
public virtual async Task<TEntity?> Delete(Guid id, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
var entity = await session.Query<RavenEntity<TEntity>>().SingleOrDefaultAsync(x => x.Data.Id == id, cancellationToken);
session.Delete(entity);
await session.SaveChangesAsync(cancellationToken);
return entity.Data; // A bit strange to return to object we just deleted.
}
}
public virtual async Task<IEnumerable<TEntity>> DeleteMany(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using (var session = Store.OpenAsyncSession())
{
var entities = await session.Query<RavenEntity<TEntity>>().Where(x => ids.Contains(x.Data.Id)).ToListAsync(cancellationToken);
foreach (var entity in entities)
{
cancellationToken.ThrowIfCancellationRequested();
session.Delete(entity);
}
await session.SaveChangesAsync(cancellationToken);
return entities.Select(x => x.Data);
}
}
}