@@ -25,18 +25,14 @@ public List<object> Get()
25
25
26
26
public object Get ( string id )
27
27
{
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 ) ;
33
30
}
34
31
35
32
private object GetRelated ( string id , RelationalRoute relationalRoute )
36
33
{
37
- // HACK: this would rely on lazy loading to work...will probably fail
38
34
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 ) ;
40
36
}
41
37
42
38
private IQueryable GetDbSetFromContext ( string propName )
@@ -47,21 +43,26 @@ private IQueryable GetDbSetFromContext(string propName)
47
43
48
44
private object GetEntityById ( Type modelType , string id , string includedRelationship )
49
45
{
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
52
47
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 ) } ) ;
56
51
52
+ // include relationships if requested
57
53
if ( ! string . IsNullOrEmpty ( includedRelationship ) )
58
54
{
59
55
var includeMethod = dataAccessorInstance . GetType ( ) . GetMethod ( "IncludeEntity" ) ;
60
56
var genericIncludeMethod = includeMethod . MakeGenericMethod ( modelType ) ;
61
57
dbSet = genericIncludeMethod . Invoke ( dataAccessorInstance , new [ ] { dbSet , includedRelationship . ToProperCase ( ) } ) ;
62
58
}
63
59
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 ;
65
66
}
66
67
67
68
public void Add ( object entity )
0 commit comments