efactive java,EF封装类,供参考!

以下是我对EF DB FIRST 生成的ObjectContext类进行封装,代码如下,供参考学习:

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Text;

5 using System.Data.Objects.DataClasses;

6 using ZBService.Model;

7 using System.Linq.Expressions;

8

9 namespace ZBService

10 {

11 public abstract class ServiceBase where T:EntityObject

12 {

13 protected mZhaoBiaoEntities zbEntities = new mZhaoBiaoEntities();

14

15 ///

16 /// 判断是否存在

17 ///

18 ///

19 ///

20 public bool Exist(Expression> whereExpr)

21 {

22 return (this.Count(whereExpr) > 0);

23 }

24

25 ///

26 /// 获取记录数

27 ///

28 ///

29 ///

30 public int Count(Expression> whereExpr)

31 {

32 return zbEntities.CreateObjectSet().Where(whereExpr).Count();

33 }

34

35 ///

36 /// 查找实体对象

37 ///

38 ///

39 ///

40 public T Find(Expression> whereExpr)

41 {

42 return zbEntities.CreateObjectSet().Where(whereExpr).FirstOrDefault();

43 }

44

45 ///

46 /// 查找实体对象列表

47 ///

48 ///

49 ///

50 public IEnumerable FindList(Expression> whereExpr, Expression> orderbyExpr, int orderDirection)

51 {

52 return this.FindList(whereExpr,t=>t,orderbyExpr,orderDirection);

53 }

54

55 ///

56 /// 查找实体对象列表

57 ///

58 ///

59 ///

60 ///

61 ///

62 ///

63 ///

64 ///

65 ///

66 public IEnumerable FindList(Expression> whereExpr, Expression> selectExpr,Expression> orderbyExpr,int orderDirection,int returnCount=-1)

67 {

68 var result = zbEntities.CreateObjectSet().Where(whereExpr).Select(selectExpr);

69 if (result != null && result.Count() > 0)

70 {

71 if (returnCount > 0)

72 {

73 if (orderDirection > 0)

74 {

75 result = result.OrderByDescending(orderbyExpr).Take(returnCount);

76 }

77 else

78 {

79 result = result.OrderBy(orderbyExpr).Take(returnCount);

80 }

81 }

82 return result.ToList();

83 }

84 return null;

85 }

86

87 ///

88 /// 分页查找实体对象列表

89 ///

90 ///

91 ///

92 ///

93 ///

94 ///

95 ///

96 ///

97 ///

98 ///

99 ///

100 public IEnumerable FindListByPage(Expression> whereExpr, Expression> selectExpr,Expression> orderbyExpr,int orderDirection,int pageSize,int pageNo,out int recordCount)

101 {

102 var result = zbEntities.CreateObjectSet().Where(whereExpr).Select(selectExpr);

103 recordCount = result.Count();

104

105 if(pageNo>recordCount) pageNo=recordCount;

106 if(pageNo<=0) pageNo=1;

107

108 if (recordCount > 0)

109 {

110 if (recordCount > pageSize)

111 {

112 if (orderDirection > 0)

113 {

114 return result.OrderByDescending(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();

115 }

116 else

117 {

118 return result.OrderBy(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();

119 }

120 }

121 else

122 {

123 if (orderDirection > 0)

124 {

125 return result.OrderByDescending(orderbyExpr).ToList();

126 }

127 else

128 {

129 return result.OrderBy(orderbyExpr).ToList();

130 }

131 }

132

133 }

134 return null;

135 }

136

137

138

139

140 ///

141 /// 增加实体

142 ///

143 ///

144 public virtual void Add(T entity)

145 {

146 this.ValidateEntity(entity,ValidateMode.Add);

147 zbEntities.CreateObjectSet().AddObject(entity);

148 }

149

150

151 ///

152 /// 增加实体列表

153 ///

154 ///

155 public virtual void AddList(IEnumerable entities)

156 {

157 var objSet = zbEntities.CreateObjectSet();

158 foreach (T entity in entities)

159 {

160 this.ValidateEntity(entity, ValidateMode.Add);

161 objSet.AddObject(entity);

162 }

163 }

164

165 ///

166 /// 更新已分离实体,若未分离则不需要执行该方法

167 ///

168 ///

169 public virtual void Update(T entity)

170 {

171 this.ValidateEntity(entity, ValidateMode.Update);

172 zbEntities.CreateObjectSet().ApplyCurrentValues(entity);

173 }

174

175 ///

176 /// 删除实体

177 ///

178 ///

179 public virtual void Delete(T entity)

180 {

181 this.ValidateEntity(entity, ValidateMode.Delete);

182 zbEntities.CreateObjectSet().DeleteObject(entity);

183 }

184

185 ///

186 /// 删除实体

187 ///

188 ///

189 public virtual void Delete(Expression> whereExpr)

190 {

191 var objSet = zbEntities.CreateObjectSet();

192 T entity = objSet.Where(whereExpr).Single();

193 //this.ValidateEntity(entity, ValidateMode.Delete);

194 objSet.DeleteObject(entity);

195 }

196

197 ///

198 /// 删除实体列表

199 ///

200 ///

201 public virtual void DeleteList(IEnumerable entities)

202 {

203 var objSet = zbEntities.CreateObjectSet();

204 foreach (T entity in entities)

205 {

206 //this.ValidateEntity(entity, ValidateMode.Delete);

207 objSet.DeleteObject(entity);

208 }

209 }

210

211

212 ///

213 /// 提交保存所有变更操作

214 ///

215 public void SubmitSave()

216 {

217 zbEntities.SaveChanges();

218 }

219

220

221 ///

222 /// 验证

223 ///

224 ///

225 ///

226 protected virtual void ValidateEntity(T entity,ValidateMode mode=ValidateMode.Add)

227 {

228

229 }

230

231 ///

232 /// 验证模式

233 ///

234 protected enum ValidateMode

235 {

236 Add=0,

237 Update=1,

238 Delete=-1

239 }

240

241 }

242 }

之所以声明为abstract,要求子类必需继承后才能正常使用,这里面有一个ValidateEntity方法,主用于子类在RUD前,验证实体对象数据完整性,可重写也可不重写!

更多IT相关资讯与技术文章,欢迎光临我的个人网站:http://www.zuowenjun.cn/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值