从零开始搭建前后端分离的NetCore(EF Core CodeFirst+Au)+Vue的项目框架之二autofac解耦
在 上一篇 中将项目的基本骨架搭起来能正常跑通,这一篇将讲到,如何通过autofac将DbContext和model进行解耦,只用添加model,而不用在DbContext中添加DbSet。
在这里就不详细讲autofac是干什么用的了,简单说下autofac。
1.autofac可替换net core自带的DI IOC,用来扩展。
2.autofac可提供Aop,具体实现在博客园有很多示例。
3.autofac的几个生命周期用法:InstancePerDependency 每次都创建一个对象 ,SingleInstance 每次都是同一个对象,InstancePerLifetimeScope 同一个生命周期生成的对象是同一个。
接下来,我们需要在启动项目上通过nuget安装两个Package:Autofac
、Autofac.Extensions.DependencyInjection。
因为autofac是通过接口来进行注入的,因此我们需要创建对应的基层接口用来注入。在basic项目通过nuget安装Autofac.Extensions.DependencyInjection、,
然后中添加 Dependency 文件夹来存放基层接口,添加IOC容器接口:IIocManager,代码如下:
- using System;
- using Autofac;
- using Autofac.Core;
- namespace DemoFrame_Basic.Dependency
- {
- /// <summary>
- /// IOC容器接口
- /// </summary>
- public interface IIocManager
- {
- IContainer Container { get; }
- bool IsRegistered(Type serviceType, ILifetimeScope scope = null);
- object Resolve(Type type, ILifetimeScope scope = null);
- T Resolve<T>(string key = "", ILifetimeScope scope = null) where T : class;
- T Resolve<T>(params Parameter[] parameters) where T : class;
- T[] ResolveAll<T>(string key = "", ILifetimeScope scope = null);
- object ResolveOptional(Type serviceType, ILifetimeScope scope = null);
- object ResolveUnregistered(Type type, ILifetimeScope scope = null);
- T ResolveUnregistered<T>(ILifetimeScope scope = null) where T : class;
- ILifetimeScope Scope();
- bool TryResolve(Type serviceType, ILifetimeScope scope, out object instance);
- }
- }
-IOC容器接口
再添加一个数据库基础接口:IEntityBase
- /// <summary>
- /// 数据库基础接口
- /// </summary>
- public interface IEntityBase
- {
- }
-数据库基础接口
IIocManager的实现类:IocManager
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.Loader;
- using Autofac;
- using Autofac.Core;
- using Autofac.Core.Lifetime;
- using Autofac.Extensions.DependencyInjection;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.DependencyModel;
- namespace DemoFrame_Basic.Dependency
- {
- /// <summary>
- /// Container manager
- /// </summary>
- public class IocManager : IIocManager
- {
- private IContainer _container;
- public static IocManager Instance { get { return SingletonInstance; } }
- private static readonly IocManager SingletonInstance = new IocManager();
- /// <summary>
- /// Ioc容器初始化
- /// </summary>
- /// <param name="config"></param>
- /// <returns></returns>
- public IServiceProvider Initialize(IServiceCollection services)
- {
- //.InstancePerDependency() //每次都创建一个对象
- //.SingleInstance() //每次都是同一个对象
- //.InstancePerLifetimeScope() //同一个生命周期生成的对象是同一个
- var builder = new ContainerBuilder();
- builder.RegisterInstance(Instance).As<IIocManager>().SingleInstance();
- //所有程序集 和程序集下类型
- var deps = DependencyContext.Default;
- var libs = deps.CompileLibraries.Where(lib => !lib.Serviceable && lib.Type != "package");//排除所有的系统程序集、Nuget下载包
- var listAllType = new List<Type>();
- foreach (var lib in libs)
- {
- try
- {
- var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name));
- listAllType.AddRange(assembly.GetTypes().Where(type => type != null));
- }
- catch { }
- }
- //注册IEntityBase实现类
- var entityBaseType = typeof(IEntityBase);
- var arrEntityBaseType = listAllType.Where(t => entityBaseType.IsAssignableFrom(t) && t != entityBaseType).ToArray();
- builder.RegisterTypes(arrEntityBaseType)
- .AsImplementedInterfaces()
- .SingleInstance()
- .PropertiesAutowired();
- foreach (var type in arrEntityBaseType)
- {
- if (type.IsClass && !type.IsAbstract && !type.BaseType.IsInterface && type.BaseType != typeof(object))
- {
- builder.RegisterType(type).As(type.BaseType)
- .SingleInstance()
- .PropertiesAutowired();
- }
- }
- //注册controller实现类 让Controller能被找到
- var controller = typeof(ControllerBase);
- var arrcontrollerType = listAllType.Where(t => controller.IsAssignableFrom(t) && t != controller).ToArray();
- builder.RegisterTypes(arrcontrollerType)
- .AsImplementedInterfaces()
- .SingleInstance()
- .PropertiesAutowired();
- foreach (var type in arrcontrollerType)
- {
- if (type.IsClass && !type.IsAbstract && !type.BaseType.IsInterface && type.BaseType != typeof(object))
- {
- builder.RegisterType(type).AsSelf();
- }
- }
- builder.Populate(services);
- _container = builder.Build();
- return new AutofacServiceProvider(_container);
- }
- /// <summary>
- /// Gets a container
- /// </summary>
- public virtual IContainer Container
- {
- get
- {
- return _container;
- }
- }
- /// <summary>
- /// Resolve
- /// </summary>
- /// <typeparam name="T">Type</typeparam>
- /// <param name="key">key</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Resolved service</returns>
- public virtual T Resolve<T>(string key = "", ILifetimeScope scope = null) where T : class
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope();
- }
- if (string.IsNullOrEmpty(key))
- {
- return scope.Resolve<T>();
- }
- return scope.ResolveKeyed<T>(key);
- }
- /// <summary>
- /// Resolve
- /// </summary>
- /// <typeparam name="T">Type</typeparam>
- /// <param name="key">key</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Resolved service</returns>
- public virtual T Resolve<T>(params Parameter[] parameters) where T : class
- {
- var scope = Scope();
- return scope.Resolve<T>(parameters);
- }
- /// <summary>
- /// Resolve
- /// </summary>
- /// <param name="type">Type</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Resolved service</returns>
- public virtual object Resolve(Type type, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope();
- }
- return scope.Resolve(type);
- }
- /// <summary>
- /// Resolve all
- /// </summary>
- /// <typeparam name="T">Type</typeparam>
- /// <param name="key">key</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Resolved services</returns>
- public virtual T[] ResolveAll<T>(string key = "", ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope();
- }
- if (string.IsNullOrEmpty(key))
- {
- return scope.Resolve<IEnumerable<T>>().ToArray();
- }
- return scope.ResolveKeyed<IEnumerable<T>>(key).ToArray();
- }
- /// <summary>
- /// Resolve unregistered service
- /// </summary>
- /// <typeparam name="T">Type</typeparam>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Resolved service</returns>
- public virtual T ResolveUnregistered<T>(ILifetimeScope scope = null) where T : class
- {
- return ResolveUnregistered(typeof(T), scope) as T;
- }
- /// <summary>
- /// Resolve unregistered service
- /// </summary>
- /// <param name="type">Type</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Resolved service</returns>
- public virtual object ResolveUnregistered(Type type, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope();
- }
- var constructors = type.GetConstructors();
- foreach (var constructor in constructors)
- {
- try
- {
- var parameters = constructor.GetParameters();
- var parameterInstances = new List<object>();
- foreach (var parameter in parameters)
- {
- var service = Resolve(parameter.ParameterType, scope);
- if (service == null) throw new Exception("Unknown dependency");
- parameterInstances.Add(service);
- }
- return Activator.CreateInstance(type, parameterInstances.ToArray());
- }
- catch (Exception)
- {
- }
- }
- throw new Exception("No constructor was found that had all the dependencies satisfied.");
- }
- /// <summary>
- /// Try to resolve srevice
- /// </summary>
- /// <param name="serviceType">Type</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <param name="instance">Resolved service</param>
- /// <returns>Value indicating whether service has been successfully resolved</returns>
- public virtual bool TryResolve(Type serviceType, ILifetimeScope scope, out object instance)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope();
- }
- return scope.TryResolve(serviceType, out instance);
- }
- /// <summary>
- /// Check whether some service is registered (can be resolved)
- /// </summary>
- /// <param name="serviceType">Type</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Result</returns>
- public virtual bool IsRegistered(Type serviceType, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope();
- }
- return scope.IsRegistered(serviceType);
- }
- /// <summary>
- /// Resolve optional
- /// </summary>
- /// <param name="serviceType">Type</param>
- /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
- /// <returns>Resolved service</returns>
- public virtual object ResolveOptional(Type serviceType, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope();
- }
- return scope.ResolveOptional(serviceType);
- }
- /// <summary>
- /// Get current scope
- /// </summary>
- /// <returns>Scope</returns>
- public virtual ILifetimeScope Scope()
- {
- try
- {
- //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
- return Container.BeginLifetimeScope();
- }
- catch (Exception)
- {
- //we can get an exception here if RequestLifetimeScope is already disposed
- //for example, requested in or after "Application_EndRequest" handler
- //but note that usually it should never happen
- //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
- return Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
- }
- }
- }
- }
-Container manager
在这里添加完以后,我们需要将自带的DI容器给替换成现在使用的autofac,
在启动项目的Startup文件中更改,最终代码如下:
- public IServiceProvider ConfigureServices(IServiceCollection services)
- {
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
- services.AddDbContext<DemoDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection")));
- return IocManager.Instance.Initialize(services);
- }
-ConfigureServices
为了方便使用,在CoreMvc项目中添加DemoWeb的类来存放一些系统数据:
- using DemoFrame_Basic.Dependency;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Caching.Memory;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Hosting;
- using System.Linq;
- namespace DemoFrame_CoreMvc
- {
- public class DemoWeb
- {
- private static IHttpContextAccessor _httpContextAccessor;
- /// <summary>
- /// Configure
- /// </summary>
- /// <param name="httpContextAccessor"></param>
- public static void Configure(IHttpContextAccessor httpContextAccessor)
- {
- _httpContextAccessor = httpContextAccessor;
- }
- /// <summary>
- /// 当前请求HttpContext
- /// </summary>
- public static HttpContext HttpContext
- {
- get => _httpContextAccessor.HttpContext;
- set => _httpContextAccessor.HttpContext = value;
- }
- /// <summary>
- /// IocManager
- /// </summary>
- public static IIocManager IocManager { get; set; }
- /// <summary>
- /// Environment
- /// </summary>
- public static IHostingEnvironment Environment { get; set; }
- /// <summary>
- /// Configuration
- /// </summary>
- public static IConfiguration Configuration { get; set; }
- /// <summary>
- /// MemoryCache
- /// </summary>
- public static IMemoryCache MemoryCache { get; set; }
- /// <summary>
- /// 获取当前请求客户端IP
- /// </summary>
- /// <returns></returns>
- public static string GetClientIp()
- {
- var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault()?.Split(',')[].Trim();
- if (string.IsNullOrEmpty(ip))
- {
- ip = HttpContext.Connection.RemoteIpAddress.ToString();
- }
- return ip;
- }
- }
- }
Startup的完整代码如下:
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- DemoWeb.Configuration = configuration;
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public IServiceProvider ConfigureServices(IServiceCollection services)
- {
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
- services.AddDbContext<DemoDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection")));
- return IocManager.Instance.Initialize(services);
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- DemoWeb.IocManager = app.ApplicationServices.GetService<IIocManager>();
- DemoWeb.Environment = env;
- try//注意这里在本地开发允许时会重置数据库,并清空所有数据,如不需要请注释
- {
- if (env.IsDevelopment())
- {
- using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
- .CreateScope())
- {
- var dbContent = serviceScope.ServiceProvider.GetService<DemoDbContext>();
- //CheckMigrations(dbContent);
- var database = serviceScope.ServiceProvider.GetService<DemoDbContext>().Database;
- database.EnsureDeleted();
- database.EnsureCreated();
- }
- }
- }
- catch (Exception ex)
- {
- //LogHelper.Logger.Error(ex, "Failed to migrate or seed database");
- }
- DemoWeb.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseHsts();
- }
- app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());//允许跨域
- app.UseHttpsRedirection();
- app.UseMvc();
- }
- }
-Startup
在这么多的配置都完成了的情况下,我们该去实现mode与DbContext的解耦操作了。那么该如何做呢?
废话不多说了,直接上代码,在数据库上下文DemoDbContext中将之前的DbSet删掉,更改如下:
!!!先将之前的model都继承 IEntityBase 接口。这样在模型生成时才能生成到数据库!!!
- /// <summary>
- /// 数据库上下文
- /// </summary>
- public class DemoDbContext : DbContext
- {
- public DemoDbContext(DbContextOptions<DemoDbContext> options)
- : base(options)
- { }
- #region IOC得到所有实体
- private readonly IEntityBase[] _entitys = DemoWeb.IocManager.ResolveAll<IEntityBase>();
- #endregion
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- if (_entitys == null)
- {
- return;
- }
- foreach (var entity in _entitys)
- {
- modelBuilder.Model.AddEntityType(entity.GetType());
- }
- }
- }
-数据库上下文
在使用数据库上下文是,使用Set<T>方法设置需要使用的的类
在下一篇中将介绍如何使用基类controller来统一前后端交互数据,统一使用一个模型进行返回。
有需要源码的可通过此 GitHub链接拉取 觉得还可以的给个 start 哦,谢谢!
从零开始搭建前后端分离的NetCore(EF Core CodeFirst+Au)+Vue的项目框架之二autofac解耦的更多相关文章
- 【手摸手,带你搭建前后端分离商城系统】02 VUE-CLI 脚手架生成基本项目,axios配置请求、解决跨域问题
[手摸手,带你搭建前后端分离商城系统]02 VUE-CLI 脚手架生成基本项目,axios配置请求.解决跨域问题. 回顾一下上一节我们学习到的内容.已经将一个 usm_admin 后台用户 表的基本增 ...
- Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之二 || 后端项目搭建
本文梯子 前言 1..net core 框架性能测试 2..net core 执行过程 3.中间件执行过程 4.AOP切面 5.整体框架结构与数据库表UML 一.创建第一个Core 1.SDK 安装 ...
- 从零开始搭建前后端分离的NetCore2.2(EF Core CodeFirst+Autofac)+Vue的项目框架之四Nlog记录日志至数据库
为什么要进行日志记录呢?为什么要存至数据库呢?只能说日志记录是每个系统都应当有的. 好的日志记录方式可以提供我们足够多定位问题的依据.查找系统或软件或项目的错误或异常记录.程序在运行时就像一个机器人, ...
- 利用grunt-contrib-connect和grunt-connect-proxy搭建前后端分离的开发环境
前后端分离这个词一点都不新鲜,完全的前后端分离在岗位协作方面,前端不写任何后台,后台不写任何页面,双方通过接口传递数据完成软件的各个功能实现.此种情况下,前后端的项目都独立开发和独立部署,在开发期间有 ...
- List多个字段标识过滤 IIS发布.net core mvc web站点 ASP.NET Core 实战:构建带有版本控制的 API 接口 ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目 Using AutoFac
List多个字段标识过滤 class Program{ public static void Main(string[] args) { List<T> list = new List& ...
- 【手摸手,带你搭建前后端分离商城系统】01 搭建基本代码框架、生成一个基本API
[手摸手,带你搭建前后端分离商城系统]01 搭建基本代码框架.生成一个基本API 通过本教程的学习,将带你从零搭建一个商城系统. 当然,这个商城涵盖了很多流行的知识点和技术核心 我可以学习到什么? S ...
- 【手摸手,带你搭建前后端分离商城系统】03 整合Spring Security token 实现方案,完成主业务登录
[手摸手,带你搭建前后端分离商城系统]03 整合Spring Security token 实现方案,完成主业务登录 上节里面,我们已经将基本的前端 VUE + Element UI 整合到了一起.并 ...
- Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之七 || API项目整体搭建 6.2 轻量级ORM
本文梯子 本文3.0版本文章 前言 零.今天完成的蓝色部分 0.创建实体模型与数据库 1.实体模型 2.创建数据库 一.在 IRepository 层设计接口 二.在 Repository 层实现相应 ...
- Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之六 || API项目整体搭建 6.1 仓储+服务+抽象接口模式
本文梯子 本文3.0版本文章 前言 零.完成图中的粉色部分 2019-08-30:关于仓储的相关话题 一.创建实体Model数据层 二.设计仓储接口与其实现类 三.设计服务接口与其实现类 四.创建 C ...
随机推荐
- R035---偷个懒,用UiPath录制电脑操作过程,迅速实现流程自动化
一.缘起 UiPath可以录制你操作电脑的过程,从而实现自动化. 目前有点鸡肋,因为有些操作过程无法录制,例如: 键盘快捷键 修改键 右键点击 鼠标悬停 即便如此,录制功能有时候还是可以用一下,特别 ...
- go 格式化 int,位数不够0补齐
n := 32 sInt := fmt.Sprintf("%07d", n)
- Excel催化剂开源第46波-按行列排列多个图形技术要点
此篇对应功能出自:第10波-快速排列工作表图形对象 - 简书 https://www.jianshu.com/p/eab71f2969a6 在Excel的对象模型中,列的宽度不是一般所期待的和行高一样 ...
- [PTA] 数据结构与算法题目集 6-7 在一个数组中实现两个堆栈
//如果堆栈已满,Push函数必须输出"Stack Full"并且返回false:如果某堆栈是空的,则Pop函数必须输出"Stack Tag Empty"(其中 ...
- MySql(Windows)
百度云:链接:http://pan.baidu.com/s/1nvlSzMh 密码:o1cw 官网下载网址:http://dev.mysql.com/downloads/mysql/
- CentOS 下配置JDK
从官网上下载jdk到系统中,并解压好 tar –axvf jdk.tr.gz 1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找 ...
- 基于 HTML5 Canvas 的可交互旋钮组件
前言 此次的 Demo 效果如下: Demo 链接:https://hightopo.com/demo/comp-knob/ 整体思路 组件参数 绘制旋钮 绘制刻度 绘制指针 绘制标尺 绘制文本 1. ...
- 基于SpringBoot从零构建博客网站 - 新增创建、修改、删除专栏功能
守望博客是支持创建专栏的功能,即可以将一系列相关的文章归档到专栏中,方便用户管理和查阅文章.这里主要讲解专栏的创建.修改和删除功能,至于专栏还涉及其它的功能,例如关注专栏等后续会穿插着介绍. 1.创建 ...
- webgl图库研究(包括BabylonJS、Threejs、LayaboxJS、SceneJS、ThingJS等框架的特性、适用范围、支持格式、优缺点、相关网址)
3D图库框架范围与示例 摘要: 为实现企业80%以上的生产数据进行智能转化,在烟草.造纸.能源.电力.机床.化肥等行业,赢得领袖企业青睐,助力企业构建AI赋能中心,实现智能化转型升级.“远舢文龙数据处 ...
- 移动端开发用touch事件还是click事件
前端开发现在包含了跨浏览器,跨平台(不同操作系统)和跨设备(不同尺寸的设备)开发. 在移动开发的过程中,到底选取touch事件还是click事件?对了,请不要鄙视click,click在移动端开发用着 ...