1+ using System ;
2+ using System . Data ;
3+ using System . Data . Entity ;
4+ using System . Linq ;
5+ using SharpRepository . Repository ;
6+ using SharpRepository . Repository . Caching ;
7+ using SharpRepository . Repository . FetchStrategies ;
8+
9+ namespace SharpRepository . Ef5Repository
10+ {
11+ public class Ef5CompoundKeyRepositoryBase < T > : LinqCompoundKeyRepositoryBase < T > where T : class , new ( )
12+ {
13+ protected IDbSet < T > DbSet { get ; private set ; }
14+ protected DbContext Context { get ; private set ; }
15+
16+ internal Ef5CompoundKeyRepositoryBase ( DbContext dbContext , ICompoundKeyCachingStrategy < T > cachingStrategy = null )
17+ : base ( cachingStrategy )
18+ {
19+ Initialize ( dbContext ) ;
20+ }
21+
22+ private void Initialize ( DbContext dbContext )
23+ {
24+ Context = dbContext ;
25+ DbSet = Context . Set < T > ( ) ;
26+ }
27+
28+ protected override void AddItem ( T entity )
29+ {
30+ // no generating primary keys
31+ DbSet . Add ( entity ) ;
32+ }
33+
34+ protected override void DeleteItem ( T entity )
35+ {
36+ DbSet . Remove ( entity ) ;
37+ }
38+
39+ protected override void UpdateItem ( T entity )
40+ {
41+ // mark this entity as modified, in case it is not currently attached to this context
42+ Context . Entry ( entity ) . State = EntityState . Modified ;
43+ }
44+
45+ protected override void SaveChanges ( )
46+ {
47+ Context . SaveChanges ( ) ;
48+ }
49+
50+ protected override IQueryable < T > BaseQuery ( IFetchStrategy < T > fetchStrategy = null )
51+ {
52+ var query = DbSet . AsQueryable ( ) ;
53+ return fetchStrategy == null ? query : fetchStrategy . IncludePaths . Aggregate ( query , ( current , path ) => current . Include ( path ) ) ;
54+ }
55+
56+ // we override the implementation fro LinqBaseRepository becausee this is built in and doesn't need to find the key column and do dynamic expressions, etc.
57+ protected override T GetQuery ( params object [ ] keys )
58+ {
59+ return DbSet . Find ( keys ) ;
60+ }
61+
62+ public override void Dispose ( )
63+ {
64+ Dispose ( true ) ;
65+ GC . SuppressFinalize ( this ) ;
66+ }
67+
68+ protected virtual void Dispose ( bool disposing )
69+ {
70+ if ( ! disposing ) return ;
71+ if ( Context == null ) return ;
72+
73+ Context . Dispose ( ) ;
74+ Context = null ;
75+ }
76+ }
77+
78+ public class Ef5CompoundKeyRepositoryBase < T , TKey , TKey2 > : LinqCompoundKeyRepositoryBase < T , TKey , TKey2 > where T : class , new ( )
79+ {
80+ protected IDbSet < T > DbSet { get ; private set ; }
81+ protected DbContext Context { get ; private set ; }
82+
83+ internal Ef5CompoundKeyRepositoryBase ( DbContext dbContext , ICompoundKeyCachingStrategy < T , TKey , TKey2 > cachingStrategy = null )
84+ : base ( cachingStrategy )
85+ {
86+ Initialize ( dbContext ) ;
87+ }
88+
89+ private void Initialize ( DbContext dbContext )
90+ {
91+ Context = dbContext ;
92+ DbSet = Context . Set < T > ( ) ;
93+ }
94+
95+ protected override void AddItem ( T entity )
96+ {
97+ DbSet . Add ( entity ) ;
98+ }
99+
100+ protected override void DeleteItem ( T entity )
101+ {
102+ DbSet . Remove ( entity ) ;
103+ }
104+
105+ protected override void UpdateItem ( T entity )
106+ {
107+ // mark this entity as modified, in case it is not currently attached to this context
108+ Context . Entry ( entity ) . State = EntityState . Modified ;
109+ }
110+
111+ protected override void SaveChanges ( )
112+ {
113+ Context . SaveChanges ( ) ;
114+ }
115+
116+ protected override IQueryable < T > BaseQuery ( IFetchStrategy < T > fetchStrategy = null )
117+ {
118+ var query = DbSet . AsQueryable ( ) ;
119+ return fetchStrategy == null ? query : fetchStrategy . IncludePaths . Aggregate ( query , ( current , path ) => current . Include ( path ) ) ;
120+ }
121+
122+ // we override the implementation fro LinqBaseRepository becausee this is built in and doesn't need to find the key column and do dynamic expressions, etc.
123+ protected override T GetQuery ( TKey key , TKey2 key2 )
124+ {
125+ return DbSet . Find ( key , key2 ) ;
126+ }
127+
128+ public override void Dispose ( )
129+ {
130+ Dispose ( true ) ;
131+ GC . SuppressFinalize ( this ) ;
132+ }
133+
134+ protected virtual void Dispose ( bool disposing )
135+ {
136+ if ( ! disposing ) return ;
137+ if ( Context == null ) return ;
138+
139+ Context . Dispose ( ) ;
140+ Context = null ;
141+ }
142+ }
143+
144+ public class Ef5CompoundKeyRepositoryBase < T , TKey , TKey2 , TKey3 > : LinqCompoundKeyRepositoryBase < T , TKey , TKey2 , TKey3 > where T : class , new ( )
145+ {
146+ protected IDbSet < T > DbSet { get ; private set ; }
147+ protected DbContext Context { get ; private set ; }
148+
149+ internal Ef5CompoundKeyRepositoryBase ( DbContext dbContext , ICompoundKeyCachingStrategy < T , TKey , TKey2 , TKey3 > cachingStrategy = null )
150+ : base ( cachingStrategy )
151+ {
152+ Initialize ( dbContext ) ;
153+ }
154+
155+ private void Initialize ( DbContext dbContext )
156+ {
157+ Context = dbContext ;
158+ DbSet = Context . Set < T > ( ) ;
159+ }
160+
161+ protected override void AddItem ( T entity )
162+ {
163+ DbSet . Add ( entity ) ;
164+ }
165+
166+ protected override void DeleteItem ( T entity )
167+ {
168+ DbSet . Remove ( entity ) ;
169+ }
170+
171+ protected override void UpdateItem ( T entity )
172+ {
173+ // mark this entity as modified, in case it is not currently attached to this context
174+ Context . Entry ( entity ) . State = EntityState . Modified ;
175+ }
176+
177+ protected override void SaveChanges ( )
178+ {
179+ Context . SaveChanges ( ) ;
180+ }
181+
182+ protected override IQueryable < T > BaseQuery ( IFetchStrategy < T > fetchStrategy = null )
183+ {
184+ var query = DbSet . AsQueryable ( ) ;
185+ return fetchStrategy == null ? query : fetchStrategy . IncludePaths . Aggregate ( query , ( current , path ) => current . Include ( path ) ) ;
186+ }
187+
188+ // we override the implementation fro LinqBaseRepository becausee this is built in and doesn't need to find the key column and do dynamic expressions, etc.
189+ protected override T GetQuery ( TKey key , TKey2 key2 , TKey3 key3 )
190+ {
191+ return DbSet . Find ( key , key2 , key3 ) ;
192+ }
193+
194+ public override void Dispose ( )
195+ {
196+ Dispose ( true ) ;
197+ GC . SuppressFinalize ( this ) ;
198+ }
199+
200+ protected virtual void Dispose ( bool disposing )
201+ {
202+ if ( ! disposing ) return ;
203+ if ( Context == null ) return ;
204+
205+ Context . Dispose ( ) ;
206+ Context = null ;
207+ }
208+ }
209+ }
0 commit comments