1+ using System . Collections . Generic ;
12using System . Linq . Expressions ;
23using System . Reflection ;
34
@@ -36,6 +37,7 @@ public class UserScopedRepositoryDecorator<TEntity, TKey, TUserKey>
3637 where TEntity : class , IHaveOwner < TUserKey >
3738 where TKey : notnull
3839 {
40+ private const string UserContextNotSetMessage = "User context is not set" ;
3941 private static readonly Lazy < PropertyInfo > _ownerProperty = new ( DiscoverOwnerProperty ) ;
4042
4143 private readonly IRepository < TEntity , TKey > _inner ;
@@ -72,9 +74,9 @@ public UserScopedRepositoryDecorator(
7274 public ValueTask < TEntity ? > FindAsync ( TKey key , CancellationToken cancellationToken = default )
7375 {
7476 var userId = _userAccessor . GetUserId ( ) ;
75- if ( userId == null )
77+ if ( EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
7678 return Options . ThrowWhenUserNotSet
77- ? throw new System . InvalidOperationException ( "User context is not set" )
79+ ? throw new System . InvalidOperationException ( UserContextNotSetMessage )
7880 : new ValueTask < TEntity ? > ( default ( TEntity ) ) ;
7981
8082 return FindScopedAsync ( key , userId , cancellationToken ) ;
@@ -102,7 +104,7 @@ public ValueTask AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken
102104 System . ArgumentNullException . ThrowIfNull ( entities ) ;
103105
104106 var userId = _userAccessor . GetUserId ( ) ;
105- if ( userId != null )
107+ if ( ! EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
106108 {
107109 foreach ( var entity in entities )
108110 {
@@ -111,7 +113,7 @@ public ValueTask AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken
111113 }
112114 else if ( Options . ThrowWhenUserNotSet )
113115 {
114- throw new System . InvalidOperationException ( "User context is not set" ) ;
116+ throw new System . InvalidOperationException ( UserContextNotSetMessage ) ;
115117 }
116118
117119 return _inner . AddRangeAsync ( entities , cancellationToken ) ;
@@ -162,13 +164,13 @@ private ValueTask ApplyOwnerAndCallAsync(TEntity entity, Func<ValueTask> action)
162164 System . ArgumentNullException . ThrowIfNull ( entity ) ;
163165
164166 var userId = _userAccessor . GetUserId ( ) ;
165- if ( userId != null )
167+ if ( ! EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
166168 {
167169 _ownerProperty . Value . SetValue ( entity , userId ) ;
168170 }
169171 else if ( Options . ThrowWhenUserNotSet )
170172 {
171- throw new System . InvalidOperationException ( "User context is not set" ) ;
173+ throw new System . InvalidOperationException ( UserContextNotSetMessage ) ;
172174 }
173175
174176 return action ( ) ;
@@ -178,9 +180,9 @@ private async ValueTask<IList<TEntity>> ApplyOwnerFilterAndCallAsync(
178180 IQuery query , Func < IQuery , ValueTask < IList < TEntity > > > action )
179181 {
180182 var userId = _userAccessor . GetUserId ( ) ;
181- if ( userId == null )
183+ if ( EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
182184 return Options . ThrowWhenUserNotSet
183- ? throw new System . InvalidOperationException ( "User context is not set" )
185+ ? throw new System . InvalidOperationException ( UserContextNotSetMessage )
184186 : Array . Empty < TEntity > ( ) ;
185187
186188 var scopedQuery = ApplyOwnerToQuery ( query , userId ) ;
@@ -191,7 +193,7 @@ private async ValueTask<IList<TEntity>> ApplyOwnerFilterAndCallAsync(
191193 IQuery query , Func < IQuery , ValueTask < TEntity ? > > action )
192194 {
193195 var userId = _userAccessor . GetUserId ( ) ;
194- if ( userId == null )
196+ if ( EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
195197 return null ;
196198
197199 var scopedQuery = ApplyOwnerToQuery ( query , userId ) ;
@@ -202,7 +204,7 @@ private async ValueTask<long> ApplyOwnerFilterAndCallAsync(
202204 IQueryFilter filter , Func < IQueryFilter , ValueTask < long > > action )
203205 {
204206 var userId = _userAccessor . GetUserId ( ) ;
205- if ( userId == null )
207+ if ( EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
206208 return 0 ;
207209
208210 var ownerFilter = BuildOwnerFilter ( userId ) ;
@@ -214,7 +216,7 @@ private async ValueTask<bool> ApplyOwnerFilterAndCallAsync(
214216 IQueryFilter filter , Func < IQueryFilter , ValueTask < bool > > action )
215217 {
216218 var userId = _userAccessor . GetUserId ( ) ;
217- if ( userId == null )
219+ if ( EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
218220 return false ;
219221
220222 var ownerFilter = BuildOwnerFilter ( userId ) ;
@@ -226,9 +228,9 @@ private async ValueTask<PageResult<TEntity>> ApplyOwnerFilterAndCallAsync(
226228 PageQuery < TEntity > request , Func < PageQuery < TEntity > , ValueTask < PageResult < TEntity > > > action )
227229 {
228230 var userId = _userAccessor . GetUserId ( ) ;
229- if ( userId == null )
231+ if ( EqualityComparer < TUserKey > . Default . Equals ( userId , default ) )
230232 return Options . ThrowWhenUserNotSet
231- ? throw new System . InvalidOperationException ( "User context is not set" )
233+ ? throw new System . InvalidOperationException ( UserContextNotSetMessage )
232234 : new PageResult < TEntity > ( request , 0 , Array . Empty < TEntity > ( ) ) ;
233235
234236 var scopedRequest = ApplyOwnerToRequest ( request , userId ) ;
@@ -261,9 +263,8 @@ private static PropertyInfo DiscoverOwnerProperty()
261263
262264 foreach ( var prop in entityType . GetProperties ( BindingFlags . Public | BindingFlags . Instance ) )
263265 {
264- foreach ( var attr in prop . GetCustomAttributes ( ) )
266+ foreach ( var attrType in prop . GetCustomAttributes ( ) . Select ( attr => attr . GetType ( ) ) )
265267 {
266- var attrType = attr . GetType ( ) ;
267268 if ( attrType . Name == "DataOwnerAttribute" &&
268269 ( attrType . Namespace == "Kista" || attrType . Namespace == "Kista.Owners" ) )
269270 {
@@ -287,7 +288,7 @@ private static PropertyInfo DiscoverOwnerProperty()
287288
288289 // === FILTER BUILDING ===
289290
290- private IQueryFilter BuildOwnerFilter ( TUserKey userId )
291+ private static IQueryFilter BuildOwnerFilter ( TUserKey userId )
291292 {
292293 return new ExpressionQueryFilter < TEntity > ( BuildOwnerExpression ( userId ) ) ;
293294 }
0 commit comments