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 )
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- // TODO: use logic like this to override GetPrimaryKey
70- // below is using the older EF stuff and it doesn't translate exactly
71- //private EntityKey GetEntityKey(object keyValue)
72- //{
73- // var entitySetName = GetEntityName();
74- // var keyPropertyName = _dbSet.EntitySet.ElementType.KeyMembers[0].ToString();
75- // return new EntityKey(entitySetName, new[] { new EntityKeyMember(keyPropertyName, keyValue) });
76-
77- //}
78-
79- //private string GetEntityName()
80- //{
81- // return string.Format("{0}.{1}", Context.DefaultContainerName, QueryBase.EntitySet.Name);
82- //}
83-
84- public override void Dispose ( )
85- {
86- Dispose ( true ) ;
87- GC . SuppressFinalize ( this ) ;
88- }
89-
90- protected virtual void Dispose ( bool disposing )
91- {
92- if ( ! disposing ) return ;
93- if ( Context == null ) return ;
94-
95- Context . Dispose ( ) ;
96- Context = null ;
97- }
98-
99- private TKey GeneratePrimaryKey ( )
100- {
101- if ( typeof ( TKey ) == typeof ( Guid ) )
102- {
103- return ( TKey ) Convert . ChangeType ( Guid . NewGuid ( ) , typeof ( TKey ) ) ;
104- }
105-
106- if ( typeof ( TKey ) == typeof ( string ) )
107- {
108- return ( TKey ) Convert . ChangeType ( Guid . NewGuid ( ) . ToString ( ) , typeof ( TKey ) ) ;
109- }
110-
111- throw new InvalidOperationException ( "Primary key could not be generated. This only works for GUID, Int32 and String." ) ;
112- }
113- }
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+ }
11499}
0 commit comments