A construction practice of autofac+dapper+mvc framework

1, environment

.net framework4.7.2,Autofac,Autofac.Mvc5,sql server

2, motivation

The company's project uses ef. The big brother who left the code before is using everywhere. The code is not layered, and even reused. That's enough. Now we need to reconstruct the original system, and adopt the design of autofac + dapper based on the principle of high efficiency, simplicity and high coupling (in fact, I prefer to use core, which is faster and simpler in configuration, and I don't want to use the old technology all the time, but the big brother of the company won't let it...)

3, code

First, reference the following packages: Autofac, autofac.mvc5, and autofac.WebApi2

The project structure is so simple


autofac configuration code


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()

            var builder = new ContainerBuilder();

            //register controller

            //Load assembly to inject
            var iServices = Assembly.Load("AutoFac.Dapper.IService");
            var services = Assembly.Load("AutoFac.Dapper.Service");

            //According to the name Convention (both the interface and the implementation of the service layer use the Service At the end of the paper, the dependency of service interface and service implementation is realized
            builder.RegisterAssemblyTypes(iServices, services)
              .Where(t => t.Name.EndsWith("Service"))

            //Register the connection object of the master-slave library. There is only one connection object for each request hashcode It's all one. It's divided into two parts. It's designed for the separation of reading and writing

            //Create a Autofac Container
            var container = builder.Build();
            //take MVC The controller object instance of autofac To create
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Code for dapper object

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>
    /// Public database connection management class for using Autofac Can be closed automatically DbConnection
    /// </summary>
    public sealed class DbMasterSession : IDisposable
        bool _disposed;
        /// <summary>
        /// For managing database connections
        /// </summary>
        public IDbConnection DbConnection { get; set; }
        public DbMasterSession()
            DbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MasterConStr"].ConnectionString);
        /// <summary>
        /// Close DbConnection
        /// </summary>
        public void Dispose()
            if (this._disposed)

            this._disposed = true;

            if (this.DbConnection == null)

            if (this.DbConnection.State != ConnectionState.Closed)


Project address: https://gitee.com/siming.liu/AutoFac.Dapper.Web

Tags: ASP.NET Database SQL

Posted on Wed, 04 Dec 2019 15:23:57 -0800 by ghettopixel