An injection of musical love... (Updating music store to use new EF configuration and DI)
Changes EF setup to use common DI container Sets up injection of context instances
This commit is contained in:
parent
d138028462
commit
737afbd610
|
|
@ -9,7 +9,12 @@ namespace MusicStore.Components
|
|||
[ViewComponent(Name = "CartSummary")]
|
||||
public class CartSummaryComponent : ViewComponent
|
||||
{
|
||||
private MusicStoreContext db = new MusicStoreContext();
|
||||
private readonly MusicStoreContext db;
|
||||
|
||||
public CartSummaryComponent(MusicStoreContext context)
|
||||
{
|
||||
db = context;
|
||||
}
|
||||
|
||||
public async Task<IViewComponentResult> InvokeAsync()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,7 +9,13 @@ namespace MusicStore.Components
|
|||
[ViewComponent(Name = "GenreMenu")]
|
||||
public class GenreMenuComponent : ViewComponent
|
||||
{
|
||||
private MusicStoreContext db = new MusicStoreContext();
|
||||
private readonly MusicStoreContext db;
|
||||
|
||||
public GenreMenuComponent(MusicStoreContext context)
|
||||
{
|
||||
db = context;
|
||||
}
|
||||
|
||||
public async Task<IViewComponentResult> InvokeAsync()
|
||||
{
|
||||
// TODO [EF] We don't query related data as yet, so the OrderByDescending isn't doing anything
|
||||
|
|
|
|||
|
|
@ -11,7 +11,13 @@ namespace MusicStore.Controllers
|
|||
//[Authorize]
|
||||
public class CheckoutController : Controller
|
||||
{
|
||||
MusicStoreContext db = new MusicStoreContext();
|
||||
private readonly MusicStoreContext db;
|
||||
|
||||
public CheckoutController(MusicStoreContext context)
|
||||
{
|
||||
db = context;
|
||||
}
|
||||
|
||||
const string PromoCode = "FREE";
|
||||
|
||||
//
|
||||
|
|
|
|||
|
|
@ -7,7 +7,12 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private MusicStoreContext db = new MusicStoreContext();
|
||||
private readonly MusicStoreContext db;
|
||||
|
||||
public HomeController(MusicStoreContext context)
|
||||
{
|
||||
db = context;
|
||||
}
|
||||
|
||||
//
|
||||
// GET: /Home/
|
||||
|
|
|
|||
|
|
@ -7,7 +7,12 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
public class ShoppingCartController : Controller
|
||||
{
|
||||
private MusicStoreContext db = new MusicStoreContext();
|
||||
private readonly MusicStoreContext db;
|
||||
|
||||
public ShoppingCartController(MusicStoreContext context)
|
||||
{
|
||||
db = context;
|
||||
}
|
||||
|
||||
//
|
||||
// GET: /ShoppingCart/
|
||||
|
|
|
|||
|
|
@ -6,7 +6,12 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
public class StoreController : Controller
|
||||
{
|
||||
MusicStoreContext db = new MusicStoreContext();
|
||||
private readonly MusicStoreContext db;
|
||||
|
||||
public StoreController(MusicStoreContext context)
|
||||
{
|
||||
db = context;
|
||||
}
|
||||
|
||||
//
|
||||
// GET: /Store/
|
||||
|
|
|
|||
|
|
@ -11,7 +11,12 @@ namespace MusicStore.Controllers
|
|||
//[Authorize(Roles="Administrator")]
|
||||
public class StoreManagerController : Controller
|
||||
{
|
||||
private MusicStoreContext db = new MusicStoreContext();
|
||||
private readonly MusicStoreContext db;
|
||||
|
||||
public StoreManagerController(MusicStoreContext context)
|
||||
{
|
||||
db = context;
|
||||
}
|
||||
|
||||
//
|
||||
// GET: /StoreManager/
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.Data.Entity;
|
||||
using System;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Data.Entity.Metadata;
|
||||
using Microsoft.Data.InMemory;
|
||||
using Microsoft.Data.SqlServer;
|
||||
|
|
@ -7,6 +8,11 @@ namespace MusicStore.Models
|
|||
{
|
||||
public class MusicStoreContext : EntityContext
|
||||
{
|
||||
public MusicStoreContext(IServiceProvider serviceProvider)
|
||||
: base(serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public EntitySet<Album> Albums { get; set; }
|
||||
public EntitySet<Artist> Artists { get; set; }
|
||||
public EntitySet<Order> Orders { get; set; }
|
||||
|
|
@ -17,9 +23,9 @@ namespace MusicStore.Models
|
|||
protected override void OnConfiguring(EntityConfigurationBuilder builder)
|
||||
{
|
||||
#if NET45
|
||||
builder.UseSqlServer(@"Server=(localdb)\v11.0;Database=MusicStore;Trusted_Connection=True;");
|
||||
builder.SqlServerConnectionString(@"Server=(localdb)\v11.0;Database=MusicStore;Trusted_Connection=True;");
|
||||
#else
|
||||
builder.UseDataStore(new InMemoryDataStore());
|
||||
builder.UseInMemoryStore(persist: true);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ namespace MusicStore.Web.Models
|
|||
{
|
||||
const string imgUrl = "~/Images/placeholder.png";
|
||||
|
||||
public static async Task InitializeMusicStoreDatabaseAsync()
|
||||
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider)
|
||||
{
|
||||
using (var db = new MusicStoreContext())
|
||||
using (var db = new MusicStoreContext(serviceProvider))
|
||||
{
|
||||
// TODO [EF] Swap to use top level API when available
|
||||
var sqlServerDataStore = db.Configuration.DataStore as SqlServerDataStore;
|
||||
|
|
@ -26,39 +26,41 @@ namespace MusicStore.Web.Models
|
|||
if (!await creator.ExistsAsync())
|
||||
{
|
||||
await creator.CreateAsync(db.Model);
|
||||
await InsertTestData();
|
||||
await InsertTestData(serviceProvider);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await InsertTestData();
|
||||
await InsertTestData(serviceProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task InsertTestData()
|
||||
private static async Task InsertTestData(IServiceProvider serviceProvider)
|
||||
{
|
||||
var genres = GetGenres();
|
||||
var artists = GetArtists();
|
||||
var albums = GetAlbums(imgUrl, genres, artists);
|
||||
|
||||
await AddOrUpdateAsync(g => g.GenreId, genres);
|
||||
await AddOrUpdateAsync(a => a.ArtistId, artists);
|
||||
await AddOrUpdateAsync(a => a.AlbumId, albums);
|
||||
await AddOrUpdateAsync(serviceProvider, g => g.GenreId, genres);
|
||||
await AddOrUpdateAsync(serviceProvider, a => a.ArtistId, artists);
|
||||
await AddOrUpdateAsync(serviceProvider, a => a.AlbumId, albums);
|
||||
}
|
||||
|
||||
// TODO [EF] This may be replaced by a first class mechanism in EF
|
||||
private static async Task AddOrUpdateAsync<TEntity>(Func<TEntity, object> propertyToMatch, IEnumerable<TEntity> entities)
|
||||
private static async Task AddOrUpdateAsync<TEntity>(
|
||||
IServiceProvider serviceProvider,
|
||||
Func<TEntity, object> propertyToMatch, IEnumerable<TEntity> entities)
|
||||
where TEntity : class
|
||||
{
|
||||
// Query in a separate context so that we can attach existing entities as modified
|
||||
List<TEntity> existingData;
|
||||
using (var db = new MusicStoreContext())
|
||||
using (var db = new MusicStoreContext(serviceProvider))
|
||||
{
|
||||
existingData = db.Set<TEntity>().ToList();
|
||||
}
|
||||
|
||||
using (var db = new MusicStoreContext())
|
||||
using (var db = new MusicStoreContext(serviceProvider))
|
||||
{
|
||||
foreach (var item in entities)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@ using Microsoft.AspNet.Mvc;
|
|||
using Microsoft.AspNet.RequestContainer;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Security.Cookies;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Data.InMemory;
|
||||
using Microsoft.Data.SqlServer;
|
||||
using Microsoft.Net.Runtime;
|
||||
using MusicStore.Logging;
|
||||
using MusicStore.Models;
|
||||
|
|
@ -29,6 +32,12 @@ public class Startup
|
|||
{
|
||||
services.AddInstance<ILoggerFactory>(new NullLoggerFactory());
|
||||
services.AddMvc();
|
||||
#if NET45
|
||||
services.AddEntityFramework(s => s.AddSqlServer());
|
||||
#else
|
||||
services.AddEntityFramework(s => s.AddInMemoryStore());
|
||||
#endif
|
||||
services.AddTransient<MusicStoreContext, MusicStoreContext>();
|
||||
services.AddInstance<UserManager<ApplicationUser>>(new UserManager<ApplicationUser>(new InMemoryUserStore<ApplicationUser>()));
|
||||
services.AddInstance<RoleManager<IdentityRole>>(new RoleManager<IdentityRole>(new InMemoryRoleStore<IdentityRole>()));
|
||||
});
|
||||
|
|
@ -55,7 +64,7 @@ public class Startup
|
|||
new { controller = "Home" });
|
||||
});
|
||||
|
||||
SampleData.InitializeMusicStoreDatabaseAsync().Wait();
|
||||
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
|
||||
CreateAdminUser(app.ApplicationServices);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@
|
|||
"Microsoft.AspNet.Identity.Security": "0.1-alpha-*",
|
||||
"Microsoft.Data.Entity": "0.1-alpha-*",
|
||||
"Microsoft.Data.Relational": "0.1-alpha-*",
|
||||
"Microsoft.Data.SqlServer": "0.1-pre-*",
|
||||
"Microsoft.Data.SqlServer": "0.1-alpha-*",
|
||||
"Microsoft.Data.InMemory": "0.1-alpha-*",
|
||||
"Microsoft.Data.Migrations": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Diagnostics": "0.1-alpha-*",
|
||||
|
|
|
|||
Loading…
Reference in New Issue