using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using EFLabModelFirst.JT.Dao;
namespace EFLabModelFirst.JT.Biz
{
public delegate object ExecuteServiceCallBackHandler();
/// <summary>
/// 业务层所有业务类的基类
/// </summary>
public class ServiceTemplate
{
protected DaoTemplate daoTemplate = new DaoTemplate();
/// <summary>
/// 执行业务方法,建议业务层所有业务均执行该方法
/// </summary>
/// <param name="callBackHandler"></param>
/// <returns></returns>
public object ExecuteService(ExecuteServiceCallBackHandler callBackHandler)
{
using(TransactionScope ts=new TransactionScope())
{
object ret = null;
if (callBackHandler != null)
{
ret = callBackHandler();
daoTemplate.Commit();
}
ts.Complete();
return ret;
}
}
}
}
使用了.NET Framework2.0开始就提供的TransactionScope来管理事务,代码非常简单,不是么
注意这句话“daoTemplate.Commit();”可以使我们在业务层对EF的任何操作再次被提交,你在后面的示范中将看到
下面是业务层的一个子类,看看如下使用这个父类来实现事务的:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EFLabModelFirst.JT.Biz { /// <summary> /// 书籍业务类,测试 /// </summary> public class BookService:ServiceTemplate { /// <summary> /// 修改某本书籍的出版社 /// </summary> public void Service1() { base.ExecuteService(() => { //找出C++大学教程这本书 var book = daoTemplate.GetEntity<Book>(b => b.BookTitle == "C++大学教程"); book.PublisherID = 9; return null; }); } /// <summary> /// 修改某本书籍的出版社,第二种做法 /// </summary> public void Service2() { base.ExecuteService(() => { //找出C++大学教程这本书 var book = daoTemplate.GetEntity<Book>(b => b.BookTitle == "C++大学教程"); //找出11号出版社 var publisher = daoTemplate.GetEntityByKey<Publisher>(11); //修改出版社 book.Publisher = publisher; return null; }); } /// <summary> /// 添加书籍和作者的关系 /// </summary> public void Service3() { base.ExecuteService(() => { //找出C++大学教程这本书 var book = daoTemplate.GetEntity<Book>(b => b.BookTitle == "C++大学教程"); //找出两位作者 var author1 = daoTemplate.GetEntity<Author>(a => a.AuthorName == "侯捷"); var author2 = daoTemplate.GetEntity<Author>(a => a.AuthorName == "刘怡"); //添加书和作者的关系 book.Authors.Add(author1); book.Authors.Add(author2); return null; }); } public void Service4() { base.ExecuteService(() => { for (int i = 0; i < 1000; i++) { daoTemplate.FindList<Book>(b => true).Select<Book,Book>(b => { Console.WriteLine(b.BookID+"\t"+b.BookTitle); return b; }).Count(); } return null; }); } /// <summary> /// 使用聚合函数计算最大书籍编号 /// </summary> public void Service5() { //base.daoTemplate.GetObject<Book, string>(null, (IEnumerable<Book> enumerable) => { // return enumerable.Select(b => b.BookID).Max<string>(); //}); string maxID = base.daoTemplate.GetObject<Book, string>(query => { return query.Select(b => b.BookID).Max<string>(); }); Console.WriteLine(maxID); } /// <summary> /// 插入一本新书 /// 0.查询最大编号 /// 1.计算书籍新编号 /// 2.添加新书 /// 3.查询新书编号 /// </summary> public void Service6() { object bookID = base.ExecuteService(() => { //查询最大编号 string maxBookID = daoTemplate.GetObject<Book, string>(query => { return query.Select(b => b.BookID).Max<string>(); }); //计算书籍新编号 maxBookID = "ISBN" + (Convert.ToInt32(maxBookID.Substring(maxBookID.Length - 4, 4))+1).ToString().PadLeft(4,'0'); daoTemplate.AddObject(new Book { BookID = maxBookID, BookDate = DateTime.Now, BookPrice = 120, BookTitle = "Linux c编程", PublisherID = 10 }); return daoTemplate.GetObject<Book, string>(query => { return query.Select(b => b.BookID).Max<string>(); }); }); Console.WriteLine(bookID); } /// <summary> /// 使用GetObject可以取代FindList查询 /// </summary> /// <returns></returns> public void Service7() { var blist = base.daoTemplate.GetObject<Book, List<Book>>(query => { return query.Select<Book, Book>(b => b).ToList(); }); foreach (var item in blist) { Console.WriteLine(item.BookID+"\t"+item.BookTitle); } } /// <summary> /// 使用GetObject可以取代FindList查询,带条件,没有使用抓取,既查看书籍信息,又查看关联的出版社信息会引起查询数据多次 /// </summary> public void Service8() { var blist = base.daoTemplate.GetObject<Book, List<Book>>(query => { return query.Select<Book, Book>(b => b).ToList(); },b=>b.PublisherID==11); foreach (var item in blist) { Console.WriteLine(item.BookID+"\t"+item.BookTitle+"\t"+item.Publisher.PublisherName); } } /// <summary> /// 使用GetObject可以取代FindList查询,带条件,使用抓取,既查看书籍信息,又查看关联的出版社信息仅查询一次 /// </summary> public void Service9() { var blist = base.daoTemplate.GetObject<Book, List<Book>>(query => { return query.Select<Book, Book>(b => b).ToList(); }, b=>true, "Publisher", "Authors");//同时抓取出版社对象和作者集合 foreach (var item in blist) { Console.WriteLine(item.BookID + "\t" + item.BookTitle + "\t" + item.Publisher.PublisherName+"\t"+item.Authors.Count()); } } } }
未经整理,请见谅,以上方法我在编写运行的同时都使用sqlserver的事件探查器观察执行的sql,这为我们分析ef的执行效率和原理带来便利,值得一提的是我的用sqlexpress版本的sqlserver,没有事件探查器,我在csdn上传的有:sqlexpress事件探查器
一些注意事项:
1、生成的模型的代码后置文件XXX.Designer.cs中的ObjectContext的子类需要重写一个方法,,比如我的代码:
/// <summary> /// 初始化新的 modelfirstEntities 对象。 /// </summary> public modelfirstEntities(string connectionString,string defaultContainerName) : base(connectionString, defaultContainerName) { this.ContextOptions.LazyLoadingEnabled = true; OnContextCreated(); }
默认没有这个方法
很多地方我认为值得改进,时间有限,就发到这里了