Skip to content

Commit

Permalink
Add OnAfterLoad handling to Repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
RickStrahl committed Jan 30, 2017
1 parent 95cd233 commit 31ab303
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
53 changes: 28 additions & 25 deletions src/AlbumViewerBusiness/AlbumRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public AlbumRepository(AlbumViewerContext context)
: base(context)
{ }


/// <summary>
/// Loads and individual album.
///
Expand All @@ -25,31 +25,34 @@ public AlbumRepository(AlbumViewerContext context)
/// </summary>
/// <param name="objId">Album Id</param>
/// <returns></returns>
public override async Task<Album> Load(object albumId)
{
Album album = null;
try
{
int id = (int) albumId;
album = await Context.Albums
.Include(ctx => ctx.Tracks)
.Include(ctx => ctx.Artist)
.FirstOrDefaultAsync(alb => alb.Id == id);
}
catch (InvalidOperationException)
{
// Handles errors where an invalid Id was passed, but SQL is valid
SetError("Couldn't load album - invalid album id specified.");
return null;
}
catch (Exception ex)
{
// handles Sql errors
SetError(ex);
}
public override async Task<Album> Load(object albumId)
{
Album album = null;
try
{
int id = (int) albumId;
album = await Context.Albums
.Include(ctx => ctx.Tracks)
.Include(ctx => ctx.Artist)
.FirstOrDefaultAsync(alb => alb.Id == id);

if (album != null)
OnAfterLoaded(album);
}
catch (InvalidOperationException)
{
// Handles errors where an invalid Id was passed, but SQL is valid
SetError("Couldn't load album - invalid album id specified.");
return null;
}
catch (Exception ex)
{
// handles Sql errors
SetError(ex);
}

return album;
}
return album;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,7 @@ protected virtual async Task<TEntity> LoadBase(Expression<Func<TEntity, bool>> w
{
var entity = await DbSet.FirstOrDefaultAsync(whereClauseLambda);

if (entity != null)
OnAfterLoaded(entity);
else
SetError("Unable to find matching entity.");


return entity;
}
catch (InvalidOperationException)
Expand Down

0 comments on commit 31ab303

Please sign in to comment.