前言
ASP.NET MVC作为微软官方的MVC解决方案,推出有一段时间了。可以说自动推出以来,一直广受关注。在经历了漫长的Preview之后,前几天终于推出了其 beta版。并且在官方文档中,微软声明最终的正式版与beta版相比不会有大的变化。所以,对于.NET平台的开发人员来说,是时候学习ASP.NET MVC了。
本系列文章作为一个ASP.NET MVC的入门教程,将不会长篇大论介绍其中的概念及理论。而是通过案例实践来学习ASP.NET MVC。在这系列文章中我将逐步完成一个“公告发布系统”。我的写作策略是:先动手做,遇到需要解释概念和理论的时候再解释,而不是先把概念和理论解释完再做东西。
另外,我还有几点要说明的:
1.为了将大家的关注点充分集中在ASP.NET MVC上,这个Demo的业务处理将使用Mock的方式。即不会真正去访问数据库,而是虚拟一些数据。
2.本Demo将不考虑任何美工问题。
下面,让我们一起开始ASP.NET MVC400电话 电子公告之旅吧。在这一篇中,我们将做完所有的准备工作。
配置环境
如果您还没有安装ASP.NET MVC的话,请到这里下载安装 。
下载后,按提示安装就可以了。
新建项目
安装完ASP.NET MVC后,在VS里新建一个项目,可以看到有一个“ASP.NET MVC Web Application”的选项,选择它,新建一个项目,并命名为“MVCDemo”。
建完项目后,可以看到默认情况下已经建立了很多文件夹,这里简略说一下各个文件夹的作用。
Content——存放应用需要的一些资源文件,如图片、CSS等。
Controllers——存放控制器类。
Models——存放业务模型组件。
Scripts——存放JavaScript脚本文件。
Views——存放视图。
现在不了解一些概念没关系,后续文章将慢慢解释。
准备工作
新建完项目后,我们要做的准备工作就是建立Mock业务模型,这样以后我们就直接使用这些“欺骗”式的业务模型进行业务处理,而将全部关注点放在ASP.NET MVC的学习上。
首先在Models下新建三个文件夹,分别叫做Entities、Interfaces、MockModels,分别用来存放实体类、接口及Mock业务模型。
Entities下有两个类:CategoryInfo和AnnounceInfo,分别是公告类别和公告的实体类。具体代码如下:
CategoryInfo.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MVCDemo.Models.Entities { /// <summary> /// 分类实体类 /// </summary> public class CategoryInfo { public int ID { get; set; } public string Name { get; set; } } } |
AnnounceInfo.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MVCDemo.Models.Entities { /// <summary> /// 公告实体类 /// </summary> public class AnnounceInfo { public int ID { get; set; } public string Title { get; set; } public string Content { get; set; } } } |
接下来,我们将定义两个接口,分别是公告类别服务和公告服务必须实现的接口。这两个接口放在Interfaces下。
ICategoryService.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MVCDemo.Models.Entities;
- namespace MVCDemo.Models.Interfaces
- {
- /// <summary>
- /// 分类服务组件接口
- /// </summary>
- public interface ICategoryService
- {
- /// <summary>
- /// 添加分类
- /// </summary>
- /// <param name="category"></param>
- void Add(CategoryInfo category);
- /// <summary>
- /// 修改分类名称
- /// </summary>
- /// <param name="id"></param>
- /// <param name="name"></param>
- void ChangeName(int id,string name);
- /// <summary>
- /// 删除分类
- /// </summary>
- /// <param name="id"></param>
- void Remove(int id);
- /// <summary>
- /// 取得某个分类详细信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- CategoryInfo GetDetail(int id);
- /// <summary>
- /// 取得所有分类
- /// </summary>
- /// <returns></returns>
- List<CategoryInfo> GetAll();
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MVCDemo.Models.Entities; namespace MVCDemo.Models.Interfaces { /// <summary> /// 分类服务组件接口 /// </summary> public interface ICategoryService { /// <summary> /// 添加分类 /// </summary> /// <param name="category"></param> void Add(CategoryInfo category); /// <summary> /// 修改分类名称 /// </summary> /// <param name="id"></param> /// <param name="name"></param> void ChangeName(int id,string name); /// <summary> /// 删除分类 /// </summary> /// <param name="id"></param> void Remove(int id); /// <summary> /// 取得某个分类详细信息 /// </summary> /// <param name="id"></param> /// <returns></returns> CategoryInfo GetDetail(int id); /// <summary> /// 取得所有分类 /// </summary> /// <returns></returns> List<CategoryInfo> GetAll(); } }
IAnnounceService.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MVCDemo.Models.Entities;
- namespace MVCDemo.Models.Interfaces
- {
- /// <summary>
- /// 公告服务组件接口
- /// </summary>
- public interface IAnnounceService
- {
- /// <summary>
- /// 发布公告
- /// </summary>
- /// <param name="announce"></param>
- void Release(AnnounceInfo announce);
- /// <summary>
- /// 修改公告信息
- /// </summary>
- /// <param name="announce"></param>
- void Notify(AnnounceInfo announce);
- /// <summary>
- /// 删除公告 (http://www.my400800.cn)
- /// </summary>
- /// <param name="id"></param>
- void Remove(int id);
- /// <summary>
- /// 取得公告详细内容
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- AnnounceInfo GetDetail(int id);
- /// <summary>
- /// 取得某个分类下的所有公告
- /// </summary>
- /// <param name="categoryId"></param>
- /// <returns></returns>
- List<AnnounceInfo> GetByCategory(CategoryInfo category);
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MVCDemo.Models.Entities; namespace MVCDemo.Models.Interfaces { /// <summary> /// 公告服务组件接口 /// </summary> public interface IAnnounceService { /// <summary> /// 发布公告 /// </summary> /// <param name="announce"></param> void Release(AnnounceInfo announce); /// <summary> /// 修改公告信息 /// </summary> /// <param name="announce"></param> void Notify(AnnounceInfo announce); /// <summary> /// 删除公告 (http://www.my400800.cn) /// </summary> /// <param name="id"></param> void Remove(int id); /// <summary> /// 取得公告详细内容 /// </summary> /// <param name="id"></param> /// <returns></returns> AnnounceInfo GetDetail(int id); /// <summary> /// 取得某个分类下的所有公告 /// </summary> /// <param name="categoryId"></param> /// <returns></returns> List<AnnounceInfo> GetByCategory(CategoryInfo category); } }
然后,我们在MockModels下建立两个Mock业务逻辑服务模型。注意它们要各自实现自己的接口。
MockCategoryService.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MVCDemo.Models.Interfaces;
- using MVCDemo.Models.Entities;
- namespace MVCDemo.Models.MockModels
- {
- /// <summary>
- /// “欺骗”服务组件,用于模拟分类的业务服务
- /// </summary>
- public class MockCategoryService : ICategoryService
- {
- /// <summary>
- /// 添加分类
- /// </summary>
- /// <param name="category"></param>
- public void Add(CategoryInfo category)
- {
- return;
- }
- /// <summary>
- /// 修改分类名称
- /// </summary>
- /// <param name="id"></param>
- /// <param name="name"></param>
- public void ChangeName(int id, string name)
- {
- return;
- }
- /// <summary>
- /// 删除分类
- /// </summary>
- /// <param name="id"></param>
- public void Remove(int id)
- {
- return;
- }
- /// <summary>
- /// 取得某个分类详细信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public CategoryInfo GetDetail(int id)
- {
- return new CategoryInfo
- {
- ID = id,
- Name = "最新通告",
- };
- }
- /// <summary>
- /// 取得所有分类
- /// </summary>
- /// <returns></returns>
- public List<CategoryInfo> GetAll()
- {
- List<CategoryInfo> categories = new List<CategoryInfo>();
- for (int i = 1; i <= 5; i++)
- {
- CategoryInfo category = new CategoryInfo
- {
- ID = i,
- Name="通告类别"+i,
- };
- categories.Add(category);
- }
- return categories;
- }
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MVCDemo.Models.Interfaces; using MVCDemo.Models.Entities; namespace MVCDemo.Models.MockModels { /// <summary> /// “欺骗”服务组件,用于模拟分类的业务服务 /// </summary> public class MockCategoryService : ICategoryService { /// <summary> /// 添加分类 /// </summary> /// <param name="category"></param> public void Add(CategoryInfo category) { return; } /// <summary> /// 修改分类名称 /// </summary> /// <param name="id"></param> /// <param name="name"></param> public void ChangeName(int id, string name) { return; } /// <summary> /// 删除分类 /// </summary> /// <param name="id"></param> public void Remove(int id) { return; } /// <summary> /// 取得某个分类详细信息 /// </summary> /// <param name="id"></param> /// <returns></returns> public CategoryInfo GetDetail(int id) { return new CategoryInfo { ID = id, Name = "最新通告", }; } /// <summary> /// 取得所有分类 /// </summary> /// <returns></returns> public List<CategoryInfo> GetAll() { List<CategoryInfo> categories = new List<CategoryInfo>(); for (int i = 1; i <= 5; i++) { CategoryInfo category = new CategoryInfo { ID = i, Name="通告类别"+i, }; categories.Add(category); } return categories; } } }
MockAnnounceService.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MVCDemo.Models.Interfaces;
- using MVCDemo.Models.Entities;
- namespace MVCDemo.Models.MockModels
- {
- /// <summary>
- /// “欺骗”服务组件,用于模拟公告的业务服务
- /// </summary>
- public class MockAnnounceService : IAnnounceService
- {
- /// <summary>
- /// 发布公告
- /// </summary>
- /// <param name="announce"></param>
- public void Release(AnnounceInfo announce)
- {
- return;
- }
- /// <summary>
- /// 修改公告信息
- /// </summary>
- /// <param name="announce"></param>
- public void Notify(AnnounceInfo announce)
- {
- return;
- }
- /// <summary>
- /// 删除公告
- /// </summary>
- /// <param name="id"></param>
- public void Remove(int id)
- {
- return;
- }
- /// <summary>
- /// 取得公告详细内容
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public AnnounceInfo GetDetail(int id)
- {
- return new AnnounceInfo
- {
- ID = id,
- Title = "第" + id + "则公告",
- Content = "全体同学明早九点集体做俯卧撑!",
- };
- }
- /// <summary>
- /// 取得某个分类下的所有公告
- /// </summary>
- /// <param name="categoryId"></param>
- /// <returns></returns>
- public List<AnnounceInfo> GetByCategory(CategoryInfo category)
- {
- List<AnnounceInfo> announces = new List<AnnounceInfo>();
- for (int i = 1; i <= 10; i++)
- {
- AnnounceInfo announce = new AnnounceInfo
- {
- ID = i,
- Title = category.Name+ "的第" + i + "则公告",
- Content = "全体同学明早九点集体做俯卧撑!",
- };
- announces.Add(announce);
- }
- return announces;
- }
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MVCDemo.Models.Interfaces; using MVCDemo.Models.Entities; namespace MVCDemo.Models.MockModels { /// <summary> /// “欺骗”服务组件,用于模拟公告的业务服务 /// </summary> public class MockAnnounceService : IAnnounceService { /// <summary> /// 发布公告 /// </summary> /// <param name="announce"></param> public void Release(AnnounceInfo announce) { return; } /// <summary> /// 修改公告信息 /// </summary> /// <param name="announce"></param> public void Notify(AnnounceInfo announce) { return; } /// <summary> /// 删除公告 /// </summary> /// <param name="id"></param> public void Remove(int id) { return; } /// <summary> /// 取得公告详细内容 /// </summary> /// <param name="id"></param> /// <returns></returns> public AnnounceInfo GetDetail(int id) { return new AnnounceInfo { ID = id, Title = "第" + id + "则公告", Content = "全体同学明早九点集体做俯卧撑!", }; } /// <summary> /// 取得某个分类下的所有公告 /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public List<AnnounceInfo> GetByCategory(CategoryInfo category) { List<AnnounceInfo> announces = new List<AnnounceInfo>(); for (int i = 1; i <= 10; i++) { AnnounceInfo announce = new AnnounceInfo { ID = i, Title = category.Name+ "的第" + i + "则公告", Content = "全体同学明早九点集体做俯卧撑!", }; announces.Add(announce); } return announces; } } }
可以看到,这两个类并没有访问数据库,也没有实现真正的业务逻辑,而不过是“捏造”了一些数据骗骗我们的表示层而已。
最后,我们要建立一个生成业务逻辑模型的生成器,用来实现表示层和业务逻辑层的解耦。当然,为了简化复杂度,这里没有使用依赖注入机制。下面是我们生成器的代码,这个类是直接放在Models下的。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MVCDemo.Models.Interfaces;
- using MVCDemo.Models.MockModels;
- namespace MVCDemo.Models
- {
- /// <summary>
- /// 服务组件生成类,用于生成业务服务组件
- /// </summary>
- public sealed class ServiceBuilder
- {
- /// <summary>
- /// 创建分类服务组件
- /// </summary>
- /// <returns>分类服务组件</returns>
- public static ICategoryService BuildCategoryService()
- {
- return new MockCategoryService();
- }
- /// <summary>
- /// 创建公告服务组件
- /// </summary>
- /// <returns>公告服务组件</returns>
- public static IAnnounceService BuildAnnounceService()
- {
- return new MockAnnounceService();
- }
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MVCDemo.Models.Interfaces; using MVCDemo.Models.MockModels; namespace MVCDemo.Models { /// <summary> /// 服务组件生成类,用于生成业务服务组件 /// </summary> public sealed class ServiceBuilder { /// <summary> /// 创建分类服务组件 /// </summary> /// <returns>分类服务组件</returns> public static ICategoryService BuildCategoryService() { return new MockCategoryService(); } /// <summary> /// 创建公告服务组件 /// </summary> /// <returns>公告服务组件</returns> public static IAnnounceService BuildAnnounceService() { return new MockAnnounceService(); } } }