@@ -33,18 +33,19 @@ public class Session : ISession
3333 protected string _tablePrefix ;
3434 private readonly ISqlDialect _dialect ;
3535 private readonly ILogger _logger ;
36+ private readonly bool _withTracking ;
3637
37- public Session ( Store store )
38+ public Session ( Store store , bool withTracking = true )
3839 {
3940 _store = store ;
4041 _tablePrefix = _store . Configuration . TablePrefix ;
4142 _dialect = store . Dialect ;
4243 _logger = store . Configuration . Logger ;
43-
44+ _withTracking = withTracking ;
4445 _defaultState = new SessionState ( ) ;
4546 _collectionStates = new Dictionary < string , SessionState > ( )
4647 {
47- [ "" ] = _defaultState
48+ [ string . Empty ] = _defaultState
4849 } ;
4950 }
5051
@@ -58,7 +59,7 @@ public ISession RegisterIndexes(IIndexProvider[] indexProviders, string collecti
5859 }
5960 }
6061
61- _indexes ??= new List < IIndexProvider > ( ) ;
62+ _indexes ??= [ ] ;
6263
6364 _indexes . AddRange ( indexProviders ) ;
6465
@@ -81,7 +82,6 @@ private SessionState GetState(string collection)
8182 return state ;
8283 }
8384
84- [ Obsolete ]
8585 public void Save ( object entity , bool checkConcurrency = false , string collection = null )
8686 => SaveAsync ( entity , checkConcurrency , collection ) . GetAwaiter ( ) . GetResult ( ) ;
8787
@@ -140,10 +140,7 @@ public async Task SaveAsync(object entity, bool checkConcurrency = false, string
140140 state . IdentityMap . AddEntity ( id , entity ) ;
141141
142142 // Then assign a new identifier if it has one
143- if ( accessor != null )
144- {
145- accessor . Set ( entity , id ) ;
146- }
143+ accessor ? . Set ( entity , id ) ;
147144
148145 state . Saved . Add ( entity ) ;
149146 }
@@ -222,6 +219,23 @@ public void Detach(object entity, string collection)
222219
223220 var state = GetState ( collection ) ;
224221
222+ DetachInternal ( entity , state ) ;
223+ }
224+
225+ public void Detach ( IEnumerable < object > entries , string collection )
226+ {
227+ CheckDisposed ( ) ;
228+
229+ var state = GetState ( collection ) ;
230+
231+ foreach ( var entry in entries )
232+ {
233+ DetachInternal ( entry , state ) ;
234+ }
235+ }
236+
237+ private static void DetachInternal ( object entity , SessionState state )
238+ {
225239 state . Saved . Remove ( entity ) ;
226240 state . Updated . Remove ( entity ) ;
227241 state . Tracked . Remove ( entity ) ;
@@ -277,14 +291,11 @@ private async Task SaveEntityAsync(object entity, string collection)
277291 doc . Version = 1 ;
278292 }
279293
280- if ( versionAccessor != null )
281- {
282- versionAccessor . Set ( entity , doc . Version ) ;
283- }
294+ versionAccessor ? . Set ( entity , doc . Version ) ;
284295
285296 doc . Content = Store . Configuration . ContentSerializer . Serialize ( entity ) ;
286297
287- _commands ??= new List < IIndexCommand > ( ) ;
298+ _commands ??= [ ] ;
288299
289300 _commands . Add ( new CreateDocumentCommand ( doc , Store , collection ) ) ;
290301
@@ -300,14 +311,12 @@ private async Task UpdateEntityAsync(object entity, bool tracked, string collect
300311 throw new ArgumentNullException ( nameof ( entity ) ) ;
301312 }
302313
303- var index = entity as IIndex ;
304-
305314 if ( entity is Document )
306315 {
307316 throw new ArgumentException ( "A document should not be saved explicitly" ) ;
308317 }
309318
310- if ( index != null )
319+ if ( entity is IIndex index )
311320 {
312321 throw new ArgumentException ( "An index should not be saved explicitly" ) ;
313322 }
@@ -379,7 +388,7 @@ private async Task UpdateEntityAsync(object entity, bool tracked, string collect
379388
380389 oldDoc . Content = newContent ;
381390
382- _commands ??= new List < IIndexCommand > ( ) ;
391+ _commands ??= [ ] ;
383392
384393 _commands . Add ( new UpdateDocumentCommand ( oldDoc , Store , version , collection ) ) ;
385394 }
@@ -463,7 +472,7 @@ private async Task DeleteEntityAsync(object obj, string collection)
463472 // Update impacted indexes
464473 await MapDeleted ( doc , obj , collection ) ;
465474
466- _commands ??= new List < IIndexCommand > ( ) ;
475+ _commands ??= [ ] ;
467476
468477 // The command needs to come after any index deletion because of the database constraints
469478 _commands . Add ( new DeleteDocumentCommand ( doc , Store , collection ) ) ;
@@ -535,7 +544,7 @@ public IEnumerable<T> Get<T>(IList<Document> documents, string collection) where
535544 // Are all the objects already in cache?
536545 foreach ( var d in documents )
537546 {
538- if ( state . IdentityMap . TryGetEntityById ( d . Id , out var entity ) )
547+ if ( _withTracking && state . IdentityMap . TryGetEntityById ( d . Id , out var entity ) )
539548 {
540549 result . Add ( ( T ) entity ) ;
541550 }
@@ -568,9 +577,12 @@ public IEnumerable<T> Get<T>(IList<Document> documents, string collection) where
568577
569578 accessor ? . Set ( item , d . Id ) ;
570579
571- // track the loaded object
572- state . IdentityMap . AddEntity ( d . Id , item ) ;
573- state . IdentityMap . AddDocument ( d ) ;
580+ if ( _withTracking )
581+ {
582+ // track the loaded object.
583+ state . IdentityMap . AddEntity ( d . Id , item ) ;
584+ state . IdentityMap . AddDocument ( d ) ;
585+ }
574586
575587 result . Add ( item ) ;
576588 }
@@ -657,7 +669,7 @@ public async Task FlushAsync()
657669 }
658670
659671 // prevent recursive calls in FlushAsync,
660- // when autoflush is triggered from an IndexProvider
672+ // when auto-flush is triggered from an IndexProvider
661673 // for instance.
662674
663675 if ( _flushing )
0 commit comments