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 Ef5RepositoryBase < T , TKey > : LinqRepositoryBase < T , TKey > where T : class , new ( )
12- {
13- protected IDbSet < T > DbSet { get ; private set ; }
14- protected DbContext Context { get ; private set ; }
15-
16- internal Ef5RepositoryBase ( DbContext dbContext , ICachingStrategy < T , TKey > cachingStrategy = null ) : base ( cachingStrategy )
17- {
18- Initialize ( dbContext ) ;
19- }
20-
21- private void Initialize ( DbContext dbContext )
22- {
23- Context = dbContext ;
24- DbSet = Context . Set < T > ( ) ;
25- }
26-
27- protected override void AddItem ( T entity )
28- {
29- if ( typeof ( TKey ) == typeof ( Guid ) || typeof ( TKey ) == typeof ( string ) )
30- {
31- TKey id ;
32- if ( GetPrimaryKey ( entity , out id ) && Equals ( id , default ( TKey ) ) )
33- {
34- id = GeneratePrimaryKey ( ) ;
35- SetPrimaryKey ( entity , id ) ;
36- }
37- }
38- DbSet . Add ( entity ) ;
39- }
40-
41- protected override void DeleteItem ( T entity )
42- {
43- DbSet . Remove ( entity ) ;
44- }
45-
46- protected override void UpdateItem ( T entity )
47- {
48- // mark this entity as modified, in case it is not currently attached to this context
49- Context . Entry ( entity ) . State = EntityState . Modified ;
50- }
51-
52- protected override void SaveChanges ( )
53- {
54- Context . SaveChanges ( ) ;
55- }
56-
57- protected override IQueryable < T > BaseQuery ( IFetchStrategy < T > fetchStrategy = null )
58- {
59- var query = DbSet . AsQueryable ( ) ;
60- return fetchStrategy == null ? query : fetchStrategy . IncludePaths . Aggregate ( query , ( current , path ) => current . Include ( path ) ) ;
61- }
62-
63- // 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.
64- protected override T GetQuery ( TKey key )
65- {
66- return DbSet . Find ( key ) ;
67- }
68-
69- public override void Dispose ( )
70- {
71- Dispose ( true ) ;
72- GC . SuppressFinalize ( this ) ;
73- }
74-
75- protected virtual void Dispose ( bool disposing )
76- {
77- if ( ! disposing ) return ;
78- if ( Context == null ) return ;
79-
80- Context . Dispose ( ) ;
81- Context = null ;
82- }
83-
84- private TKey GeneratePrimaryKey ( )
85- {
86- if ( typeof ( TKey ) == typeof ( Guid ) )
87- {
88- return ( TKey ) Convert . ChangeType ( Guid . NewGuid ( ) , typeof ( TKey ) ) ;
89- }
90-
91- if ( typeof ( TKey ) == typeof ( string ) )
92- {
93- return ( TKey ) Convert . ChangeType ( Guid . NewGuid ( ) . ToString ( ) , typeof ( TKey ) ) ;
94- }
95-
96- throw new InvalidOperationException ( "Primary key could not be generated. This only works for GUID, Int32 and String." ) ;
97- }
98- }
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 Ef5RepositoryBase < T , TKey > : LinqRepositoryBase < T , TKey > where T : class , new ( )
12+ {
13+ protected IDbSet < T > DbSet { get ; private set ; }
14+ protected DbContext Context { get ; private set ; }
15+
16+ internal Ef5RepositoryBase ( DbContext dbContext , ICachingStrategy < T , TKey > cachingStrategy = null ) : base ( cachingStrategy )
17+ {
18+ Initialize ( dbContext , cachingStrategy != null ) ;
19+ }
20+
21+ private void Initialize ( DbContext dbContext , bool hasCaching )
22+ {
23+ Context = dbContext ;
24+ DbSet = Context . Set < T > ( ) ;
25+
26+ // this could solve issue #50 where DynamicProxy objects mess up the cache
27+ // if (hasCaching)
28+ // {
29+ // Context.Configuration.ProxyCreationEnabled = false;
30+ // }
31+ }
32+
33+ protected override void AddItem ( T entity )
34+ {
35+ if ( typeof ( TKey ) == typeof ( Guid ) || typeof ( TKey ) == typeof ( string ) )
36+ {
37+ TKey id ;
38+ if ( GetPrimaryKey ( entity , out id ) && Equals ( id , default ( TKey ) ) )
39+ {
40+ id = GeneratePrimaryKey ( ) ;
41+ SetPrimaryKey ( entity , id ) ;
42+ }
43+ }
44+ DbSet . Add ( entity ) ;
45+ }
46+
47+ protected override void DeleteItem ( T entity )
48+ {
49+ DbSet . Remove ( entity ) ;
50+ }
51+
52+ protected override void UpdateItem ( T entity )
53+ {
54+ // mark this entity as modified, in case it is not currently attached to this context
55+ Context . Entry ( entity ) . State = EntityState . Modified ;
56+ }
57+
58+ protected override void SaveChanges ( )
59+ {
60+ Context . SaveChanges ( ) ;
61+ }
62+
63+ protected override IQueryable < T > BaseQuery ( IFetchStrategy < T > fetchStrategy = null )
64+ {
65+ var query = DbSet . AsQueryable ( ) ;
66+ return fetchStrategy == null ? query : fetchStrategy . IncludePaths . Aggregate ( query , ( current , path ) => current . Include ( path ) ) ;
67+ }
68+
69+ // 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.
70+ protected override T GetQuery ( TKey key )
71+ {
72+ return DbSet . Find ( key ) ;
73+ }
74+
75+ public override void Dispose ( )
76+ {
77+ Dispose ( true ) ;
78+ GC . SuppressFinalize ( this ) ;
79+ }
80+
81+ protected virtual void Dispose ( bool disposing )
82+ {
83+ if ( ! disposing ) return ;
84+ if ( Context == null ) return ;
85+
86+ Context . Dispose ( ) ;
87+ Context = null ;
88+ }
89+
90+ private TKey GeneratePrimaryKey ( )
91+ {
92+ if ( typeof ( TKey ) == typeof ( Guid ) )
93+ {
94+ return ( TKey ) Convert . ChangeType ( Guid . NewGuid ( ) , typeof ( TKey ) ) ;
95+ }
96+
97+ if ( typeof ( TKey ) == typeof ( string ) )
98+ {
99+ return ( TKey ) Convert . ChangeType ( Guid . NewGuid ( ) . ToString ( ) , typeof ( TKey ) ) ;
100+ }
101+
102+ throw new InvalidOperationException ( "Primary key could not be generated. This only works for GUID, Int32 and String." ) ;
103+ }
104+ }
99105}
0 commit comments