《Pro ASP.NET MVC 3 Framework》英文原版教材个人勘误

2011年6月27日,Apress今年比较吸引人的《Pro ASP.NET MVC 3 Framework》一书终于发布了。

http://www.apress.com/9781430234043

此书详细的为大家讲解了ASP.NET MVC 3的各个方面,并同时介绍并贯穿着Entity Framework 4.1、领域模型(Domain Model)设计、测试驱动开发(TDD)、基于Ninject的依赖注入、Moq模拟仓库等技术的运用,是一本不可多得的有关ASP.NET MVC 3的好书。

不得不说中文版的ASP.NET MVC系列教材太少了,MVC3的更是没有,所以读英文原版教材是最佳方案,这可以让你始终同技术发展保持同步,至少领先国内绝大多数人一步,而且还不会被中文翻译所误导。

不过在读此书的过程中,发现作者还是有许许多多错误的,一些很明显的错误我就不记录了,在此我将在读书的过程中记录一些初次接触新技术的人不太容易纠正的错误,分享给大家。

267页:Listing 9-13. Implementing the SaveProduct Method

错误
 1 using System.Linq;
2 using SportsStore.Domain.Abstract;
3 using SportsStore.Domain.Entities;
4
5 namespace SportsStore.Domain.Concrete
6 {
7 public class EFProductRepository : IProductRepository
8 {
9 private EFDbContext context = new EFDbContext();
10
11 public IQueryable<Product> Products
12 {
13 get { return context.Products; }
14 }
15
16 public void SaveProduct(Product product)
17 {
18 if (product.ProductID == 0)
19 {
20 context.Products.Add(product);
21 }
22
23 context.SaveChanges();
24 }
25 }
26 }

这里的问题是产品信息编辑调用的数据仓库保存方法无法正确保存修改后的数据,由于本人对EF4.1不熟悉所以在这里花费了半天时间找到了问题所在,就是少写了将传入参数保存进上下文的代码,需要注意的是必须先要引用System.Data.Entity程序集到SportsStore.Domain项目中。

正确
 1 using System.Linq;
2 using SportsStore.Domain.Abstract;
3 using SportsStore.Domain.Entities;
4
5 namespace SportsStore.Domain.Concrete
6 {
7 public class EFProductRepository : IProductRepository
8 {
9 private EFDbContext context = new EFDbContext();
10
11 public IQueryable<Product> Products
12 {
13 get { return context.Products; }
14 }
15
16 public void SaveProduct(Product product)
17 {
18 if (product.ProductID == 0)
19 {
20 context.Products.Add(product);
21 }
22 else
23 {
24 context.Entry(product).State = System.Data.EntityState.Modified;
25 }
26
27 context.SaveChanges();
28 }
29 }
30 }

  

posted @ 2011-07-20 11:20  胡皓  阅读(1175)  评论(6编辑  收藏  举报