原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端

chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车案例,非常精彩,这里这里记录下对此项目的理解。

文章:
http://chsakell.com/2015/01/31/angularjs-feat-web-api/
http://chsakell.com/2015/03/07/angularjs-feat-web-api-enable-session-state/

源码:
https://github.com/chsakell/webapiangularjssecurity

本系列共三篇,本篇是第一篇。

购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端
购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session
购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

■ 配置EF

首先搞清模型之间的关系:

public class Gadget
{
public int GadgetID{get;set;}
... public int CategoryID{get;set;}
public Category Category{get;set;}
} public class Category
{
public int CategoryID{get;set;}
...
public List<Gadget> Gadgets{get;set;}
} public class Order
{
public int OrderID{get;set;}
...
public List<Gadget> Gadgets{get;set;}
} public class GadgetOrder
{
public int GadgetOrderID{get;set;} public int OrderID{get;set;}
public Order Order{get;set;} public int GadgetID{get;set;}
public Gadget Gadget{get;set;}
}

以上,Category和Gadget是1对多的关系,GadgetOrder是Order和Gadget的中间表。

接着需要通过EF Fluent API来配置领域,需要实现EntityTypeConfiguration<TModel>这个基类。比如:

public class OrderConfiguration : EntityTypeConfiguration<Order>
{
public OrderConfiguration()
{
Ignore(o => o.Gadgets);
}
}

然后就配置上下文,继承DbContext这个基类。

public class StoreContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
modelBuilder.Configurations.Add(new OrderConfiguration());
} public DbSet<Order> Orders{get;set;}
...
}

我们还希望在生成数据库的时候生成一些种子数据,需要继承DropCreateDatabaseIfModelChanges<TContext>这个泛型类。

public class StoreInitializer : DropCreateDatabaseIfModelChanges<StoreContext>
{
protected override void Seed(StoreContext context)
{
try
{
GetCategoreis().ForEach(c => context.Categories.Add(c));
...
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
} private static List<Category> GetCategories()
{
...
}
}

如何调用数据库种子数据的类StoreInitializer呢?有一种方法使在项目全局文件中配置。(还有一种方法使在DbContext的构造函数中配置,还有一种在数据库迁移文件中配置,etc.)

void Application_Start(object sender, EventArgs e)
{
Database.SetInitializer(new StoreIntializer());
}

最后,关于EF的配置部分,需要在Web.config中配置,大致如下:

<connectionStrings>
<add name="ProductServiceContext" connectionString="Data Source=.;User=someusername;Password=somepassword;Initial Catalog=MyProductService;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

■ CategoriesController

先俯瞰。

public class CategoriesController : ApiController
{
private StoreContext db = new SotoreContext(); ... private bool CategoryExists(int id)
{
return db.Categories.Count(g => g.CategoryID=id) > ;
} protected override void Dispose(boo disposing)
{
if(disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}

再细节。

/GET api/Categories
public IQueryable<Category> GetCategories()
{
return db.Categories;
} //GET api/Categories/5
[ResponseType(typeof(Category))]
public async Task<IHttpActionResult> GetCategory(int id)
{
Category category = await db.Categories.FindAsync(id);
if(category == null)
{
return NotFound();
}
return Ok(category);
} //put api/Categories/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutCategory(int id, Category category)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
} if(id != category.CategoryID)
{
return BadRequest();
} db.Entry(category).State = EntityState.Modified; try
{
await db.SavheChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!CategoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCOde.NoContet);
} //post api/Categories
[ResponseType(typeof(Category))]
public async Task<IHttpActionResult> PostCategory(Category category)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
} db.Categories.Add(category);
await db.SaveChangesAsync(); //返回到指定的路由
return CreatedAtRoute("DefaultApi", new {id = category.CategoryID}, category);
} //delete api/Categoreis/5
[ResponseType(typeof(Category))]
public async Task<IHttpActionResult> DeleteCategory(int id)
{
Category category = await db.Categories.FindAsync(id);
if(category == null)
{
return NotFound();
} db.Categories.Remove(category);
await db.SaveChangesAsync();
return Ok(category);
}

■ GadgetsController

先俯瞰。

public class GadgetsController : ApiController
{
private StoreContext db = new StoreContext(); protected override void Dispose(bool disposing)
{
if(disposing)
{
db.Dispose();
} base.Dispose(disposing);
} private bool GadgetExists(int id)
{
return db.Gadgets.Count(g => g.GadgetID == ID) > ;
}
}

再细节。

//get api/Gadgets
public IQueryable<Gadget> GetGadgets()
{
return db.Gadgets;
} //get api/Gadgets/5
[ResponseType(typeof(Gadgets))]
public async Task<IHttpActionResult> GetGadget(int id)
{
Gadget gadget = await db.Gadgets.FindAsync(id);
if(gadget == null)
{
return NotFound();
}
return Ok(gadget);
} //put api/Gadgets/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutGadget(int id, Gadget gadget)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
} if(d != gadget.GadgetID)
{
return BadRequest();
} db.Entry(gadget).State = EntityState.Modified; try
{
await db.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!GadgetExists(id))
{
return NotFound();
}
else
{
throw;
}
}
} //post api/Gadgets
[ResposneType(typeof(Gadget))]
public async Task<IHttpActionResult> PostGadget(Gadget gadget)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Gadgets.Add(gadget);
await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new {id=gadget.GadgetID}, gadget)
} //delete api/Gadgets/5
[ResponseType(typeof(Gadget))]
public async Task<IHttpActionResult> DeleteGadget(int id)
{
Gadget gadget = await db.Gadgets.FindAsync(id);
if(gadget == null)
{
return NotFound();
}
db.Gadgets.Remove(gadget);
await db.SaveChangesAsync();
return Ok(gadget);
}

