Skip to content

Commit 5b2624d

Browse files
author
Jeff Treuting
committed
Cleaned up some resharper suggestions
1 parent df71dd6 commit 5b2624d

File tree

7 files changed

+331
-361
lines changed

7 files changed

+331
-361
lines changed

SharpRepository.Caching.Memcached/MemCachedCachingProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public bool Get<T>(string key, out T value)
145145
{
146146
value = Client.Get<T>(key);
147147

148-
if (Object.Equals(value, default(T)))
148+
if (Equals(value, default(T)))
149149
{
150150
value = default(T);
151151
return false;

SharpRepository.Caching.Redis/RedisCachingProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public bool Get<T>(string key, out T value)
104104
{
105105
value = Client.Get<T>(key);
106106

107-
if (Object.Equals(value, default(T)))
107+
if (Equals(value, default(T)))
108108
{
109109
value = default(T);
110110
return false;
Lines changed: 98 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,99 @@
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

Comments
 (0)