首页 > 其他分享 >.NET 文件系统(一)-- EfCore的使用,反射生成DbSet与Autofac的使用

.NET 文件系统(一)-- EfCore的使用,反射生成DbSet与Autofac的使用

时间:2022-08-30 13:47:12浏览次数:55  
标签:Autofac set get -- FileDownLoadSystem System EfCore using public

项目基础设计

1.创建FileDownLoadSystem 空解决方案的项目
2.创建FileDownLoadSystem.API API项目
3.创建FileDownLoadSystem.Core 类库
4.创建FileDownLoadSystem.Entity 类库
5.创建FileDownLoadSystem.System 类库

FileDownLoadSystem.Entity 实体

1.创建基础模型

在FileDownLoadSystem.Entity 类库创建一个类:BaseModel

点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileDownLoadSystem.Entity
{
	public class BaseModel
	{
		public int Id { get; set; }
	}
}

2.创建DataModel

在FileDownLoadSystem.Entity 类库创建一个类:DataModel

点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileDownLoadSystem.Entity
{
	public class DataModel
	{
		/// <summary>
		/// 分页通用参数
		/// </summary>
		public class PageDataOptions
		{
			public int Page { get; set; }
			public int Rows { get; set; }
			public int Total { get; set; }
			public string TableName { get; set; }
			/// <summary>
			/// 排序字段
			/// </summary>
			/// <value></value>
			public string Sort { get; set; }
			/// <summary>
			/// 排序方式
			/// </summary>
			/// <value></value>
			public string Order { get; set; }
			public string Wheres { get; set; }
			public object Value { get; set; }

		}
		/// <summary>
		/// 查询通用参数
		/// </summary>
		public class SearchParameters
		{
			public string Name { get; set; }
			public string Value { get; set; }
			public string DisplayType { get; set; }
		}
	}
}

3.创建数据库映射模型

在FileDownLoadSystem.Entity 创建一个名称为FileInfo文件夹
在这个文件夹下创建一个Files类(为避免和.NET 本身的File文件操作类产生二义性,所以加了s)

Files
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileDownLoadSystem.Entity.FileInfo
{
	public class Files : BaseModel
	{
		/// <summary>
		/// 文件类型id
		/// </summary>
		/// <value></value>
		public int FileTypeId { get; set; }
		/// <summary>
		/// 文件名称
		/// </summary>
		/// <value></value>
		public string FileName { get; set; }
		/// <summary>
		/// 文件图标url
		/// </summary>
		/// <value></value>
		public string? FileIconUrl { get; set; }
		/// <summary>
		/// 发布时间
		/// </summary>
		/// <value></value>
		public DateTime PublishDate { get; set; }
		/// <summary>
		/// 点击时间
		/// </summary>
		/// <value></value>
		public long ClickTimes { get; set; }
		/// <summary>
		/// 下载时间
		/// </summary>
		/// <value></value>
		public long DownloadTimes { get; set; }
		/// <summary>
		/// 文件说明
		/// </summary>
		/// <value></value>
		public string Notification { get; set; }
	}
}

4.在FileInfo文件夹下创建FileTypes实体类

FileTypes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileDownLoadSystem.Entity.FileInfo
{
	/// <summary>
	/// 文件类型实体
	/// </summary>
	public class FileTypes : BaseModel
	{
		/// <summary>
		/// 文件类型名称
		/// </summary>
		/// <value></value>
		public string TypeName { get; set; }
	}
}

5.在FileInfo文件夹下创建一个FilePackages类

点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileDownLoadSystem.Entity.FileInfo
{
	/// <summary>
	/// 文件包实体
	/// </summary>
	public class FilePackages : BaseModel
	{
		/// <summary>
		/// 文件id
		/// </summary>
		/// <value></value>
		public int FileId { get; set; }
		/// <summary>
		/// 文件包下载地址
		/// </summary>
		/// <value></value>
		public string PackageUrl { get; set; }
		/// <summary>
		/// 文件包名称
		/// </summary>
		/// <value></value>
		public string PackageName { get; set; }
		/// <summary>
		/// 发布时间
		/// </summary>
		/// <value></value>
		public DateTime PublishTime { get; set; }
	}
}

6.在FileInfo下创建一个FileWebConfigs 类

FileWebConfigs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileDownLoadSystem.Entity.FileInfo
{
	/// <summary>
	/// 文件系统web端配置
	/// </summary>
	public class FileWebConfigs : BaseModel
	{
		/// <summary>
		/// 内容
		/// </summary>
		/// <value></value>
		public string Content { get; set; }
		/// <summary>
		/// 发布时间
		/// </summary>
		/// <value></value>
		public DateTime PublishDate { get; set; }
	}
}

FileDownLoadSystem.Core 如下:

需要安装的包文件

因为用的是EFCore框架,所以安装的包是EFCore的包
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Pomelo.EntityFrameworkCore.MySql

创建一个类FileDownloadSystemDbContext

先创建一个EfDbContext文件夹
在这个文件夹下创建FileDownloadSystemDbContext类

FileDownloadSystemDbContext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading.Tasks;
using FileDownLoadSystem.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyModel;

