成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

分享基于EF+WCF的通用三層架構及解析

開發 架構
本項目結合EF 4.3及WCF實現了經典三層架構,各層面向接口,WCF實現SOA,Repository封裝調用,在此基礎上實現了WCFContext,動態服務調用及一個分頁的實例。

本項目結合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的實現:

 

  • 通過用來訪問領域對象的一個類似集合的接口,在領域與數據映射層之間進行協調,將領域模型從客戶代碼和數據映射層之間解耦出來,具體實現代碼:
  1. View Code   
  2.  public class DaoBase : IRepository, IDisposable  
  3.      {  
  4.          public DbContext context;  
  5.    
  6.          public DaoBase()  
  7.          {  
  8.              this.context = new EasyEF.DAL.DbContext();  
  9.          }  
  10.    
  11.          public T Update<T>(T entity) where T : class 
  12.          {  
  13.              var set = context.Set<T>();  
  14.              set.Attach(entity);  
  15.              context.Entry<T>(entity).State = EntityState.Modified;  
  16.              context.SaveChanges();  
  17.    
  18.              return entity;  
  19.          }  
  20.    
  21.          public T Insert<T>(T entity) where T : class 
  22.          {  
  23.              context.Set<T>().Add(entity);  
  24.              context.SaveChanges();  
  25.              return entity;  
  26.          }  
  27.    
  28.          public void Delete<T>(T entity) where T : class 
  29.          {  
  30.              context.Entry<T>(entity).State = EntityState.Deleted;  
  31.              context.SaveChanges();  
  32.          }  
  33.    
  34.          public T Find<T>(params object[] keyValues) where T : class 
  35.          {  
  36.              return context.Set<T>().Find(keyValues);  
  37.          }  
  38.    
  39.          public List<T> FindAll<T>(Expression<Func<T, bool>> conditions = null) where T : class 
  40.          {  
  41.              if (conditions == null)  
  42.                  return context.Set<T>().ToList();  
  43.              else 
  44.                  return context.Set<T>().Where(conditions).ToList();  
  45.          }  
  46.    
  47.          public PagedList<T> FindAllByPage<T, S>(Expression<Func<T, bool>> conditions, Expression<Func<T, S>> orderBy, int pageSize, int pageIndex) where T : class 
  48.          {  
  49.              var queryList = conditions == null ? context.Set<T>() : context.Set<T>().Where(conditions) as IQueryable<T>;  
  50.    
  51.              return queryList.OrderByDescending(orderBy).ToPagedList(pageIndex, pageSize);  
  52.          }  
  53.    
  54.          public void Dispose()  
  55.          {  
  56.              this.context.Dispose();  
  57.          } 

4. 數據層基于Entity Framwork code First:

DBContext

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

Model Mapping

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

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. 最后提供源碼下

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

原文鏈接:http://www.cnblogs.com/guozili/archive/2012/09/03/2667429.html

責任編輯:林師授 來源: 博客園
相關推薦

2011-04-19 13:53:41

三層架構

2009-07-28 17:25:14

ASP.NET三層結構

2009-05-06 09:40:04

LINQWEB開發構架

2009-08-26 18:20:42

三層架構

2009-07-28 17:18:33

2013-01-09 11:00:20

架構開發三層架構.NET架構

2011-08-08 14:14:03

架構

2018-03-08 15:30:31

超融合架構傳統三層架構

2010-01-11 16:26:42

三層交換機作用

2012-02-03 09:44:33

.NET

2010-01-28 15:29:52

三層交換機

2015-07-02 10:57:11

General框架架構開發

2018-10-31 14:32:53

數據中心網絡架構

2009-04-30 15:56:50

三層架構MVCMVP

2009-07-28 15:08:50

MVC三層架構實例

2012-07-16 10:19:02

MongoDB

2011-05-12 14:24:14

三層架構

2010-02-07 14:55:33

第三層交換技術

2010-02-03 14:31:12

三層交換機

2012-02-07 10:40:13

MVCJava
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品一区二区三区四区五区 | 国产一区二区麻豆 | 亚洲国产中文字幕 | 国产一区二区三区在线 | 青青久久久 | 国产免费一区二区三区 | 91精品一区二区三区久久久久久 | 欧美一区二区三区 | 久久成人免费视频 | 97视频成人 | 国产真实精品久久二三区 | 99国内精品| 亚洲社区在线 | 日韩2020狼一二三 | 亚洲精品天堂 | 在线观看 亚洲 | 中文字幕精品一区 | 一区二区视屏 | 国产精品日韩在线观看一区二区 | 国产一区二区三区在线免费 | 综合精品久久久 | 日韩欧美在线一区 | 国产网站在线播放 | 一级毛片免费完整视频 | 一区二区三区中文字幕 | 夜夜精品浪潮av一区二区三区 | 国产三区精品 | 免费看国产一级特黄aaaa大片 | 亚洲成在线观看 | 日韩精品免费 | 欧美日韩国产在线观看 | 日日操夜夜操天天操 | 欧美xxxx做受欧美 | 91亚洲精品国偷拍自产在线观看 | 天天综合网7799精品 | а天堂中文最新一区二区三区 | 91亚洲精品久久久电影 | 91综合网 | 天天拍天天射 | 国产欧美一区二区三区在线播放 | 亚洲久草 |