Skip to content

Commit

Permalink
Update MVC pages (incomplete sample).
Browse files Browse the repository at this point in the history
Add example of using DbContext without DI
  • Loading branch information
RickStrahl committed Jul 26, 2015
1 parent 306b9f9 commit 3f1e569
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 59 deletions.
60 changes: 37 additions & 23 deletions src/AlbumViewerAspNet5/Controllers/AlbumViewerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ namespace MusicStoreVNext
public class AlbumViewerApiController : Controller
{
AlbumViewerContext context;
IServiceProvider serviceProvider;

public AlbumViewerApiController(AlbumViewerContext ctx)
public AlbumViewerApiController(AlbumViewerContext ctx, IServiceProvider svcProvider)
{
context = ctx;
serviceProvider = svcProvider;
}


Expand All @@ -45,16 +47,11 @@ public object HelloWorld(string name = null)
[Route("api/albums")]
public async Task<IEnumerable<Album>> Albums()
{
List<Album> result;

// For demonstration create Context Instance manually
using (var ctxt = context) // new AlbumViewerContext())
{
result = await ctxt.Albums.Include(ctx => ctx.Tracks)
.Include(ctx => ctx.Artist)
.OrderBy(alb => alb.Title)
.ToListAsync();
}
var result = await context.Albums
.Include(ctx => ctx.Tracks)
.Include(ctx => ctx.Artist)
.OrderBy(alb => alb.Title)
.ToListAsync();

return result;
}
Expand Down Expand Up @@ -86,19 +83,36 @@ public async Task<Album> Album([FromBody] Album postedAlbum)
[Route("api/artists")]
public async Task<IEnumerable> Artists()
{
var result = await context.Artists
.OrderBy(art => art.ArtistName)
.Select(art => new
{
ArtistName = art.ArtistName,
Description = art.Description,
ImageUrl = art.ImageUrl,
Id = art.Id,
AlbumCount = context.Albums.Count(alb => alb.ArtistId == art.Id)
})
.ToListAsync();
return result;
// For demonstration use a manually instantiated instance of
// the dbContext.
//
// Note the need to pass in serviceProvider or *something*
// that gives access to the Configuration so the connection
// string from config can be found.
//
// This overload is what EF uses internally to create a
// to create a configured instance of a context.
//
// Scoped instances of Context can be problematic in some
// controllers - you're incurring overhead for each hit even
// if context is isn't used in an action. If you need multiple instances
// of contexts you may also need to manually instantiate.
using (var ctxt = new AlbumViewerContext(serviceProvider))
{
var result = await ctxt.Artists
.OrderBy(art => art.ArtistName)
.Select(art => new
{
art.ArtistName,
art.Description,
art.ImageUrl,
art.Id,
AlbumCount = context.Albums.Count(alb => alb.ArtistId == art.Id)
})
.ToListAsync();

return result;
}
}

[HttpGet]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ namespace MusicStoreVNext
{
public class AlbumViewerMvcController : Controller
{
AlbumViewerContext context;
AlbumViewerContext context;
private readonly IServiceProvider serviceProvider;

public AlbumViewerMvcController(AlbumViewerContext ctx)
public AlbumViewerMvcController(AlbumViewerContext ctx, IServiceProvider svcProvider)
{
context = ctx;
serviceProvider = svcProvider;
//this.environment = environment;
}

Expand All @@ -31,6 +33,7 @@ public async Task<ActionResult> Index()

public async Task<ActionResult> Albums()
{

var result = await context.Albums
.Include(ctx => ctx.Tracks)
.Include(ctx => ctx.Artist)
Expand Down
51 changes: 27 additions & 24 deletions src/AlbumViewerAspNet5/Views/AlbumViewerMvc/Album.cshtml
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
@model MusicStoreBusiness.Album
<div ng-controller="albumController as view" class="container">
@Model.Title

Tag Helper: <input type="text" for="Title" class="inputfield" />
@model AlbumViewerBusiness.Album
<div class="container">

<div class="btn-group" style="margin-bottom: 10px;">
<a href="#/albums" class="btn btn-sm btn-default"><i class="fa fa-list"></i> List</a>
<a href="#/album/edit/{{view.album.Id}}" class="btn btn-sm btn-default"><i class="fa fa-edit"></i> Edit</a>
<a href="{{view.album.AmazonUrl}}" class="btn btn-sm btn-default"><i class="fa fa-dollar"></i> Buy</a>
<a href="@Url.Action("Albums")" class="btn btn-sm btn-default"><i class="fa fa-list"></i> List</a>
<a href="@Url.Action("edit","album", new { Model.Id})" class="btn btn-sm btn-default"><i class="fa fa-edit"></i> Edit</a>
<a href="@Model.AmazonUrl" class="btn btn-sm btn-default"><i class="fa fa-dollar"></i> Buy</a>
<button ng-click="view.deleteAlbum(view.album)" class="btn btn-sm btn-default"><i class="fa fa-remove"></i> Delete</button>
</div>

<div class="separator"></div>


<div class="row">
<div class="col-sm-3">
<img ng-src="{{view.album.ImageUrl}}" class="album-image-big" />
<img src="@Model.ImageUrl" class="album-image-big" />
</div>
<div class="col-sm-9">
<h2 class="album-title-big">
@Model.Title
</h2>
<div class="album-title-big">
{{view.album.Title}}
@Model.Title
</div>

<div class="album-artist">

by <a href="#/artist/{{view.album.ArtistId}}">{{view.album.Artist.ArtistName}}</a>
{{(view.album.Year ? 'in ' + view.album.Year : '')}}
by <a href="#/artist/@Model.ArtistId">@Model.Artist.ArtistName</a>
@( Model.Year > 0 ? "in " + Model.Year : "" )
</div>
<div>
@Model.Description
</div>
<div ng-bind-html="view.album.Description | linebreakFilter"></div>

<hr />
<hr/>

<table class=" table table-striped small">
<tr ng-repeat="song in view.album.Tracks">
<td>{{song.SongName}}</td>
<td>{{song.Length}}</td>
</tr>
@foreach (var song in @Model.Tracks)
{
<tr>
<td>@song.SongName</td>
<td>@song.Length</td>
</tr>
}
</table>

<hr />
<hr/>
More from
<a href="#/artist/{{view.album.ArtistId}}">
{{view.album.Artist.ArtistName}}<br />
<img ng-src="{{view.album.Artist.ImageUrl}}" style="height: 100px; border-radius: 5px;box-shadow: 2px 2px 3px #535353" />
<a href="artist/@Model.ArtistId">
@Model.Artist.ArtistName<br/>
<img src="@Model.Artist.ImageUrl" style="height: 100px; border-radius: 5px; box-shadow: 2px 2px 3px #535353"/>
</a>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/AlbumViewerAspNet5/Views/AlbumViewerMvc/Albums.cshtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@model IEnumerable<MusicStoreBusiness.Album>
@model IEnumerable<AlbumViewerBusiness.Album>

<div class="container container-fluid">
<button class="btn btn-sm btn-success pull-right" <i class="fa fa-plus"></i> Add Album</button>
<button class="btn btn-sm btn-success pull-right"><i class="fa fa-plus"></i> Add Album</button>
<div class="page-header-text"><i class="fa fa-list"></i> Albums <span class="badge">@Model.Count()</span></div>

<hr style="margin-top: 6px;" />
@foreach (var album in Model.ToList())
{
Expand All @@ -11,7 +11,7 @@
<a href="album/@album.Id/edit"><i class="fa fa-edit" title="Edit"></i></a> &nbsp;
<a ng-click="view.deleteAlbum(album)"><i class="fa fa-remove" title="Delete"></i></a>
</div>
<div ng-click="view.albumClick(album)">
<div onclick="window.location.href='album/@album.Id'">
<img src="@album.ImageUrl" class="album-image" />
<div style="padding-left: 80px;">
<div class="album-title">@album.Title</div>
Expand Down
12 changes: 6 additions & 6 deletions src/AlbumViewerAspNet5/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@


<link href="~/css/bootstrap.min.css" rel="stylesheet" />
<link href="../css/bootstrap-theme.css" rel="stylesheet" />
<link href="../css/font-awesome.min.css" rel="stylesheet" />
<link href="~/css/bootstrap-theme.css" rel="stylesheet" />
<link href="~/css/font-awesome.min.css" rel="stylesheet" />

<link href="../css/musicstore.css" rel="stylesheet" />
</head>
Expand Down Expand Up @@ -65,9 +65,9 @@

<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="#/albums" ng-class="{ selected: activeTab == 'albums'}">Albums</a></li>
<li><a href="#/artists" ng-class="{ selected: activeTab == 'artists'}">Artists</a></li>
<li><a href="#/about" ng-class="{ selected: activeTab == 'about'}">About</a></li>
<li><a href="albums" ng-class="{ selected: activeTab == 'albums'}">Albums</a></li>
<li><a href="artists" ng-class="{ selected: activeTab == 'artists'}">Artists</a></li>
<li><a href="about" ng-class="{ selected: activeTab == 'about'}">About</a></li>
</ul>
<form id="SearchBoxForm" class="navbar-form navbar-right" ng-show="searchVisible">
<input id="SearchBox" type="text" class="form-control" placeholder="Search..."
Expand All @@ -79,7 +79,7 @@
</div>
</div>

<div class="ng-cloak" style="margin-top: 60px;padding: 10px;">
<div style="margin-top: 60px;padding: 10px;">
<div id="MainView" autoscroll=""
ng-view>
@RenderBody()
Expand Down

0 comments on commit 3f1e569

Please sign in to comment.