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

创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕10年,专业且经验丰富。10年网站优化营销经验,我们已为近千家中小企业提供了成都网站制作、做网站、外贸营销网站建设解决方案,按需制作,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!
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
- public class DaoBase : IRepository, IDisposable
- {
- public DbContext context;
- public DaoBase()
- {
- this.context = new EasyEF.DAL.DbContext();
- }
- public T Update
(T entity) where T : class - {
- var set = context.Set
(); - set.Attach(entity);
- context.Entry
(entity).State = EntityState.Modified; - context.SaveChanges();
- return entity;
- }
- public T Insert
(T entity) where T : class - {
- context.Set
().Add(entity); - context.SaveChanges();
- return entity;
- }
- public void Delete
(T entity) where T : class - {
- context.Entry
(entity).State = EntityState.Deleted; - context.SaveChanges();
- }
- public T Find
(params object[] keyValues) where T : class - {
- return context.Set
().Find(keyValues); - }
- public List
FindAll (Expression > conditions = null) where T : class - {
- if (conditions == null)
- return context.Set
().ToList(); - else
- return context.Set
().Where(conditions).ToList(); - }
- public PagedList
FindAllByPage (Expression > conditions, Expression > orderBy, int pageSize, int pageIndex) where T : class - {
- var queryList = conditions == null ? context.Set
() : context.Set ().Where(conditions) as IQueryable ; - return queryList.OrderByDescending(orderBy).ToPagedList(pageIndex, pageSize);
- }
- public void Dispose()
- {
- this.context.Dispose();
- }
4. 数据层基于Entity Framwork code First:
DBContext
- View Code
- public class DbContext : System.Data.Entity.DbContext
- {
- public DbContext()
- : base("MyDbContext")
- {
- this.Configuration.ProxyCreationEnabled = false;
- }
- public DbSet
Categories { get; set; } - public DbSet
Products { get; set; } - }
Model Mapping
- View Code
- [Table("Product")]
- public partial class Product
- {
- public int Id { get; set; }
- [StringLength(50)]
- [Required(ErrorMessage = "名称不能为空")]
- public string Name { get; set; }
- 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
GetProducts(int pageSize, int pageIndex, int categoryId = 0) - {
- //Test WCFContext
- var context = WCFContext.Current.Operater;
- return this.dao.FindAllByPage
(p => categoryId == 0 ? true : p.CategoryId == categoryId, p => p.Id, pageSize, pageIndex); - }
- DAL调用通用的Repository接口
- public PagedList
FindAllByPage (Expression > conditions, Expression > orderBy, int pageSize, int pageIndex) where T : class - {
- var queryList = conditions == null ? context.Set
() : context.Set ().Where(conditions) as IQueryable ; - return queryList.OrderByDescending(orderBy).ToPagedList(pageIndex, pageSize);
- }
6. 最后提供源码下
http://files.cnblogs.com/guozili/EasyEF.rar
原文链接:http://www.cnblogs.com/guozili/archive/2012/09/03/2667429.html
网页名称:分享基于EF+WCF的通用三层架构及解析
链接URL:http://jxjierui.cn/article/dhsdpde.html


咨询
建站咨询
