tegoryInfo
{
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
Code
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)
{
throw new Exception("发布公告失败了!原因?没有原 因!我是业务组件,我说失败就失败!");
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下的。
ServiceBuilder.cs:
Code
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>分类服务
|