一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](四)
前言
上一篇《一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)》,我们完成了:
* 引用SqlSugar
* 使用SqlSugar对Repository类的改造
并成功使用PostRepository来查询到了数据,今天我们来创建一个新的服务层以及安装配置依赖注入框架组件Autofac等。
本篇知识要点
* 创建服务层:TsBlog.Services
* 创建服务接口
* 实现服务接口
* 创建仓储接口
* 安装Autofac依赖注入组件
* 注册配置Autofac 依赖注入
教程内容
创建服务层
选中解决方案中的解决方案文件夹[1.Libraries],右键单击=>>添加=>>新项目,在弹出的对话框中添加一个.NET Framework 4.6.2的C#类库项目,命名为:TsBlog.Services。项目创建成功后,删除自动生成的Class1.cs文件。
由于服务层需要依赖于仓储层,所以首先切换到仓储层[TsBlog.Repositories]项目中,创建博文的仓储接口类:IPostRepository,代码如下:
using System.Collections.Generic;
using TsBlog.Domain.Entities;
namespace TsBlog.Repositories
{
public interface IPostRepository
{
/// <summary>
/// 根据ID查询单条数据
/// </summary>
/// <param name="id">ID</param>
/// <returns></returns>
Post FindById(int id);
/// <summary>
/// 查询所有数据(无分页,大数量时请慎用)
/// </summary>
/// <returns></returns>
IEnumerable<Post> FindAll();
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
int Insert(Post entity);
/// <summary>
/// 更新实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
bool Update(Post entity);
/// <summary>
/// 根据实体删除一条数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
bool Delete(Post entity);
/// <summary>
/// 删除指定ID的数据
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
bool DeleteById(object id);
/// <summary>
/// 删除指定ID集合的数据(批量删除)
/// </summary>
/// <param name="ids">主键ID集合</param>
/// <returns></returns>
bool DeleteByIds(object[] ids);
}
}
再切换到服务层,在刚才创建的服务层项目中首先引用仓储层,并分别创建以下服务接口和类文件:
IPostService.cs:
using System.Collections.Generic;
using TsBlog.Domain.Entities;
namespace TsBlog.Services
{
public interface IPostService
{
/// <summary>
/// 根据ID查询单条数据
/// </summary>
/// <param name="id">ID</param>
/// <returns></returns>
Post FindById(int id);
/// <summary>
/// 查询所有数据(无分页,大数量时请慎用)
/// </summary>
/// <returns></returns>
IEnumerable<Post> FindAll();
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
int Insert(Post entity);
/// <summary>
/// 更新实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
bool Update(Post entity);
/// <summary>
/// 根据实体删除一条数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
bool Delete(Post entity);
/// <summary>
/// 删除指定ID的数据
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
bool DeleteById(object id);
/// <summary>
/// 删除指定ID集合的数据(批量删除)
/// </summary>
/// <param name="ids">主键ID集合</param>
/// <returns></returns>
bool DeleteByIds(object[] ids);
}
}
PostService.cs
using System.Collections.Generic;
using TsBlog.Domain.Entities;
using TsBlog.Repositories;
namespace TsBlog.Services
{
public class PostService : IPostService
{
private readonly IPostRepository _postRepository;
public PostService(IPostRepository 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 int Insert(Post entity)
{
return _postRepository.Insert(entity);
}
public bool Update(Post entity)
{
return _postRepository.Update(entity);
}
}
}
最后,我们再切换到仓储层,在PostRepository文件中使用IPostRepository接口并使用SqlSugar实现该接口中的所有数据操作的方法,
PostRepository.cs
using System.Collections.Generic;
using TsBlog.Domain.Entities;
namespace TsBlog.Repositories
{
/// <summary>
/// POST表的数据库操作类
/// </summary>
public class PostRepository : IPostRepository
{
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="id">Post ID</param>
/// <returns></returns>
public Post FindById(int id)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var entity = db.Queryable<Post>().Single(x => x.Id == id);
return entity;
}
}
/// <summary>
/// 查询所有数据(无分页,大数量时请慎用)
/// </summary>
/// <returns></returns>
public IEnumerable<Post> FindAll()
{
#region SqlSugar读取方式
using (var db = DbFactory.GetSqlSugarClient())
{
var list = db.Queryable<Post>().ToList();
return list;
}
#endregion
}
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public int Insert(Post entity)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Insertable(entity).ExecuteReturnBigIdentity();
//返回的i是long类型,这里你可以根据你的业务需要进行处理
return (int)i;
}
}
/// <summary>
/// 更新实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public bool Update(Post entity)
{
using (var db = DbFactory.GetSqlSugarClient())
{
//这种方式会以主键为条件
var i = db.Updateable(entity).ExecuteCommand();
return i > 0;
}
}
/// <summary>
/// 根据实体删除一条数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public bool Delete(Post entity)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Deleteable(entity).ExecuteCommand();
return i > 0;
}
}
/// <summary>
/// 删除指定ID的数据
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
public bool DeleteById(object id)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Deleteable<Post>(id).ExecuteCommand();
return i > 0;
}
}
/// <summary>
/// 删除指定ID集合的数据(批量删除)
/// </summary>
/// <param name="ids">主键ID集合</param>
/// <returns></returns>
public bool DeleteByIds(object[] ids)
{
using (var db = DbFactory.GetSqlSugarClient())
{
var i = db.Deleteable<Post>().In(ids).ExecuteCommand();
return i > 0;
}
}
}
}
到这里,我们的仓储和服务层准备工作就完成了,接下来安装依赖注入组件:Autofac
安装Autofac
选择解决方案夹[2.Persentation]中的Web项目[TsBlog.Frontend],在"引用"("References")上单击右键,调出Nuget程序包管理界面,搜索"autofac",如下:
Autofac的当前版本为:v4.6.2
同时,再搜索"Autofac.Mvc5",如下:
配置/注册依赖选项
Autofac安装完成之后,我们需要对依赖的接口对实现在Autofac中进行注册,本示例的Autofac配置在Global.asax文件中(请确保TsBlog.Frontend项目中引用了:TsBlog.Domain,TsBlog.Repositories,TsBlog.Servcies这本个项目),如下:
Global.asax
using Autofac;
using Autofac.Integration.Mvc;
using System.Web.Mvc;
using System.Web.Routing;
using TsBlog.Repositories;
using TsBlog.Services;
namespace TsBlog.Frontend
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleConfig.RegisterBundles(BundleTable.Bundles);
AutofacRegister();
}
private void AutofacRegister()
{
var builder = new ContainerBuilder();
//注册MvcApplication程序集中所有的控制器
builder.RegisterControllers(typeof(MvcApplication).Assembly);
//注册仓储层服务
builder.RegisterType<PostRepository>().As<IPostRepository>();
//注册服务层服务
builder.RegisterType<PostService>().As<IPostService>();
//注册过滤器
builder.RegisterFilterProvider();
var container = builder.Build();
//设置依赖注入解析器
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
然后,我们修改控制器文件夹中的HomeController,修改后的代码如下:
HomeController.cs
using System.Web.Mvc;
using TsBlog.Services;
namespace TsBlog.Frontend.Controllers
{
public class HomeController : Controller
{
private readonly IPostService _postService;
public HomeController(IPostService postService)
{
_postService = postService;
}
public ActionResult Index()
{
return View();
}
public ActionResult Post()
{
//var postRepository = new PostRepository();
//var post = postRepository.FindById(1);
//return View(post);
var post = _postService.FindById(1);
return View(post);
}
}
}
再次按F5运行,打开页面:http://localhost:54739/home/post,这次我们可以看到和前两篇一样的运行效果了:
本文的源码托管地址:https://github.com/lampo1024/TsBlog/releases/tag/v1.4
本文学习到此结束,本系列未完待续......
如果你喜欢Rector的本系列文章,请为我点个大大的赞,以支持Rector在后续的写作中更有基(激)情,哈哈。。。
本文同步发表至 图享网 《一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](四)》
一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](四)的更多相关文章
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](五)
前言 Hi,大家好,我是Rector 时间飞逝,一个星期又过去了,今天还是星期五,Rector在图享网继续跟大家分享系列文本:一步一步创建ASP.NET MVC5程序[Repository+Autof ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](一)
前言 大家好,我是Rector 从今天开始,Rector将为大家推出一个关于创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar]的文章系列, ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)
前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)>我们通过如下操作: 创建实体及工具类 创建Re ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)
前言: 在本系列第一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](一)>中,我为大家介绍了搭建空白解决方案以 ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](六)
前言 大家好,我是Rector 又是星期五,很兴奋,很高兴,很high...啦啦啦... Rector在图享网又和大家见面啦!!!上一篇<一步一步创建ASP.NET MVC5程序[Reposit ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](七)
前言 大家好,我依旧是你们的老朋友Rector,很高兴又在周五的时候准时和大家见面. Rector的系列文章[一步一步创建ASP.NET MVC5程序[Repository+Autofac+Autom ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](八)
前言 Hi, 大家好,还是星期五,还是Rector,又在图享网准时和大家见面了. 今天给大家带来系列教程<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Auto ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](九)
前言 童鞋们,大家好 我是专注.NET开发者社区建设的实践者Rector. 首先,为自己间隔了两个星期五再更新本系列文章找个不充分的理由:Rector最近工作,家庭的各种事务所致,希望大家谅解. 本文 ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](十)
前言 朋友们, 大家好,我还是Rector,写ASP.NET MVC 5系列文章[一步一步创建ASP.NET MVC5程序Repository+Autofac+Automapper+SqlSugar] ...
随机推荐
- Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题
前提准备: 搭建好Angular开发环境 1 安装Visual Studio Code 教程简单,不会的去问度娘 2 安装Chrome浏览器 教程简单,不会的趣闻度娘 3 Visual Studio ...
- JAVA IO分析三:IO总结&文件分割与合并实例
时间飞逝,马上就要到2018年了,今天我们将要学习的是IO流学习的最后一节,即总结回顾前面所学,并学习一个案例用于前面所学的实际操作,下面我们就开始本节的学习: 一.原理与概念 一.概念流:流动 .流 ...
- InfluxDB:cannot use field in group by clause
最近在使用InfluxDB时,发现一个很奇怪的问题,一个本来正常的功能,做了一次改动后,就不能正常显示了. 一.查询语句 SELECT MEMORY FROM "ACM_PROCESS_MO ...
- 使用canvas实现绚丽的时钟特效
源码 https://github.com/2016Messi/Gorgeous-clock 效果展示 https://2016messi.github.io/Gorgeous-clock/ 如果各位 ...
- ES6之Symbol
ES6中Symbol是为了防止属性名冲突而引入的,是独一无二的.Symbol值是通过Symbol函数生成.Symbol值不能与其他类型的值运算否则会报错且Symbol的值可以转换为字符串或者是布尔值但 ...
- Find the Maximum sum
Given an array of n elements.Find the maximum sum when the array elements will be arranged in such w ...
- 修改Linux内核参数提高Nginx服务器并发性能
当linux下Nginx达到并发数很高,TCP TIME_WAIT套接字数量经常达到两.三万,这样服务器很容易被拖死.事实上,我们可以简单的通过修改Linux内核参数,可以减少Nginx服务器 的TI ...
- 快速开发 jQuery 插件的 10 大技巧
在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模式.这样我就可以 copy & paste 大部分的代码结构,只要专注最主要的逻辑代码就行了. ...
- 关于oracle数据库 跨表查询建立 视图的方法
工作中很多时候都会遇到需要将两个不同的表空间甚至数据库的表进行联合查询或者建立视图的情况. 不同的表空间查询我们可以通过在将要查询的表前面加上 表空间的对应的用户名来实现,如有两个表空间分别对应两个用 ...
- TurnipBit之DIY无线遥控智能小车
一.准备工作 TurnipBit 开发板 2块 TurnipBit 扩展板 1块 数据线 1条 智能小车器件 1套 电机驱动模块(L298N) 1个 在线可视化编程 点击进入 二.思路设计 2 ...