掃二維碼與項(xiàng)目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
本項(xiàng)目結(jié)合EF 4.3及WCF實(shí)現(xiàn)了經(jīng)典三層架構(gòu),各層面向接口,WCF實(shí)現(xiàn)SOA,Repository封裝調(diào)用,在此基礎(chǔ)上實(shí)現(xiàn)了WCFContext,動態(tài)服務(wù)調(diào)用及一個分頁的實(shí)例。

創(chuàng)新互聯(lián)擁有一支富有激情的企業(yè)網(wǎng)站制作團(tuán)隊(duì),在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)深耕10年,專業(yè)且經(jīng)驗(yàn)豐富。10年網(wǎng)站優(yōu)化營銷經(jīng)驗(yàn),我們已為近千家中小企業(yè)提供了成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)解決方案,按需制作,設(shè)計(jì)滿意,售后服務(wù)無憂。所有客戶皆提供一年免費(fèi)網(wǎng)站維護(hù)!
1. 項(xiàng)目架構(gòu)圖:
2. 項(xiàng)目解決方案:
3. Wcf Service的實(shí)現(xiàn):
3. 數(shù)據(jù)層Repository的實(shí)現(xiàn):
- 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. 數(shù)據(jù)層基于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調(diào)用服務(wù)端分頁的實(shí)例:
- 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

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流