Changed CartItem.CartId type from string to Guid. Needed this change to ease the creation of an index on the CartId column in the database. String is not an indexable type, guid (uniqueidentifier in SQL Server) is. Adding that index has a big impact on database performance for the MusicStore app for stress and reliability testing

This commit is contained in:
martinpf 2016-06-22 13:38:26 -07:00
Родитель a5ab80d403
Коммит 6c02e5ed84
2 изменённых файлов: 14 добавлений и 9 удалений

Просмотреть файл

@ -9,7 +9,7 @@ namespace MusicStore.Models
public int CartItemId { get; set; } public int CartItemId { get; set; }
[Required] [Required]
public string CartId { get; set; } public Guid CartId { get; set; }
public int AlbumId { get; set; } public int AlbumId { get; set; }
public int Count { get; set; } public int Count { get; set; }

Просмотреть файл

@ -10,9 +10,9 @@ namespace MusicStore.Models
public class ShoppingCart public class ShoppingCart
{ {
private readonly MusicStoreContext _dbContext; private readonly MusicStoreContext _dbContext;
private readonly string _shoppingCartId; private readonly Guid _shoppingCartId;
private ShoppingCart(MusicStoreContext dbContext, string id) private ShoppingCart(MusicStoreContext dbContext, Guid id)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_shoppingCartId = id; _shoppingCartId = id;
@ -21,7 +21,7 @@ namespace MusicStore.Models
public static ShoppingCart GetCart(MusicStoreContext db, HttpContext context) public static ShoppingCart GetCart(MusicStoreContext db, HttpContext context)
=> GetCart(db, GetCartId(context)); => GetCart(db, GetCartId(context));
public static ShoppingCart GetCart(MusicStoreContext db, string cartId) public static ShoppingCart GetCart(MusicStoreContext db, Guid cartId)
=> new ShoppingCart(db, cartId); => new ShoppingCart(db, cartId);
public async Task AddToCart(Album album) public async Task AddToCart(Album album)
@ -166,17 +166,22 @@ namespace MusicStore.Models
} }
// We're using HttpContextBase to allow access to sessions. // We're using HttpContextBase to allow access to sessions.
private static string GetCartId(HttpContext context) private static Guid GetCartId(HttpContext context)
{ {
var cartId = context.Session.GetString("Session"); Guid cartId;
string cartIdString = context.Session.GetString("Session");
if (cartId == null) if (cartIdString == null)
{ {
//A GUID to hold the cartId. //A GUID to hold the cartId.
cartId = Guid.NewGuid().ToString(); cartId = Guid.NewGuid();
// Send cart Id as a cookie to the client. // Send cart Id as a cookie to the client.
context.Session.SetString("Session", cartId); context.Session.SetString("Session", cartId.ToString());
}
else
{
cartId = Guid.Parse(cartIdString);
} }
return cartId; return cartId;