日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
分享基于EF+WCF的通用三層架構(gòu)及解析

本項目結(jié)合EF 4.3及WCF實現(xiàn)了經(jīng)典三層架構(gòu),各層面向接口,WCF實現(xiàn)SOA,Repository封裝調(diào)用,在此基礎上實現(xiàn)了WCFContext,動態(tài)服務調(diào)用及一個分頁的實例。

創(chuàng)新互聯(lián)擁有一支富有激情的企業(yè)網(wǎng)站制作團隊,在互聯(lián)網(wǎng)網(wǎng)站建設行業(yè)深耕10年,專業(yè)且經(jīng)驗豐富。10年網(wǎng)站優(yōu)化營銷經(jīng)驗,我們已為近千家中小企業(yè)提供了成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設解決方案,按需制作,設計滿意,售后服務無憂。所有客戶皆提供一年免費網(wǎng)站維護!

1. 項目架構(gòu)圖:


2. 項目解決方案:

  • 在傳統(tǒng)的三層架構(gòu)上增加了WcfService(服務端),WcfClientProxy(客戶端服務調(diào)用),及WcfExtension(一些擴展)

3. Wcf Service的實現(xiàn):

  • 工廠實現(xiàn)了RemoteServiceFactory(用于遠程調(diào)用)和RefServiceFactory(本地引用調(diào)用服務層)生成客戶端代理,都需要實現(xiàn)IServiceFactory的“IService CreateService();”
  • RemoteServiceFactory通過ChannelFactory動態(tài)產(chǎn)生客戶端代理類IService,并將此對象進行緩存
  • WCFExtension實現(xiàn)了WCFContext,可傳輸用戶登陸或IP上下文信息,以及攔截方法寫Log的機制,具體可以參考 http://www.cnblogs.com/lovecindywang/archive/2012/03/01/2376144.html

3. 數(shù)據(jù)層Repository的實現(xiàn):

  • 通過用來訪問領域?qū)ο蟮囊粋€類似集合的接口,在領域與數(shù)據(jù)映射層之間進行協(xié)調(diào),將領域模型從客戶代碼和數(shù)據(jù)映射層之間解耦出來,具體實現(xiàn)代碼:
 
 
 
 
  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 entity) where T : class 
  12.          {  
  13.              var set = context.Set();  
  14.              set.Attach(entity);  
  15.              context.Entry(entity).State = EntityState.Modified;  
  16.              context.SaveChanges();  
  17.    
  18.              return entity;  
  19.          }  
  20.    
  21.          public T Insert(T entity) where T : class 
  22.          {  
  23.              context.Set().Add(entity);  
  24.              context.SaveChanges();  
  25.              return entity;  
  26.          }  
  27.    
  28.          public void Delete(T entity) where T : class 
  29.          {  
  30.              context.Entry(entity).State = EntityState.Deleted;  
  31.              context.SaveChanges();  
  32.          }  
  33.    
  34.          public T Find(params object[] keyValues) where T : class 
  35.          {  
  36.              return context.Set().Find(keyValues);  
  37.          }  
  38.    
  39.          public List FindAll(Expression> conditions = null) where T : class 
  40.          {  
  41.              if (conditions == null)  
  42.                  return context.Set().ToList();  
  43.              else 
  44.                  return context.Set().Where(conditions).ToList();  
  45.          }  
  46.    
  47.          public PagedList FindAllByPage(Expression> conditions, Expression> orderBy, int pageSize, int pageIndex) where T : class 
  48.          {  
  49.              var queryList = conditions == null ? context.Set() : context.Set().Where(conditions) as IQueryable;  
  50.    
  51.              return queryList.OrderByDescending(orderBy).ToPagedList(pageIndex, pageSize);  
  52.          }  
  53.    
  54.          public void Dispose()  
  55.          {  
  56.              this.context.Dispose();  
  57.          } 

4. 數(shù)據(jù)層基于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 Categories { get; set; }  
  11.          public DbSet Products { get; set; }  
  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調(diào)用服務端分頁的實例:

  • MVC調(diào)用Wcf客戶代理請求分頁數(shù)據(jù)集合
 
 
 
 
  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信息并調(diào)用數(shù)據(jù)層
 
 
 
 
  1. public PagedList GetProducts(int pageSize, int pageIndex, int categoryId = 0)  
  2.          {  
  3.              //Test WCFContext  
  4.              var context = WCFContext.Current.Operater;  
  5.              return this.dao.FindAllByPage(p => categoryId == 0 ? true : p.CategoryId == categoryId, p => p.Id, pageSize, pageIndex);  
  6.          } 
  • DAL調(diào)用通用的Repository接口
 
 
 
 
  1. public PagedList FindAllByPage(Expression> conditions, Expression> orderBy, int pageSize, int pageIndex) where T : class 
  2.          {  
  3.              var queryList = conditions == null ? context.Set() : context.Set().Where(conditions) as IQueryable;  
  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


當前名稱:分享基于EF+WCF的通用三層架構(gòu)及解析
當前網(wǎng)址:http://www.dlmjj.cn/article/dhsdpde.html