Skip to content

Commit 84495c5

Browse files
author
Jeff Treuting
committed
Merge compound key branch
2 parents 86ba55b + 3efc0b2 commit 84495c5

64 files changed

Lines changed: 5714 additions & 318 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SharpRepository.Db4oRepository/Db4oConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
2222

2323
return new Db4oRepository<T, TKey>(RepositoryConfiguration["directory"]);
2424
}
25+
26+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
27+
{
28+
throw new NotImplementedException();
29+
}
2530
}
2631
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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+
}

SharpRepository.Ef5Repository/Ef5ConfigRepositoryFactory.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,30 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
5555

5656
return new Ef5Repository<T, TKey>(dbContext);
5757
}
58+
59+
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
60+
{
61+
// check for required parameters
62+
if (String.IsNullOrEmpty(RepositoryConfiguration["connectionString"]))
63+
{
64+
throw new ConfigurationErrorsException("The connectionString attribute is required in order to use the Ef5Repository via the configuration file.");
65+
}
66+
67+
Type dbContextType = null;
68+
69+
if (!String.IsNullOrEmpty(RepositoryConfiguration["dbContextType"]))
70+
{
71+
dbContextType = Type.GetType(RepositoryConfiguration["dbContextType"]);
72+
}
73+
74+
var connectionString = RepositoryConfiguration["connectionString"];
75+
76+
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
77+
var dbContext = dbContextType == null ?
78+
new DbContext(connectionString) :
79+
(DbContext)Activator.CreateInstance(dbContextType, connectionString);
80+
81+
return new Ef5Repository<T, TKey, TKey2>(dbContext);
82+
}
5883
}
5984
}

SharpRepository.Ef5Repository/Ef5Repository.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55

66
namespace SharpRepository.Ef5Repository
77
{
8+
public class Ef5Repository<T, TKey, TKey2> : Ef5CompoundKeyRepositoryBase<T, TKey, TKey2> where T : class, new()
9+
{
10+
public Ef5Repository(DbContext dbContext, ICompoundKeyCachingStrategy<T, TKey, TKey2> cachingStrategy = null)
11+
: base(dbContext, cachingStrategy)
12+
{
13+
}
14+
}
15+
16+
public class Ef5CompoundKeyRepository<T> : Ef5CompoundKeyRepositoryBase<T> where T : class, new()
17+
{
18+
public Ef5CompoundKeyRepository(DbContext dbContext, ICompoundKeyCachingStrategy<T> cachingStrategy = null)
19+
: base(dbContext, cachingStrategy)
20+
{
21+
}
22+
}
23+
824
/// <summary>
925
/// Entity Framework repository layer
1026
/// </summary>

SharpRepository.Ef5Repository/SharpRepository.Ef5Repository.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="..\CommonAssembly.cs">
5151
<Link>Properties\CommonAssembly.cs</Link>
5252
</Compile>
53+
<Compile Include="Ef5CompoundKeyRepositoryBase.cs" />
5354
<Compile Include="Ef5ConfigRepositoryFactory.cs" />
5455
<Compile Include="Ef5Repository.cs" />
5556
<Compile Include="Ef5RepositoryBase.cs" />

0 commit comments

Comments
 (0)