@@ -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 ) ;
0 commit comments