记一次autofac+dapper+mvc的框架搭建实践
1,环境
.net framework4.7.2,Autofac,Autofac.Mvc5,sql server
2,动机
公司项目用的是ef,之前留下代码的大哥,到处using,代码没有分层,连复用都么的,真的是够了。现在要重构原来的系统,本着高效、简洁、高耦合性的原则采用autofac + dapper的设计(其实我更像用core来做,core速度更快,配置更简单,而且我不想一直用老技术,但是公司大哥不让啊。。。)
3,代码
先引用如下几个包 autofac、autofac.mvc5、autofac.WebApi2

项目结构是这样,比较简单

autofac 配置代码
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using AutoFac.Dapper.DataBase;
using System.Linq;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing; namespace AutoFac.Dapper.Test
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); var builder = new ContainerBuilder(); //注册controller
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //加载要注入的程序集
var iServices = Assembly.Load("AutoFac.Dapper.IService");
var services = Assembly.Load("AutoFac.Dapper.Service"); //根据名称约定(服务层的接口和实现均以Service结尾),实现服务接口和服务实现的依赖
builder.RegisterAssemblyTypes(iServices, services)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerRequest(); //注册主从库的连接对象,每次请求只有一个库的hashcode都是一个 这里分了俩个是为了读写分离设计的
builder.RegisterType<DbMasterSession>().InstancePerRequest();
builder.RegisterType<DbSlaverSession>().InstancePerRequest(); //创建一个Autofac的容器
var container = builder.Build();
//将MVC的控制器对象实例 交由autofac来创建
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
}
dapper对象的代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoFac.Dapper.DataBase
{
/// <summary>
/// 公共的数据库连接管理类,用于使用Autofac时可以自动关闭DbConnection
/// </summary>
public sealed class DbMasterSession : IDisposable
{
bool _disposed;
/// <summary>
/// 用于管理数据库连接
/// </summary>
public IDbConnection DbConnection { get; set; }
public DbMasterSession()
{
DbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MasterConStr"].ConnectionString);
}
/// <summary>
/// 关闭DbConnection
/// </summary>
public void Dispose()
{
if (this._disposed)
return; this._disposed = true; if (this.DbConnection == null)
return; if (this.DbConnection.State != ConnectionState.Closed)
this.DbConnection.Close();
}
}
}
项目地址: https://gitee.com/siming.liu/AutoFac.Dapper.Web
记一次autofac+dapper+mvc的框架搭建实践的更多相关文章
- Spring MVC + jpa框架搭建,及全面分析
一,hibernate与jpa的关系 首先明确一点jpa是什么?以前我就搞不清楚jpa和hibernate的关系. 1,JPA(Java Persistence API)是Sun官方提出的Java持久 ...
- Spring4 mvc+maven 框架搭建(1)
这篇博客其实很早就应该写,早在半年前,因为对SpringMVC感兴趣,便自学了一下Spring.一段时间的学习后,对Spring有了一个基本的了解,于是想着自己动手搭建一个SpringMvc的框架出来 ...
- Spring4 mvc+maven 框架搭建(3)
经过前面两个环节,spring mvc的原料已经准备好了,现在就可以正式开始搭建springmvc框架了. 首先先介绍介绍搭建的框架具有的功能: 1)集成log4j,配置好日志相关并可以打印出相关的日 ...
- Spring+Spring MVC+Hibernate框架搭建实例
前言:这里只是说明整个搭建流程,并不进行原理性的讲解 一 下面所需要用到的数据库配置: 数据库方面,使用mysql创建一个users表,具体代码如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- ASP.NET MVC系列 框架搭建(三)之服务层的搭建
邯郸学步 吾虽是一不知名的菜鸟,但,吾亦有一个从后台程序员成为一名小小架构师梦想,深知架构师不是想想就成的. 吾已工作过一阵子,吾妄想在真正毕业之后工作一年左右就能拿到那个数ten thousand的 ...
- ASP.NET MVC系列 框架搭建(二)之仓储层的优化
大神勿喷,小神默默学. 会了就是不值一提的东西,不会就是绝对的高大上. 最后上传源码.希望能给读者带来一些新的认识及知识. 还没上过头条..各位大神,请点支持一下小弟. 陆续更新.更新到你会为止!! ...
- Spring4 mvc+maven 框架搭建(2)
在上一篇博客中,数据库数据和mybatis相关的java代码已经生成,接下来就可以使用IDE工具来搭建框架了. 在这里,我使用maven构建和管理代码,使用jdk1.8环境. 首先打开Eclipse, ...
- spring+spring MVC+mybatis 框架搭建
1.新建一个javaWeb工程Test,创建时记得勾选web.xml文件. 2.导入需要的jar包,Mybatis所有的jar,spring所有的jar,mysql驱动包. 这里mybatis和spr ...
- ASP.NET MVC系列 框架搭建(一)之仓储层的搭建
大神勿喷,小神默默学. 会了就是不值一提的东西,不会就是绝对的高大上. 最后上传源码.希望能给读者带来一些新的认识及知识. 还没上过头条..各位大神,请点支持一下小弟. 陆续更新.更新到你会为止!! ...
随机推荐
- sql条件查询-日期比较(取年月部分)
查询当年当月的数据: select * from compalete_task where to_Char(create_date,'yyyyMM') = to_Char(sysdate,'yyyyM ...
- [vb.net]控制台进度条的示例
Private Sub ConsoleProcessBar() Dim isBreak As Boolean = False Dim colorBack As ConsoleColor = Conso ...
- LSTM长短期记忆神经网络模型简介
LSTM网络也是一种时间递归神经网络,解决RNN的长期依赖关系. RNN模型在训练时会遇到梯度消失或者爆炸的问题,训练时计算和反向传播,梯度倾向于在每一时刻递增或递减,梯度发散到无穷大或者0..... ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum
https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...
- Livelock
Unlike deadlock, livelocked packets continue to move through the network, but never reach their dest ...
- 【服务器】Nginx文件配置
nginx.conf文件 #运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log lo ...
- s5-14 链路状态路由选择
为什么DV逐渐让位于LS? DV 站的不高,看得不远 完全相信邻居 LS 想办法站得高,看更远 多高.多远? 怎么做? 链路状态路由(Link State) 主要思想 发现 它的邻 ...
- Oracle中根据当前时间和活动类型去数据库查询活动id
活动类型默认是1,代表邀请好友 select * from t_invite_activityinfo twhere sysdate >= t.begintime and sysdate< ...
- AngularJS实战之Controller之间的通信
我们时常会在不同controller之间进行通信,接下来就介绍三种controller之间的通信方式 一.使用$on.$emit和$broadcast进行controller通信 虽然AngularJ ...
- Silverlight中关于ComboBox的各种使用
前端放置了几个ComboBox的控件. <Grid x:Name="LayoutRoot" Background="White"> <Comb ...