■ OrdersController

firstly overview.

public class OrdersController : ApiController
{
private StoreContext db = new StoreContext(); protected override void Dispose(bool dispoing)
{
if(disposing)
{
db.Dispose();
}
base.Dispose(disposing);
} private bool OrderExists(int id)
{
return db.Orders.Count(g => g.OrderID == id) > ;
}
}

then details.

// get api/Orders
public IQueryable<Order> GetOrders()
{
return db.Orders;
} //get api/Orders/5
[ResponseType(typeof(Order))]
public async Task<IHttpActionResult> GetOrder(int id)
{
Order order = await db.Orders.FindAsync(id);
if(order == null)
{
return NotFound();
}
return Ok(order);
} //put api/Orders/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutOrder(int id, Order order)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
} if(id != order.OrderID)
{
return BadRequest();
} db.Entry(order).State = EntityState.Modified; try
{
await db.SaveChangesAsync();
}
catch(DbUpdateConcurrecyException)
{
if(!OrderExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCOde.NoContet);
} //post api/Orders
[RequestType(typeof(Order))]
public async Task<IHttpActionResult> PostOrder(Order order)
{
if(!ModelState.IsValid)
{
reuturn BadRequest(ModelState);
}
try
{
db.Orders.Add(order);
foreach(Gadget gadget in order.Gadgets)
{
db.GadgetOrders.Add(new GadgetOrder{
OrderID = order.OrderID,
GadgetID = gadget.GadgetID
})
} await db.SaveChangesAsync();
}
catch(Exception ex)
{
return BadRequest(ex.Message);
}
return CreatedAtRoutne("Default", new {controller = "Home", action="viewOrder", id = order.orderID}, order);
} //delete api/Orders/5
[ResponseType(typeof(Order))]
public async Task<IHttpActionResult> DeleteOrder(int id)
{
Order order = await db.Orders.FindAsync(id);
if(order == null)
{
return NotFound();
} db.Orders.Remoe(order);
await db.SaveChangesAsync();
return Ok(order);
}

待续~~

购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端的更多相关文章

  1. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...

  2. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session chsakell分享了前端使用AngularJS,后端使用ASP.NE ...

  3. 对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解

    依然chsakell,他写了一篇前端AngularJS,后端OData,ASP.NET Web API的Demo,关于OData在ASP.NET Web API中的正删改查没有什么特别之处,但在前端调 ...

  4. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(4)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  5. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(3)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  6. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(2)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  7. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(1)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  8. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查

    AngularJS中的$resource服务相比$http服务更适合与RESTful服务进行交互.本篇后端使用ASP.NET Web API, 前端使用$resource,实现增删改查. 本系列包括: ...

  9. ASP.NET Web API 2 external logins with Facebook and Google in AngularJS app

    转载:http://bitoftech.net/2014/08/11/asp-net-web-api-2-external-logins-social-logins-facebook-google-a ...

随机推荐

  1. VS2010 Command Prompt Error:Cannot determine the location of the VS Common Tools folder

    就在VS2010 Command Prompt 用vcvarsall.bat x64重新设置环境变量的时候,出现了标题中的错误.原因就在参考链接中 References: http://stackov ...

  2. SSH自动部署(转)

    我的是windows环境,目前开发的过程中,有些项目需要一下子部署到很多的linux服务器上.写了个脚本能够自动上传文件和执行部署任务.完成这个任务需要的条件包括SSH配置和一个执行脚本. 准备 1. ...

  3. 【转】android 5.0 64bit系统加载库文件失败问题浅析

    原文网址:http://blog.csdn.net/andrewblog/article/details/43601303 最近公司的一个项目使用android 5.0 64 bit平台,相对以前版本 ...

  4. 浅谈JVM内存区域划分

    好吧,虽说真的有看过<深入分析Java Web技术内幕>一书,但当时看的时候还是一知半解,稀里糊涂的看完了.本来是打算暑假拿起来再看一遍的,但是早两天一个阿里学长给我做了个小面试,让我颇受 ...

  5. java实现矩阵连乘的动态规划

    package com.cjs.algorithm; public class DynamicPlan { /** * 此方法用来求解矩阵连乘的最小数乘次数 * * @param p * 传入的要连乘 ...

  6. hdu 5167 Fibonacci(预处理)

    Problem Description Following is the recursive definition of Fibonacci sequence: Fi=⎧⎩⎨01Fi−1+Fi−2i ...

  7. 前端开发利器—FIDDLER 转

    http://www.cnblogs.com/yuzhongwusan/archive/2012/07/20/2601306.html 前端开发利器—FIDDLER 1.Fiddler相对其他调试工具 ...

  8. Wikioi 1080一维树状数组

    半个月时间最终把那些杂七杂八的学完了,尽管学完也,也仅仅是有了个模板,自己手敲还是不太行.所以如今開始要疯狂刷题了! ! .!!! 这题裸的树状数组.曾经写那道<敌兵布阵>的时候写过,所以 ...

  9. js_day13

  10. 【转载】CocoaPods安装和使用教程

    转自:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaP ...