首页 技术 正文
技术 2022年11月23日
0 收藏 692 点赞 3,041 浏览 2020 个字

这2天实际开发中明确的东西,在这篇博文中记录一下。

之前对是否需要自己封装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: ,
useAsync: true))
{
await fs.WriteAsync(bits, , 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();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848