PostRepository.cs
using TsBlog.Domain.Entities; namespace TsBlog.Repositories { /// <summary> /// POST表的数据库操作类 /// </summary> public class PostRepository : GenericRepository<Post> { } }IPostRepository 和 PostRepository 是不是一下简洁了很多,但实现的方法还是和重构前是一样的。
怎么样,如果我们再新增用户表的仓储接口和仓储实现是不是非常简单了呢?再也不用为重复的增,删,改,查操作来复制,粘贴了。
配置基于接口的依赖注入在项目【TsBlog.Repositories】中添加接口类 IDependency.cs :
namespace TsBlog.Repositories { /// <summary> /// 依赖注入的接口约束 /// </summary> public interface IDependency { } }在泛型仓储抽象基类 GenericRepository.cs 中添加 IDependency 接口约束 :
public abstract class GenericRepository<T> : IDependency, IRepository<T> where T : class, new()打开项目【TsBlog.Frontend】中的 Global.asax 重新配置 AutofacRegister 方法,如下:
private void AutofacRegister() { var builder = new ContainerBuilder(); //注册MvcApplication程序集中所有的控制器 builder.RegisterControllers(typeof(MvcApplication).Assembly); //注册仓储层服务 //builder.RegisterType<PostRepository>().As<IPostRepository>(); //注册基于接口约束的实体 var assembly = AppDomain.CurrentDomain.GetAssemblies(); builder.RegisterAssemblyTypes(assembly) .Where( t => t.GetInterfaces() .Any(i => i.IsAssignableFrom(typeof(IDependency))) ) .AsImplementedInterfaces() .InstancePerDependency(); //注册服务层服务 builder.RegisterType<PostService>().As<IPostService>(); //注册过滤器 builder.RegisterFilterProvider(); var container = builder.Build(); //设置依赖注入解析器 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); }按F5运行,是否报错了?
是的,因为我们刚才修改了泛型仓储中Insert的返回类型,所以,修改 IPostService.cs中的Insert的返回类型为long:
修改后的 IPostService.cs:
using System.Collections.Generic; using TsBlog.Domain.Entities; namespace TsBlog.Services { public interface IPostService { /// <summary> /// 根据ID查询单条数据 /// </summary> /// <param>ID</param> /// <returns></returns> Post FindById(int id); /// <summary> /// 查询所有数据(无分页,大数量时请慎用) /// </summary> /// <returns></returns> IEnumerable<Post> FindAll(); /// <summary> /// 写入实体数据 /// </summary> /// <param>博文实体类</param> /// <returns></returns> long Insert(Post entity); /// <summary> /// 更新实体数据 /// </summary> /// <param>博文实体类</param> /// <returns></returns> bool Update(Post entity); /// <summary> /// 根据实体删除一条数据 /// </summary> /// <param>博文实体类</param> /// <returns></returns> bool Delete(Post entity); /// <summary> /// 删除指定ID的数据 /// </summary> /// <param>主键ID</param> /// <returns></returns> bool DeleteById(object id); /// <summary> /// 删除指定ID集合的数据(批量删除) /// </summary> /// <param>主键ID集合</param> /// <returns></returns> bool DeleteByIds(object[] ids); } }再修改 PostService.cs中的Insert的返回类型为long:
public long Insert(Post entity) { return _postRepository.Insert(entity); }修改后的 PostService.cs:
using System.Collections.Generic; using TsBlog.Domain.Entities; using TsBlog.Repositories; namespace TsBlog.Services { public class PostService : IPostService { private readonly IRepository<Post> _postRepository; public PostService(IRepository<Post> postRepository) { _postRepository = postRepository; } public bool Delete(Post entity) { return _postRepository.Delete(entity); } public bool DeleteById(object id) { return _postRepository.DeleteById(id); } public bool DeleteByIds(object[] ids) { return _postRepository.DeleteByIds(ids); } public IEnumerable<Post> FindAll() { return _postRepository.FindAll(); } public Post FindById(int id) { return _postRepository.FindById(id); } public long Insert(Post entity) { return _postRepository.Insert(entity); } public bool Update(Post entity) { return _postRepository.Update(entity); } } }