惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
雷峰网
雷峰网
博客园_首页
小众软件
小众软件
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
U
Unit 42
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 叶小钗

博客园 - guozili

分享12306抢票心得-终极秒杀思路篇 分享12306抢票心得-最终篇 分享12306全自动验证码识别提交,春运抢票准备时 分享“12306 P2P平台”创业Idea 分享12306秒票杀手锏源码 分享12306秒票心得及杀手锏 分享基于EF+MVC+Bootstrap的通用后台管理系统及架构 基于Silverlight的快速开发框架RapidSL新特性解析 分享基于silverlight的仿“百度文库”的文档控件 分享基于silverlight的一个“树形结构图”控件 基于 Silverlight的快速开发框架RapidSL新版改进源码 基于 Silverlight的快速开发框架RapidSL之MVVM解析 基于Silverlight的快速开发框架RapidSL之开源 分享基于silverlight的一个大文件上传控件 抢火车票利器:分享一个抓取火车票转让信息的小程序 基于 Silverlight的精简框架之版本升级及使用 基于 Silverlight的精简框架之版本和控件更新 Silverlight project demos for MSRA in 2009,to look forward to silverlight's furture 异步编程练习
分享基于EF+WCF的通用三层架构及解析
guozili · 2012-09-03 · via 博客园 - guozili

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

1. 项目架构图:

 


2. 项目解决方案:

 

  • 在传统的三层架构上增加了WcfService(服务端),WcfClientProxy(客户端服务调用),及WcfExtension(一些扩展)

3. Wcf Service的实现:

 

  • 工厂实现了RemoteServiceFactory(用于远程调用)和RefServiceFactory(本地引用调用服务层)生成客户端代理,都需要实现IServiceFactory的“IService CreateService();”
  • RemoteServiceFactory通过ChannelFactory动态产生客户端代理类IService,并将此对象进行缓存
  • WCFExtension实现了WCFContext,可传输用户登陆或IP上下文信息,以及拦截方法写Log的机制,具体可以参考 http://www.cnblogs.com/lovecindywang/archive/2012/03/01/2376144.html

3. 数据层Repository的实现:

 
  •  通过用来访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调,将领域模型从客户代码和数据映射层之间解耦出来,具体实现代码:

View Code

 1 public class DaoBase : IRepository, IDisposable
 2     {
 3         public DbContext context;
 4 
 5         public DaoBase()
 6         {
 7             this.context = new EasyEF.DAL.DbContext();
 8         }
 9 
10         public T Update<T>(T entity) where T : class
11         {
12             var set = context.Set<T>();
13             set.Attach(entity);
14             context.Entry<T>(entity).State = EntityState.Modified;
15             context.SaveChanges();
16 
17             return entity;
18         }
19 
20         public T Insert<T>(T entity) where T : class
21         {
22             context.Set<T>().Add(entity);
23             context.SaveChanges();
24             return entity;
25         }
26 
27         public void Delete<T>(T entity) where T : class
28         {
29             context.Entry<T>(entity).State = EntityState.Deleted;
30             context.SaveChanges();
31         }
32 
33         public T Find<T>(params object[] keyValues) where T : class
34         {
35             return context.Set<T>().Find(keyValues);
36         }
37 
38         public List<T> FindAll<T>(Expression<Func<T, bool>> conditions = nullwhere T : class
39         {
40             if (conditions == null)
41                 return context.Set<T>().ToList();
42             else
43                 return context.Set<T>().Where(conditions).ToList();
44         }
45 
46         public PagedList<T> FindAllByPage<T, S>(Expression<Func<T, bool>> conditions, Expression<Func<T, S>> orderBy, int pageSize, int pageIndex) where T : class
47         {
48             var queryList = conditions == null ? context.Set<T>() : context.Set<T>().Where(conditions) as IQueryable<T>;
49 
50             return queryList.OrderByDescending(orderBy).ToPagedList(pageIndex, pageSize);
51         }
52 
53         public void Dispose()
54         {
55             this.context.Dispose();
56         }


4. 数据层基于Entity Framwork code First:

  • DBContext

    View Code

     1 public class DbContext : System.Data.Entity.DbContext
     2     {
     3         public DbContext()
     4             : base("MyDbContext")
     5         {
     6             this.Configuration.ProxyCreationEnabled = false;
     7         }
     8         
     9         public DbSet<Category> Categories { getset; }
    10         public DbSet<Product> Products { getset; }
    11     }

  • Model Mapping

    View Code

     1 [Table("Product")]
     2     public partial class Product
     3     {
     4         public int Id { getset; }
     5 
     6         [StringLength(50)]
     7         [Required(ErrorMessage = "名称不能为空")]
     8         public string Name { getset; }
     9 
    10         public int Size { getset; }
    11 
    12         [StringLength(300)]
    13         public string PhotoUrl { getset; }
    14 
    15         public DateTime AddTime { getset; }
    16 
    17         public int CategoryId { getset; }
    18         public virtual Category Category { getset; }
    19     }


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

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

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

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

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

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

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

  • DAL调用通用的Repository接口

    1 public PagedList<T> FindAllByPage<T, S>(Expression<Func<T, bool>> conditions, Expression<Func<T, S>> orderBy, int pageSize, int pageIndex) where T : class
    2         {
    3             var queryList = conditions == null ? context.Set<T>() : context.Set<T>().Where(conditions) as IQueryable<T>;
    4 
    5             return queryList.OrderByDescending(orderBy).ToPagedList(pageIndex, pageSize);
    6         }


6. 最后提供源码下载

https://files.cnblogs.com/guozili/EasyEF.rar

posted on 2012-09-03 08:26  guozili  阅读(22202)  评论()    收藏  举报