开发笔记:用不用UnitOfWork以及Repository返回什么集合类型
2015-05-19 20:46
453 查看
这2天实际开发中明确的东西,在这篇博文中记录一下。
之前对是否需要自己封装UnitOfWork有些犹豫,因为Entity Framework就是一个UnitOfWork实现, 自己再封装一下显得有些多余。
但是在这次开发中,把涉及数据库操作的实现代码放在最后写,先完成其他层的代码。这种情况下,根本用不了EF,只能先Fake出一个UnitOfWork,这时必须要进行UnitOfWork的封装。
所以,定义了IUnitOfWork接口并实现了一个FakeUnitOfWork,简化的示例代码如下:
IUnitOfWork
FakeUnitOfWork
到写数据库操作代码时,基于EF实现一下IUnitOfWork接口即可。
接下来是Repository返回集合类型的问题。
之前Repository接口多数返回的是IList<T>,这样使用的一个考虑就是让涉及数据库查询的操作尽量在Repository层完成,避免在Application层进行数据库查询操作。
但是今天在Application层的Service中用到了AutoMapper的Project功能,Project的好处是让EF生成的SQL只查询DTO中的字段,但Project扩展方法是针对(且只能针对)IQureryable<T>接口的,所以不得不将Repository接口的返回类型改为IQureryable<T>。
考虑到Project的巨大吸引力以及为了保持Repository返回类型的一致,以后Repository就都统一返回IQureryable<T>吧。
示例代码如下:
IBlogCategoryRepository
BlogCategoryService
之前对是否需要自己封装UnitOfWork有些犹豫,因为Entity Framework就是一个UnitOfWork实现, 自己再封装一下显得有些多余。
但是在这次开发中,把涉及数据库操作的实现代码放在最后写,先完成其他层的代码。这种情况下,根本用不了EF,只能先Fake出一个UnitOfWork,这时必须要进行UnitOfWork的封装。
所以,定义了IUnitOfWork接口并实现了一个FakeUnitOfWork,简化的示例代码如下:
IUnitOfWork
public interface IUnitOfWork : IDisposable { TEntity Add<TEntity>(TEntity entity) where TEntity : class; Task CommitAsync(); }
FakeUnitOfWork
public class FakeUnitOfWork : IUnitOfWoerk { private IList _entities = new List<object>(); public TEntity Add<TEntity>(TEntity entity) where TEntity : class { var property = typeof(TEntity).GetProperty("ID"); if(property != null) { property.SetValue(entity, new Random().Next()); } _entities.Add(entity); return entity; } public async Task CommitAsync() { var bits = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(_entities, Newtonsoft.Json.Formatting.Indented)); using (var fs = new FileStream( path: @"C:\temp\FakeUnitOfWork.json", mode: FileMode.Create, access: FileAccess.Write, share: FileShare.None, bufferSize: 4096, useAsync: true)) { await fs.WriteAsync(bits, 0, bits.Length); } } }
到写数据库操作代码时,基于EF实现一下IUnitOfWork接口即可。
接下来是Repository返回集合类型的问题。
之前Repository接口多数返回的是IList<T>,这样使用的一个考虑就是让涉及数据库查询的操作尽量在Repository层完成,避免在Application层进行数据库查询操作。
但是今天在Application层的Service中用到了AutoMapper的Project功能,Project的好处是让EF生成的SQL只查询DTO中的字段,但Project扩展方法是针对(且只能针对)IQureryable<T>接口的,所以不得不将Repository接口的返回类型改为IQureryable<T>。
考虑到Project的巨大吸引力以及为了保持Repository返回类型的一致,以后Repository就都统一返回IQureryable<T>吧。
示例代码如下:
IBlogCategoryRepository
public interface IBlogCategoryRepository { Task<IQueryable<BlogCategory>> GetCategoriesByBlogId(int blogId, bool activeOnly); }
BlogCategoryService
public class BlogCategoryService : IBlogCategoryService { private IBlogCategoryRepository _blogCategoryRepository; public BlogCategoryServiceImp(IBlogCategoryRepository blogCategoryRepository) { _blogCategoryRepository = blogCategoryRepository; } async Task<IList<BlogCategoryDto>> IBlogCategoryService.GetCategoriesByBlogId(int blogId, bool activeOnly) { return (await _blogCategoryRepository .GetCategoriesByBlogId(blogId, activeOnly)) .Project() .To<BlogCategoryDto>() .ToList(); } }
相关文章推荐
- Android开发笔记——SharedPreferences 存储实体类以及任意类型
- springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定
- Android开发笔记--屏蔽返回键,home键以及其他实体按键
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
- IOS开发学习笔记(十一)——ObjectC中集合类型的使用
- springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定
- Android开发笔记--屏蔽返回键,home键以及其他实体按键
- IOS开发笔记--UIButton类型属性简单归纳以及自定义按钮的设置
- springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
- iPhone开发笔记(一):NavBar+TarBar以及其他
- UWP开发:异步方法返回集合的数据绑定问题
- iPhone开发学习笔记007——Xcode4.2下iPhone多视图开发(自已创建MainWindow.xib和不用MainWindow.xib两种实现)
- 嵌入式软件开发培训笔记——java第五天(API——math/string/集合/日期时间以及java线程)
- SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
- js数据类型隐式转换笔记以及js包装对象
- 【郝斌数据结构自学笔记】26_通过链表排序算法的演示再次详细讨论到底什么是算法以及到底什么是泛型【重点】
- stuts返回xml类型的日期时间,各节点代表什么意思,并将xml格式时间转换成string类型的日期时间
- 黑马程序员—Java学习笔记之集合框架(三)以及1.5新特性
- springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
- c/c++: c++函数返回类型什么情况带const