好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

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

本项目结合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

 

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

DBContext

View Code

Model Mapping 

View Code

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


 

 

标签:  Entity Framework ,  Wcf ,  三层架构

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于分享基于EF+WCF的通用三层架构及解析的详细内容...

  阅读:42次