您的位置:首页 > 运维架构 > 网站架构

分享基于EF+WCF的通用三层架构及解析

2012-09-03 08:26 519 查看

本项目结合EF 4.3及WCF实现了经典三层架构,各层面向接口,WCF实现SOA,Repository封装调用,在此基础上实现了WCFContext,动态服务调用及一个分页的实例。

1. 项目架构图:

View Code

1 [Table("Product")]

2 public partial class Product

3 {

4 public int Id { get; set; }

5

6 [StringLength(50)]

7 [Required(ErrorMessage = "名称不能为空")]

8 public string Name { get; set; }

9

public int Size { get; set; }

[StringLength(300)]

public string PhotoUrl { get; set; }

public DateTime AddTime { get; set; }

public int CategoryId { get; set; }

public virtual Category Category { get; set; }

}




5. 提供了MVC调用服务端分页的实例:

MVC调用Wcf客户代理请求分页数据集合

public ActionResult Index(int pageIndex = 1)
{
var products = this.Service.GetProducts(PageSize, pageIndex);
return View(products);
}

MVC附加用户Context信息到服务端

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
WCFContext.Current.Operater = new Operater(){Name = "guozili",Time = DateTime.Now,IP = Fetch.UserIp,};
}

BLL取出Context信息并调用数据层

public PagedList<Product> GetProducts(int pageSize, int pageIndex, int categoryId = 0)
{
//Test WCFContext
var context = WCFContext.Current.Operater;
return this.dao.FindAllByPage<Product, int>(p => categoryId == 0 ? true : p.CategoryId == categoryId, p => p.Id, pageSize, pageIndex);
}

DAL调用通用的Repository接口

public PagedList<T> FindAllByPage<T, S>(Expression<Func<T, bool>> conditions, Expression<Func<T, S>> orderBy, int pageSize, int pageIndex) where T : class
{
var queryList = conditions == null ? context.Set<T>() : context.Set<T>().Where(conditions) as IQueryable<T>;

return queryList.OrderByDescending(orderBy).ToPagedList(pageIndex, pageSize);
}




6. 最后提供源码下载 http://files.cnblogs.com/guozili/EasyEF.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: