Skip to content

Commit f8db873

Browse files
author
jtreuting
committed
Exception handling
Removed custom exception Added ArgumentNullExceptions on public methods where applicable Added InvalidOperationExceptions for the PK type in GeneratePrimaryKey and when the entity type and the key type are the same
1 parent eec918e commit f8db873

7 files changed

Lines changed: 49 additions & 50 deletions

File tree

SharpRepository.InMemoryRepository/InMemoryRepositoryBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ private TKey GeneratePrimaryKey()
121121
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
122122
}
123123

124-
throw new Exceptions.PrimaryKeyInvalidException(
125-
"Primary key could not be generated. This only works for GUID and Int32.");
124+
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID and Int32.");
126125
}
127126

128127
public override string ToString()

SharpRepository.RavenDbRepository/RavenDbRepositoryBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ private TKey GeneratePrimaryKey()
118118
return (TKey) Convert.ChangeType(TypeName + "s/", typeof (string));
119119
}
120120

121-
throw new Repository.Exceptions.PrimaryKeyInvalidException(
122-
"Primary key could not be generated. This only works for GUID, Int32 and String.");
121+
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and String.");
123122
}
124123
}
125124
}

SharpRepository.Repository/Exceptions/PrimaryKeyInvalidException.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

SharpRepository.Repository/FetchStrategies/FetchStrategyExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static string ToPropertyName<T>(this Expression<Func<T, object>> selector
2020
var me = selector.Body as MemberExpression;
2121
if (me == null)
2222
{
23-
throw new ArgumentException("MemberExpression expected.");
23+
throw new ArgumentException("MemberExpression expected.", "selector");
2424
}
2525

2626
var trimPrefix = me.ToString();

SharpRepository.Repository/RepositoryBase.cs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ protected RepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null)
4040
if (typeof(T) == typeof(TKey))
4141
{
4242
// this check is mainly because of the overloaded Delete methods Delete(T) and Delete(TKey), ambiguous reference if the generics are the same
43-
throw new Exception("The repository type and the primary key type can not be the same.");
43+
throw new InvalidOperationException("The repository type and the primary key type can not be the same.");
4444
}
4545

4646
_cachingStrategy = cachingStrategy ?? new NoCachingStrategy<T, TKey>();
4747
_typeName = typeof (T).Name;
4848
_queryManager = new QueryManager<T, TKey>(_cachingStrategy);
4949
}
5050

51-
public abstract IQueryable<T> AsQueryable();
52-
51+
public abstract IQueryable<T> AsQueryable();
52+
53+
// These are the actual implementation that the derived class needs to implement
5354
protected abstract IEnumerable<T> GetAllQuery();
5455
protected abstract IEnumerable<T> GetAllQuery(IQueryOptions<T> queryOptions);
5556

@@ -68,15 +69,16 @@ public IEnumerable<T> GetAll(IQueryOptions<T> queryOptions)
6869

6970
public IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
7071
{
72+
if (selector == null) throw new ArgumentNullException("selector");
73+
7174
return GetAll(queryOptions)
7275
.AsQueryable()
7376
.Select(selector);
7477
}
7578

76-
// This is the actual implementation that the derived class needs to implement
79+
// These are the actual implementation that the derived class needs to implement
7780
protected abstract T GetQuery(TKey key);
7881

79-
// this is what is called and uses the Write Through caching logic
8082
public T Get(TKey key)
8183
{
8284
return _queryManager.ExecuteGet(
@@ -87,6 +89,8 @@ public T Get(TKey key)
8789

8890
public TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector)
8991
{
92+
if (selector == null) throw new ArgumentNullException("selector");
93+
9094
var result = Get(key);
9195
if (result == null)
9296
return default(TResult);
@@ -95,13 +99,14 @@ public TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector)
9599
return results.AsQueryable().Select(selector).FirstOrDefault();
96100
}
97101

98-
// This is the actual implementation that the derived class needs to implement
102+
// These are the actual implementation that the derived class needs to implement
99103
protected abstract IEnumerable<T> FindAllQuery(ISpecification<T> criteria);
100104
protected abstract IEnumerable<T> FindAllQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions);
101105

