11using System ;
22using SmartStore . Core . Infrastructure ;
3+ using System . Linq . Expressions ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
36
47namespace SmartStore . Core . Data
58{
69 public class DbContextScope : IDisposable
710 {
8- private readonly bool _autoDetectChangesEnabled ;
11+ private readonly IDbContext _ctx ;
12+ private readonly bool _autoDetectChangesEnabled ;
913 private readonly bool _proxyCreationEnabled ;
1014 private readonly bool _validateOnSaveEnabled ;
1115 private readonly bool _forceNoTracking ;
1216 private readonly bool _hooksEnabled ;
1317 private readonly bool _autoCommit ;
14- private readonly IDbContext _ctx ;
18+ private readonly bool _lazyLoading ;
19+
1520
1621 public DbContextScope ( IDbContext ctx = null ,
1722 bool ? autoDetectChanges = null ,
1823 bool ? proxyCreation = null ,
1924 bool ? validateOnSave = null ,
2025 bool ? forceNoTracking = null ,
2126 bool ? hooksEnabled = null ,
22- bool ? autoCommit = null )
27+ bool ? autoCommit = null ,
28+ bool ? lazyLoading = null )
2329 {
2430 _ctx = ctx ?? EngineContext . Current . Resolve < IDbContext > ( ) ;
2531 _autoDetectChangesEnabled = _ctx . AutoDetectChangesEnabled ;
@@ -28,6 +34,7 @@ public DbContextScope(IDbContext ctx = null,
2834 _forceNoTracking = _ctx . ForceNoTracking ;
2935 _hooksEnabled = _ctx . HooksEnabled ;
3036 _autoCommit = _ctx . AutoCommitEnabled ;
37+ _lazyLoading = _ctx . LazyLoadingEnabled ;
3138
3239 if ( autoDetectChanges . HasValue )
3340 _ctx . AutoDetectChangesEnabled = autoDetectChanges . Value ;
@@ -46,7 +53,36 @@ public DbContextScope(IDbContext ctx = null,
4653
4754 if ( autoCommit . HasValue )
4855 _ctx . AutoCommitEnabled = autoCommit . Value ;
49- }
56+
57+ if ( lazyLoading . HasValue )
58+ _ctx . LazyLoadingEnabled = lazyLoading . Value ;
59+ }
60+
61+ public IDbContext DbContext
62+ {
63+ get { return _ctx ; }
64+ }
65+
66+ public void LoadCollection < TEntity , TCollection > (
67+ TEntity entity ,
68+ Expression < Func < TEntity , ICollection < TCollection > > > navigationProperty ,
69+ bool force = false ,
70+ Func < IQueryable < TCollection > , IQueryable < TCollection > > queryAction = null )
71+ where TEntity : BaseEntity
72+ where TCollection : BaseEntity
73+ {
74+ _ctx . LoadCollection ( entity , navigationProperty , force , queryAction ) ;
75+ }
76+
77+ public void LoadReference < TEntity , TProperty > (
78+ TEntity entity ,
79+ Expression < Func < TEntity , TProperty > > navigationProperty ,
80+ bool force = false )
81+ where TEntity : BaseEntity
82+ where TProperty : BaseEntity
83+ {
84+ _ctx . LoadReference ( entity , navigationProperty , force ) ;
85+ }
5086
5187 public int Commit ( )
5288 {
@@ -61,6 +97,7 @@ public void Dispose()
6197 _ctx . ForceNoTracking = _forceNoTracking ;
6298 _ctx . HooksEnabled = _hooksEnabled ;
6399 _ctx . AutoCommitEnabled = _autoCommit ;
100+ _ctx . LazyLoadingEnabled = _lazyLoading ;
64101 }
65102
66103 }
0 commit comments