@@ -25,18 +25,14 @@ public List<object> Get()
2525
2626 public object Get ( string id )
2727 {
28- if ( _context . Route is RelationalRoute )
29- {
30- return GetRelated ( id , _context . Route as RelationalRoute ) ;
31- }
32- return GetEntityById ( _context . Route . BaseModelType , id , null ) ;
28+ var route = _context . Route as RelationalRoute ;
29+ return route != null ? GetRelated ( id , route ) : GetEntityById ( _context . Route . BaseModelType , id , null ) ;
3330 }
3431
3532 private object GetRelated ( string id , RelationalRoute relationalRoute )
3633 {
37- // HACK: this would rely on lazy loading to work...will probably fail
3834 var entity = GetEntityById ( relationalRoute . BaseModelType , id , relationalRoute . RelationshipName ) ;
39- return relationalRoute . BaseModelType . GetProperties ( ) . FirstOrDefault ( pi => pi . Name . ToCamelCase ( ) == relationalRoute . RelationshipName . ToCamelCase ( ) ) . GetValue ( entity ) ;
35+ return relationalRoute . BaseModelType . GetProperties ( ) . FirstOrDefault ( pi => pi . Name . ToCamelCase ( ) == relationalRoute . RelationshipName . ToCamelCase ( ) ) ? . GetValue ( entity ) ;
4036 }
4137
4238 private IQueryable GetDbSetFromContext ( string propName )
@@ -47,21 +43,26 @@ private IQueryable GetDbSetFromContext(string propName)
4743
4844 private object GetEntityById ( Type modelType , string id , string includedRelationship )
4945 {
50- // HACK: I _believe_ by casting to IEnumerable, we are loading all records into memory, if so... find a better way...
51- // Also, we are making a BIG assumption that the resource has an attribute Id and not ResourceId which is allowed by EF
46+ // get generic dbSet
5247 var dataAccessorInstance = Activator . CreateInstance ( typeof ( GenericDataAccess ) ) ;
53- var dataAccessorMethod = dataAccessorInstance . GetType ( ) . GetMethod ( "GetDbSet" ) ;
54- var genericMethod = dataAccessorMethod . MakeGenericMethod ( modelType ) ;
55- var dbSet = genericMethod . Invoke ( dataAccessorInstance , new [ ] { ( ( DbContext ) _context . DbContext ) } ) ;
48+ var dataAccessorGetDbSetMethod = dataAccessorInstance . GetType ( ) . GetMethod ( "GetDbSet" ) ;
49+ var genericGetDbSetMethod = dataAccessorGetDbSetMethod . MakeGenericMethod ( modelType ) ;
50+ var dbSet = genericGetDbSetMethod . Invoke ( dataAccessorInstance , new [ ] { ( ( DbContext ) _context . DbContext ) } ) ;
5651
52+ // include relationships if requested
5753 if ( ! string . IsNullOrEmpty ( includedRelationship ) )
5854 {
5955 var includeMethod = dataAccessorInstance . GetType ( ) . GetMethod ( "IncludeEntity" ) ;
6056 var genericIncludeMethod = includeMethod . MakeGenericMethod ( modelType ) ;
6157 dbSet = genericIncludeMethod . Invoke ( dataAccessorInstance , new [ ] { dbSet , includedRelationship . ToProperCase ( ) } ) ;
6258 }
6359
64- return ( dbSet as IEnumerable < dynamic > ) . SingleOrDefault ( x => x . Id . ToString ( ) == id ) ;
60+ // get the SingleOrDefault value by Id
61+ var dataAccessorSingleOrDefaultMethod = dataAccessorInstance . GetType ( ) . GetMethod ( "SingleOrDefault" ) ;
62+ var genericSingleOrDefaultMethod = dataAccessorSingleOrDefaultMethod . MakeGenericMethod ( modelType ) ;
63+ var entity = genericSingleOrDefaultMethod . Invoke ( dataAccessorInstance , new [ ] { dbSet , "Id" , id } ) ;
64+
65+ return entity ;
6566 }
6667
6768 public void Add ( object entity )
0 commit comments