11using System ;
2- using System . Collections ;
3- using System . Reflection ;
42using System . Collections . Generic ;
53using System . Linq ;
64using System . Linq . Expressions ;
75using SharpRepository . Repository . Caching ;
86using SharpRepository . Repository . FetchStrategies ;
9- using SharpRepository . Repository . Helpers ;
107using SharpRepository . Repository . Queries ;
118using SharpRepository . Repository . Specifications ;
129using SharpRepository . Repository . Transactions ;
@@ -69,7 +66,30 @@ public override IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>
6966 ) ;
7067 }
7168
72- public override T Get ( params object [ ] keys )
69+ // These are the actual implementation that the derived class needs to implement
70+ protected abstract T GetQuery ( params object [ ] keys ) ;
71+
72+ public bool Exists ( params object [ ] keys )
73+ {
74+ return TryGet ( out T entity , keys ) ;
75+ }
76+
77+ public bool TryGet ( out T entity , params object [ ] keys )
78+ {
79+ entity = default ( T ) ;
80+
81+ try
82+ {
83+ entity = Get ( keys ) ;
84+ return entity != null ;
85+ }
86+ catch ( Exception )
87+ {
88+ return false ;
89+ }
90+ }
91+
92+ public T Get ( params object [ ] keys )
7393 {
7494 return _queryManager . ExecuteGet (
7595 ( ) => GetQuery ( keys ) ,
@@ -167,7 +187,16 @@ public override void Add(T entity)
167187 _queryManager . OnItemAdded ( keys , entity ) ;
168188 }
169189
170- public void Delete ( T entity )
190+ public void Delete ( params object [ ] keys )
191+ {
192+ var entity = Get ( keys ) ;
193+
194+ if ( entity == null ) throw new ArgumentException ( "No entity exists with these keys." , "keys" ) ;
195+
196+ Delete ( entity ) ;
197+ }
198+
199+ public override void Delete ( T entity )
171200 {
172201 if ( entity == null ) throw new ArgumentNullException ( "entity" ) ;
173202
@@ -193,6 +222,64 @@ public override void Update(T entity)
193222 _queryManager . OnItemUpdated ( keys , entity ) ;
194223 }
195224
225+ protected virtual bool GetPrimaryKeys ( T entity , out object [ ] keys )
226+ {
227+ keys = null ;
228+ var propInfo = GetPrimaryKeyPropertyInfo ( ) ;
229+
230+ // if there is no property that matches then return false
231+ if ( propInfo == null )
232+ return false ;
233+
234+ keys = propInfo . Select ( info => info . GetValue ( entity , null ) ) . ToArray ( ) ;
235+
236+ return true ;
237+ }
238+
239+ protected virtual bool SetPrimaryKey ( T entity , object [ ] keys )
240+ {
241+ var propInfo = GetPrimaryKeyPropertyInfo ( ) ;
242+
243+ // if there is no property that matches then return false
244+ if ( propInfo == null || keys == null || propInfo . Length != keys . Length )
245+ return false ;
246+
247+ var i = 0 ;
248+ foreach ( var key in keys )
249+ {
250+ propInfo [ i ] . SetValue ( entity , key , null ) ;
251+ i ++ ;
252+ }
253+
254+ return true ;
255+ }
256+
257+ protected virtual ISpecification < T > ByPrimaryKeySpecification ( object [ ] keys )
258+ {
259+ var propInfo = GetPrimaryKeyPropertyInfo ( ) ;
260+ if ( propInfo == null || keys == null || propInfo . Length != keys . Length )
261+ return null ;
262+
263+ ISpecification < T > specification = null ;
264+ var parameter = Expression . Parameter ( typeof ( T ) , "x" ) ;
265+
266+ var i = 0 ;
267+ foreach ( var lambda in keys . Select ( key => Expression . Lambda < Func < T , bool > > (
268+ Expression . Equal (
269+ Expression . PropertyOrField ( parameter , propInfo [ i ] . Name ) ,
270+ Expression . Constant ( key )
271+ ) ,
272+ parameter
273+ ) )
274+ )
275+ {
276+ specification = specification == null ? new Specification < T > ( lambda ) : specification . And ( lambda ) ;
277+ i ++ ;
278+ }
279+
280+ return specification ;
281+ }
282+
196283 private void Save ( )
197284 {
198285 SaveChanges ( ) ;
@@ -401,7 +488,7 @@ public override void Add(T entity)
401488 _queryManager . OnItemAdded ( key , key2 , entity ) ;
402489 }
403490
404- public void Delete ( T entity )
491+ public override void Delete ( T entity )
405492 {
406493 if ( entity == null ) throw new ArgumentNullException ( "entity" ) ;
407494
@@ -701,7 +788,7 @@ public override void Add(T entity)
701788 _queryManager . OnItemAdded ( key , key2 , key3 , entity ) ;
702789 }
703790
704- public void Delete ( T entity )
791+ public override void Delete ( T entity )
705792 {
706793 if ( entity == null ) throw new ArgumentNullException ( "entity" ) ;
707794
0 commit comments