namespace FileDownLoadSystem.Core.EfDbContext
{
	/// <summary>
	/// 文件系统数据库上下文
	/// </summary>
	public class FileDownloadSystemDbContext : DbContext
	{
		public FileDownloadSystemDbContext()
		{

		}
		public FileDownloadSystemDbContext(DbContextOptions options) : base(options)
		{

		}
		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			if (!optionsBuilder.IsConfigured)
			{
				//提交EFCore性能
				optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
				optionsBuilder.UseMySql("Server=102.89.8.23;port=3306;database=FileDownloadSystem;uid=root;pwd=123456", MySqlServerVersion.LatestSupportedServerVersion);
			}
			base.OnConfiguring(optionsBuilder);
		}
		/// <summary>
		/// 完成自动写DBset的功能
		/// </summary>
		/// <param name="modelBuilder"></param>
		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			try
			{
				//获取所有的类库
				//DependencyContext 依赖Microsoft.Extensions.DependencyModel包
				var compilationLibrary = DependencyContext.Default.CompileLibraries
														  .Where(x => !x.Serviceable && x.Type != "package" && x.Type == "project");
				//获取所有数据库模型
				foreach (var _compilation in compilationLibrary)
				{
					//加载指定类型
					AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(_compilation.Name))
									   .GetTypes()
									   .Where(s => s.BaseType != null
									   			&& !s.IsAbstract
												&& s.BaseType == typeof(BaseModel))
									   .ToList()
									   .ForEach(t =>
									   {
										   //将实体转换为DbSet
										   modelBuilder.Entity(t);
									   });
				}
			}
			catch (System.Exception ex)
			{

				throw;
			}
			base.OnModelCreating(modelBuilder);
		}
	}
}

之前没学习过的内容,通过程序集,反射读取DbSet,不需要人为的去写死DbSet了

我用的是vscode,点击Core的项目,点击在集成终端中打开,输入如下命令:
dotnet ef migrations add init 初始化数据库迁移文件
dotnet ef database update 生成数据表,如果数据库没生成,也声称数据库,将迁移文件写到数据库中
接下来,你看你的数据库就发现已经生成了数据库及数据表

添加Autofac的包

  • Autofac
  • Autofac.Extensions.Dependencylnjection

Autofac的使用

在 FileDownLoadSystem.Core 项目下新增一个文件夹Extensions,
在这个文件夹中新增一个接口IDependency
让 FileDownloadSystemDbContext 数据库上下文类继承于这个接口
在Extensions文件夹下新增一个类:AutofacServiceCollectionExtensions
在program中使用autofac的方式:

点击查看代码
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder =>
{
	//添加扩展类
	builder.Services.AddModule(containerBuilder, configuration);
});

添加扩展类实现:

在Core项目下新建一个文件夹:Configuration
在这个文件夹下创建一个类

Connection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileDownLoadSystem.Core.Configuration
{
	/// <summary>
	/// 连接字符串
	/// </summary>
	public class Connection
	{
		public string DBType { get; set; }
		public string DbConnectionString { get; set; }
		public string RedisConnectionString { get; set; }
		public string UseRedis { get; set; }
	}
}

AppSetting代码如下:

在Configuration文件夹下新增AppSetting类,用于初始化配置文件

AppSetting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace FileDownLoadSystem.Core.Configuration
{
	public class AppSetting
	{
		public static Dictionary<string, string> Connections { get; set; }
		//获取Connection对象
		private static Connection _connection;
		public static void Init(IServiceCollection services, IConfiguration configuration)
		{
			//Microsoft.Extensions.Configuration.Binder
			_connection = configuration.GetSection("Connection").Get<Connection>();
		}
	}
}

标签:Autofac,set,get,--,FileDownLoadSystem,System,EfCore,using,public
From: https://www.cnblogs.com/rookiewang/p/16635696.html

相关文章

  • 使用java处理字符串公式运算的方法
    在改进一个关于合同的项目时,有个需求,就是由于合同中非数据项的计算公式会根据年份而进行变更,而之前是将公式硬编码到系统中的,只要时间一变,系统就没法使用了,因此要求合同中......
  • hbase
    hbase和hadoophttp://c.biancheng.net/view/6501.htmlhbase架构文档https://blog.csdn.net/u012485099/article/details/110941341一个regionserver管理多个region,......
  • hadoop
    YARNhttps://baijiahao.baidu.com/s?id=1697462995678594984&wfr=spider&for=pcResourceManager:资源分配调度NodeManager:一个机器node上的管理ApplicationMaster:一个......
  • docker swarm集群中,task是什么意思?
    你在查看dockerswarm文档的时候,是不是经常听说task这个词,是什么意思呢? 非常,非常的简单: 在dockerswarm中,service中的容器,就叫做task. Container=task ......
  • ThreadPoolTaskExecutor和ThreadPoolExecutor区别
    一、ThreadPoolExecutorThreadPoolExecutor是JDK中的线程池类,实现了Executor接口。顾名思义,Executor是一个专门用来处理多线程工作的接口,所有多线程处理相关的类都实现了......
  • java类加载过程
    https://blog.csdn.net/weixin_37766296/article/details/80545283 https://www.cnblogs.com/wangwudi/p/12327942.html 类的加载顺序ClassLoader中默认的加载顺序......
  • 性能优化-日志篇
    首先自定义项目的Log系统usingUnityEngine;namespaceDAO{publicstaticclassLogger{publicstaticvoidLog(objectobj){......
  • Spring使用ThreadPoolTaskExecutor自定义线程池及实现异步调用
    一、ThreadPoolTaskExecutor本文采用Executors的工厂方法进行配置。1、将线程池用到的参数定义到配置文件中在项目的resources目录下创建executor.properties文件......
  • element图片预览el-image-viewer
    el-image-viewer竟然可以单独使用,真不错1、引入el-image-viewer组件importElImageViewerfrom'element-ui/packages/image/src/image-viewer'components:{ElImageVie......
  • 什么情况下会用到final?
     https://blog.csdn.net/qq_46657442/article/details/121360785 ①final作用final修饰类:表示该类不能被继承,是顶级类。修饰方法  :表示不能重写。修饰变量 :表......