102-
// this is what is called and uses Generational Caching
103106
public IEnumerable<T> FindAll(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null)
104107
{
108+
if (criteria == null) throw new ArgumentNullException("criteria");
109+
105110
return _queryManager.ExecuteFindAll(
106111
() => queryOptions == null ? FindAllQuery(criteria).ToList() : FindAllQuery(criteria, queryOptions).ToList(),
107112
criteria,
@@ -111,24 +116,34 @@ public IEnumerable<T> FindAll(ISpecification<T> criteria, IQueryOptions<T> query
111116

112117
public IEnumerable<TResult> FindAll<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
113118
{
119+
if (criteria == null) throw new ArgumentNullException("criteria");
120+
114121
return FindAll(criteria, queryOptions).AsQueryable().Select(selector).ToList();
115122
}
116123

117124
public IEnumerable<T> FindAll(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null)
118125
{
126+
if (predicate == null) throw new ArgumentNullException("predicate");
127+
119128
return FindAll(new Specification<T>(predicate), queryOptions);
120129
}
121130

122131
public IEnumerable<TResult> FindAll<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
123132
{
133+
if (predicate == null) throw new ArgumentNullException("predicate");
134+
if (selector == null) throw new ArgumentNullException("selector");
135+
124136
return FindAll(new Specification<T>(predicate), selector, queryOptions);
125137
}
126138

139+
// These are the actual implementation that the derived class needs to implement
127140
protected abstract T FindQuery(ISpecification<T> criteria);
128141
protected abstract T FindQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions);
129142

130143
public T Find(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null)
131144
{
145+
if (criteria == null) throw new ArgumentNullException("criteria");
146+
132147
return _queryManager.ExecuteFind(
133148
() => queryOptions == null ? FindQuery(criteria) : FindQuery(criteria, queryOptions),
134149
criteria,
@@ -138,6 +153,9 @@ public T Find(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null)
138153

139154
public TResult Find<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
140155
{
156+
if (criteria == null) throw new ArgumentNullException("criteria");
157+
if (selector == null) throw new ArgumentNullException("selector");
158+
141159
var result = Find(criteria, queryOptions);
142160
if (result == null)
143161
return default(TResult);
@@ -148,18 +166,26 @@ public TResult Find<TResult>(ISpecification<T> criteria, Expression<Func<T, TRes
148166

149167
public T Find(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null)
150168
{
169+
if (predicate == null) throw new ArgumentNullException("predicate");
170+
151171
return Find(new Specification<T>(predicate), queryOptions);
152172
}
153173

154174
public TResult Find<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
155175
{
176+
if (predicate == null) throw new ArgumentNullException("predicate");
177+
if (selector == null) throw new ArgumentNullException("selector");
178+
156179
return Find(new Specification<T>(predicate), selector, queryOptions);
157180
}
158181

182+
// This is the actual implementation that the derived class needs to implement
159183
protected abstract void AddItem(T entity);
160184

161185
public void Add(T entity)
162186
{
187+
if (entity == null) throw new ArgumentNullException("entity");
188+
163189
ProcessAdd(entity, BatchMode);
164190
}
165191

@@ -178,16 +204,21 @@ private void ProcessAdd(T entity, bool batchMode)
178204

179205
public void Add(IEnumerable<T> entities)
180206
{
207+
if (entities == null) throw new ArgumentNullException("entities");
208+
181209
foreach (var entity in entities)
182210
{
183211
Add(entity);
184212
}
185213
}
186214

215+
// This is the actual implementation that the derived class needs to implement
187216
protected abstract void DeleteItem(T entity);
188217

189218
public void Delete(T entity)
190219
{
220+
if (entity == null) throw new ArgumentNullException("entity");
221+
191222
ProcessDelete(entity, BatchMode);
192223
}
193224

@@ -216,18 +247,18 @@ public void Delete(TKey key)
216247
{
217248
var entity = Get(key);
218249

219-
if (entity == default(T))
220-
{
221-
throw new ArgumentException("No entity exists with this key."); // TODO: should this be a custom exception?
222-
}
250+
if (entity == null) throw new ArgumentException("No entity exists with this key.", "key");
223251

224252
Delete(entity);
225253
}
226254

255+
// This is the actual implementation that the derived class needs to implement
227256
protected abstract void UpdateItem(T entity);
228257

229258
public void Update(T entity)
230259
{
260+
if (entity == null) throw new ArgumentNullException("entity");
261+
231262
ProcessUpdate(entity, BatchMode);
232263
}
233264

@@ -246,6 +277,8 @@ private void ProcessUpdate(T entity, bool batchMode)
246277

247278
public void Update(IEnumerable<T> entities)
248279
{
280+
if (entities == null) throw new ArgumentNullException("entities");
281+
249282
foreach (var entity in entities)
250283
{
251284
Update(entity);

SharpRepository.Repository/SharpRepository.Repository.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
<Compile Include="Queries\QueryManager.cs" />
7575
<Compile Include="Queries\PagingOptions.cs" />
7676
<Compile Include="Queries\IQueryOptions.cs" />
77-
<Compile Include="Exceptions\PrimaryKeyInvalidException.cs" />
7877
<Compile Include="FetchStrategies\FetchStrategyExtensions.cs" />
7978
<Compile Include="FetchStrategies\GenericFetchStrategy.cs" />
8079
<Compile Include="FetchStrategies\IFetchStrategy.cs" />

SharpRepository.XmlRepository/XmlRepositoryBase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ private TKey GeneratePrimaryKey()
161161
var nextInt = Convert.ToInt32(pkValue) + 1;
162162
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
163163
}
164-
165-
throw new Repository.Exceptions.PrimaryKeyInvalidException(
166-
"Primary key could not be generated. This only works for GUID and Int32.");
164+
165+
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID and Int32.");
167166
}
168167

169168
public override string ToString()

0 commit comments

Comments
 (0)