本项目结合EF 4.3及WCF实现了经典三层架构,各层面向接口,WCF实现SOA,Repository封装调用,在此基础上实现了WCFContext,动态服务调用及一个分页的实例。
创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕10年,专业且经验丰富。10年网站优化营销经验,我们已为近千家中小企业提供了成都网站制作、做网站、外贸营销网站建设解决方案,按需制作,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!
1. 项目架构图:
2. 项目解决方案:
3. Wcf Service的实现:
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调用服务端分页的实例:
- public ActionResult Index(int pageIndex = 1)
- {
- var products = this.Service.GetProducts(PageSize, pageIndex);
- return View(products);
- }
- protected override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- base.OnActionExecuting(filterContext);
- WCFContext.Current.Operater = new Operater(){Name = "guozili",Time = DateTime.Now,IP = Fetch.UserIp,};
- }
- 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); - }
- 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://www.mswzjz.cn/qtweb/news46/549346.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能