关于SqlSugarCore 和我项目架构产生冲突的问题结点,希望加入更多参数识别缓存版本 返回
PS:我就不从源码上下手修改了哈。
问题描述: 我的代码是支持多版本模块加载的,所以我的每个模块可能都有同一个Model 或版本不同 ,但对于不同版本的Model 情况下,SqlSugar 的typeCache 缓存了之前的程序集类型,那么另外一个模块在进行使用时将发生冲突,CreateInterface 失败。
代码发生错误时提示的信息:
System.InvalidCastException: Unable to cast object of type 'SqlSugar.MySqlQueryable`1[Jusoft.Glob.Model.Aa_company_basic]' to type 'SqlSugar.ISugarQueryable`1[Jusoft.Glob.Model.Aa_company_basic]'. at SqlSugar.InstanceFactory.CreateInstance[Restult](String className, Type[] types) at SqlSugar.InstanceFactory.CreateInstance[T,Restult](String className) at SqlSugar.InstanceFactory.GetQueryable[T](ConnectionConfig currentConnectionConfig) at SqlSugar.SqlSugarProvider.CreateQueryable[T]() at SqlSugar.SqlSugarProvider.Queryable[T]() at SqlSugar.SqlSugarClient.Queryable[T]() at Jusoft.AppBase.Daos.QueryDao`1.get_Queryable() in E:\yinai\Documents\Visual Studio 2019\ProjectCore\Jusoft通用基础框架Core版\Jusoft.AppBase\Daos\QueryDao.cs:line 36 at Jusoft.AppBase.Daos.QueryDao`1.QueryFirstOrDefaultAsync(Expression`1 whereExpression) in E:\yinai\Documents\Visual Studio 2019\ProjectCore\Jusoft通用基础框架Core版\Jusoft.AppBase\Daos\QueryDao.cs:line 49 at Jusoft.Test1.WEBAPI.Controllers.MTestController.GetById()
GitHub:https://github.com/sunkaixuan/SqlSugar/blob/bb842a6dd2f7c17365237c90fc76dc424c304e60/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/InstanceFactory.cs#L12
文件:SqlSugar/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/InstanceFactory.cs
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Reflection; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace SqlSugar | |
| { | |
| public class InstanceFactory | |
| { | |
| static Assembly assembly = Assembly.Load(UtilConstants.AssemblyName); | |
| static Dictionary<string, Type> typeCache = new Dictionary<string, Type>(); | |
| public static void RemoveCache() | |
| { | |
| typeCache = new Dictionary<string, Type>(); | |
| } | |
热忱回答(9)
-
fate stay night VIP0
2020/11/18你有比较好的代码提供吗?我这边没测试代码
0 回复 -
疋疋 VIP0
2020/11/19@fate stay night:我需要整理一下代码才行,需要一些时间。整个架构有点大
0 回复 -
疋疋 VIP0
2020/11/19@疋疋:另外原理上我是利用AssemblyLoadContext进行模块热加载卸载以及每个模块下所引用的dll 都是相对于模块独立的,A/B模块有公有的一个Model.dll,但是由于A/B模块开发时间是不同的,所以针对于Model.dll 版本上可能有差异,利用AssemblyLoadContext可以将A模块只使用Model.dll(v1.0.0),B模块只使用Model.dll(v1.3.0)。而且两个Model.dll 也都加载到程序集中了。
我的建议是:将 字典集的Key 修改为一个class { typeName , version },以便于区分版本:
calss TypeModel{ string typeName,Version version }
static Dictionary<TypeModel, Type> typeCache = new Dictionary<TypeModel, Type>();
另外有个建议,对于多线程的环境,使用 ConcurrentDictionary会不会比 Dictionary上更好的处理并发的问题,这种情况下貌似可以省略Lock 的操作。可以研究看看
0 回复 -
fate stay night VIP0
2020/11/19@疋疋:ConcurrentDictionary 性能更差这方面我是测试过的,代码中也用到了ConcurrentDictionary 存储比较大的数量
0 回复 -
fate stay night VIP0
2020/11/19@疋疋:如何改最好你这边提供给我改,我会尽量把你的代码设计的和其它人的代码分离开,这样后期双方都可以升级,因为我这边没你真实的环境去测
0 回复 -
fate stay night VIP0
2020/11/19你可以加我QQ沟通
0 回复 -
疋疋 VIP0
2020/11/19@fate stay night:明白,等我整理好提供出来看看
0 回复 -
疋疋 VIP0
2020/11/23@fate stay night:我已经整理出来了重现那个问题的sln,你看我们怎么联系把那个示例代码给到你那边?
0 回复 -
fate stay night VIP0
2020/11/23@疋疋:qq 610262374
0 回复