优化 FreeSql.Repository 使用方法

This commit is contained in:
28810
2019-02-28 20:38:26 +08:00
parent f034d4194d
commit 8e4d3c03a5
9 changed files with 74 additions and 13 deletions

View File

@ -1,6 +1,7 @@
using FreeSql;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Text;
public static class IFreeSqlExtenssions {
@ -12,10 +13,12 @@ public static class IFreeSqlExtenssions {
/// <typeparam name="TKey"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static IRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that) where TEntity : class {
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that) where TEntity : class {
return new DefaultRepository<TEntity, TKey>(that);
return dicGetRepository.GetOrAdd(typeof(TEntity), type1 => new ConcurrentDictionary<Type, IRepository>())
.GetOrAdd(typeof(TKey), type2 => new DefaultRepository<TEntity, TKey>(that)) as DefaultRepository<TEntity, TKey>;
}
static ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>> dicGetRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>>();
/// <summary>
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
@ -23,8 +26,9 @@ public static class IFreeSqlExtenssions {
/// <typeparam name="TEntity"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static IRepository<TEntity, Guid> GetGuidRepository<TEntity>(this IFreeSql that) where TEntity : class {
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that) where TEntity : class {
return new GuidRepository<TEntity>(that);
return dicGetGuidRepository.GetOrAdd(typeof(TEntity), type1 => new GuidRepository<TEntity>(that)) as GuidRepository<TEntity>;
}
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
}