From 0eee50177ccd765acd70e02a9e292e9dda1bfccc Mon Sep 17 00:00:00 2001 From: "zan.dai" Date: Fri, 14 May 2021 17:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ef=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LBON.Consts/LBON.Consts.csproj | 2 +- .../DependencyInjection/IScopedDependency.cs | 6 + .../ISingletonDependency.cs | 6 + .../ITransientDependency.cs | 6 + .../DependencyInjectionExtension.cs | 61 ++++ .../LBON.DependencyInjection.csproj | 38 +++ .../Entities/EntityBase.cs | 7 + .../Entities/FullAuditedEntity.cs | 15 + .../Entities/ICreationAudited.cs | 18 ++ .../Entities/IDeletionAudited.cs | 23 ++ LBON.EntityFrameworkCore/Entities/IEntity.cs | 7 + .../Entities/IExtendableObject.cs | 16 + .../Entities/IModificationAudited.cs | 18 ++ .../Extensions/ExtendableObjectExtensions.cs | 148 +++++++++ .../LBON.EntityFrameworkCore.csproj | 40 +++ .../Repositories/EfRepository.cs | 295 ++++++++++++++++++ .../Repositories/IEfRepository.cs | 61 ++++ LBON.Extensions/LBON.Extensions.csproj | 13 +- LBON.Helper/LBON.Helper.csproj | 2 +- LBON.Helper/TypeHelper.cs | 64 ++++ LBON.sln | 14 +- 21 files changed, 852 insertions(+), 8 deletions(-) create mode 100644 LBON.DependencyInjection/DependencyInjection/IScopedDependency.cs create mode 100644 LBON.DependencyInjection/DependencyInjection/ISingletonDependency.cs create mode 100644 LBON.DependencyInjection/DependencyInjection/ITransientDependency.cs create mode 100644 LBON.DependencyInjection/DependencyInjectionExtension.cs create mode 100644 LBON.DependencyInjection/LBON.DependencyInjection.csproj create mode 100644 LBON.EntityFrameworkCore/Entities/EntityBase.cs create mode 100644 LBON.EntityFrameworkCore/Entities/FullAuditedEntity.cs create mode 100644 LBON.EntityFrameworkCore/Entities/ICreationAudited.cs create mode 100644 LBON.EntityFrameworkCore/Entities/IDeletionAudited.cs create mode 100644 LBON.EntityFrameworkCore/Entities/IEntity.cs create mode 100644 LBON.EntityFrameworkCore/Entities/IExtendableObject.cs create mode 100644 LBON.EntityFrameworkCore/Entities/IModificationAudited.cs create mode 100644 LBON.EntityFrameworkCore/Extensions/ExtendableObjectExtensions.cs create mode 100644 LBON.EntityFrameworkCore/LBON.EntityFrameworkCore.csproj create mode 100644 LBON.EntityFrameworkCore/Repositories/EfRepository.cs create mode 100644 LBON.EntityFrameworkCore/Repositories/IEfRepository.cs create mode 100644 LBON.Helper/TypeHelper.cs diff --git a/LBON.Consts/LBON.Consts.csproj b/LBON.Consts/LBON.Consts.csproj index 1e284f0..5d14fa7 100644 --- a/LBON.Consts/LBON.Consts.csproj +++ b/LBON.Consts/LBON.Consts.csproj @@ -5,7 +5,7 @@ Lu Ban Of .Net - .Net 高可用、高效率的扩展组件 0.0.1 false - 2.0.1.3 + 2.0.1.4 CacoCode LICENSE https://github.com/CacoCode/LBON diff --git a/LBON.DependencyInjection/DependencyInjection/IScopedDependency.cs b/LBON.DependencyInjection/DependencyInjection/IScopedDependency.cs new file mode 100644 index 0000000..4d7fee3 --- /dev/null +++ b/LBON.DependencyInjection/DependencyInjection/IScopedDependency.cs @@ -0,0 +1,6 @@ +namespace LBON.DependencyInjection.DependencyInjection +{ + public interface IScopedDependency + { + } +} diff --git a/LBON.DependencyInjection/DependencyInjection/ISingletonDependency.cs b/LBON.DependencyInjection/DependencyInjection/ISingletonDependency.cs new file mode 100644 index 0000000..2524785 --- /dev/null +++ b/LBON.DependencyInjection/DependencyInjection/ISingletonDependency.cs @@ -0,0 +1,6 @@ +namespace LBON.DependencyInjection.DependencyInjection +{ + public interface ISingletonDependency + { + } +} diff --git a/LBON.DependencyInjection/DependencyInjection/ITransientDependency.cs b/LBON.DependencyInjection/DependencyInjection/ITransientDependency.cs new file mode 100644 index 0000000..f253414 --- /dev/null +++ b/LBON.DependencyInjection/DependencyInjection/ITransientDependency.cs @@ -0,0 +1,6 @@ +namespace LBON.DependencyInjection.DependencyInjection +{ + public interface ITransientDependency + { + } +} diff --git a/LBON.DependencyInjection/DependencyInjectionExtension.cs b/LBON.DependencyInjection/DependencyInjectionExtension.cs new file mode 100644 index 0000000..27e08b0 --- /dev/null +++ b/LBON.DependencyInjection/DependencyInjectionExtension.cs @@ -0,0 +1,61 @@ +using System.Linq; +using System.Reflection; +using LBON.DependencyInjection.DependencyInjection; +using LBON.EntityFrameworkCore.Repositories; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class DependencyInjectionExtension + { + public static void ServiceRegister(this IServiceCollection services, params Assembly[] assemblies) + { + services.AddScoped(typeof(IEfRepository<,,>), typeof(EfRepository<,,>)); + if (assemblies.Length <= 0) + { + assemblies = new[] { Assembly.GetExecutingAssembly() }; + } + + foreach (var assembly in assemblies) + { + var allTypes = assembly.GetTypes(); + foreach (var type in allTypes) + { + if (typeof(ITransientDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) + { + + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency")); + foreach (var interfaceType in interfaceTypes) + { + services.AddTransient(interfaceType, type); + } + } + } + + foreach (var type in allTypes) + { + if (typeof(ISingletonDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) + { + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency")); + foreach (var interfaceType in interfaceTypes) + { + services.AddSingleton(interfaceType, type); + } + } + } + + foreach (var type in allTypes) + { + if (typeof(IScopedDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) + { + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency")); + foreach (var interfaceType in interfaceTypes) + { + services.AddScoped(interfaceType, type); + } + } + } + } + + } + } +} diff --git a/LBON.DependencyInjection/LBON.DependencyInjection.csproj b/LBON.DependencyInjection/LBON.DependencyInjection.csproj new file mode 100644 index 0000000..de50b7b --- /dev/null +++ b/LBON.DependencyInjection/LBON.DependencyInjection.csproj @@ -0,0 +1,38 @@ + + + + netstandard2.1 + Lu Ban Of .Net - .Net 高可用、高效率的扩展组件 + 2.0.1.4 + false + 2.0.1.4 + CacoCode + LICENSE + https://github.com/CacoCode/LBON + logo.png + https://github.com/CacoCode/LBON + LBON.DependencyInjection + CacoCode + CacoCode + + + + + + True + + + + True + + + + + + + + + + + + diff --git a/LBON.EntityFrameworkCore/Entities/EntityBase.cs b/LBON.EntityFrameworkCore/Entities/EntityBase.cs new file mode 100644 index 0000000..8305312 --- /dev/null +++ b/LBON.EntityFrameworkCore/Entities/EntityBase.cs @@ -0,0 +1,7 @@ +namespace LBON.EntityFrameworkCore.Entities +{ + public class EntityBase : IEntity + { + public TKey Id { get; set; } + } +} diff --git a/LBON.EntityFrameworkCore/Entities/FullAuditedEntity.cs b/LBON.EntityFrameworkCore/Entities/FullAuditedEntity.cs new file mode 100644 index 0000000..0a89b69 --- /dev/null +++ b/LBON.EntityFrameworkCore/Entities/FullAuditedEntity.cs @@ -0,0 +1,15 @@ +using System; + +namespace LBON.EntityFrameworkCore.Entities +{ + public class FullAuditedEntity:EntityBase, ICreationAudited, IModificationAudited, IDeletionAudited + { + public TUser CreatorId { get; set; } + public DateTime CreationTime { get; set; } + public TUser LastModifierId { get; set; } + public DateTime? LastModificationTime { get; set; } + public TUser DeleterId { get; set; } + public bool IsDeleted { get; set; } + public DateTime? DeletionTime { get; set; } + } +} diff --git a/LBON.EntityFrameworkCore/Entities/ICreationAudited.cs b/LBON.EntityFrameworkCore/Entities/ICreationAudited.cs new file mode 100644 index 0000000..22f27df --- /dev/null +++ b/LBON.EntityFrameworkCore/Entities/ICreationAudited.cs @@ -0,0 +1,18 @@ +using System; + +namespace LBON.EntityFrameworkCore.Entities +{ + public interface ICreationAudited : IHasCreator, IHasCreationTime + { + } + + public interface IHasCreator + { + TUser CreatorId { get; set; } + } + + public interface IHasCreationTime + { + DateTime CreationTime { get; set; } + } +} diff --git a/LBON.EntityFrameworkCore/Entities/IDeletionAudited.cs b/LBON.EntityFrameworkCore/Entities/IDeletionAudited.cs new file mode 100644 index 0000000..e0ae130 --- /dev/null +++ b/LBON.EntityFrameworkCore/Entities/IDeletionAudited.cs @@ -0,0 +1,23 @@ +using System; + +namespace LBON.EntityFrameworkCore.Entities +{ + public interface IDeletionAudited : IHasDeleter,ISoftDelete, IHasDeletionTime + { + } + + public interface ISoftDelete + { + bool IsDeleted { get; set; } + } + + public interface IHasDeleter + { + TUser DeleterId { get; set; } + } + + public interface IHasDeletionTime + { + DateTime? DeletionTime { get; set; } + } +} diff --git a/LBON.EntityFrameworkCore/Entities/IEntity.cs b/LBON.EntityFrameworkCore/Entities/IEntity.cs new file mode 100644 index 0000000..7846ecc --- /dev/null +++ b/LBON.EntityFrameworkCore/Entities/IEntity.cs @@ -0,0 +1,7 @@ +namespace LBON.EntityFrameworkCore.Entities +{ + public interface IEntity + { + TKey Id { get; } + } +} diff --git a/LBON.EntityFrameworkCore/Entities/IExtendableObject.cs b/LBON.EntityFrameworkCore/Entities/IExtendableObject.cs new file mode 100644 index 0000000..93ec8ad --- /dev/null +++ b/LBON.EntityFrameworkCore/Entities/IExtendableObject.cs @@ -0,0 +1,16 @@ +using JetBrains.Annotations; + +namespace LBON.EntityFrameworkCore.Entities +{ + public interface IExtendableObject + { + /// + /// Gets or sets the extension data. + /// + /// + /// The extension data. + /// + [CanBeNull] + string ExtensionData { get; set; } + } +} diff --git a/LBON.EntityFrameworkCore/Entities/IModificationAudited.cs b/LBON.EntityFrameworkCore/Entities/IModificationAudited.cs new file mode 100644 index 0000000..7d6eb04 --- /dev/null +++ b/LBON.EntityFrameworkCore/Entities/IModificationAudited.cs @@ -0,0 +1,18 @@ +using System; + +namespace LBON.EntityFrameworkCore.Entities +{ + public interface IModificationAudited : IHasLastModifier, IHasLastModificationTime + { + } + + public interface IHasLastModifier + { + TUser LastModifierId { get; set; } + } + + public interface IHasLastModificationTime + { + DateTime? LastModificationTime { get; set; } + } +} diff --git a/LBON.EntityFrameworkCore/Extensions/ExtendableObjectExtensions.cs b/LBON.EntityFrameworkCore/Extensions/ExtendableObjectExtensions.cs new file mode 100644 index 0000000..930a907 --- /dev/null +++ b/LBON.EntityFrameworkCore/Extensions/ExtendableObjectExtensions.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using JetBrains.Annotations; +using LBON.EntityFrameworkCore.Entities; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using LBON.Helper; + +namespace LBON.EntityFrameworkCore.Extensions +{ + public static class ExtendableObjectExtensions + { + public static T GetData([NotNull] this IExtendableObject extendableObject, [NotNull] string name, bool handleType = false) + { + return extendableObject.GetData( + name, + handleType + ? new JsonSerializer { TypeNameHandling = TypeNameHandling.All } + : JsonSerializer.CreateDefault() + ); + } + + public static T GetData([NotNull] this IExtendableObject extendableObject, [NotNull] string name, [CanBeNull] JsonSerializer jsonSerializer) + { + if (extendableObject.ExtensionData == null) + { + return default(T); + } + + var json = JObject.Parse(extendableObject.ExtensionData); + + var prop = json[name]; + if (prop == null) + { + return default(T); + } + + if (TypeHelper.IsPrimitiveExtendedIncludingNullable(typeof(T))) + { + return prop.Value(); + } + else + { + return (T)prop.ToObject(typeof(T), jsonSerializer ?? JsonSerializer.CreateDefault()); + } + } + + + + public static void SetData([NotNull] this IExtendableObject extendableObject, [NotNull] string name, [CanBeNull] T value, bool handleType = false) + { + extendableObject.SetData( + name, + value, + handleType + ? new JsonSerializer { TypeNameHandling = TypeNameHandling.All } + : JsonSerializer.CreateDefault() + ); + } + + public static void SetData([NotNull] this IExtendableObject extendableObject, [NotNull] string name, [CanBeNull] T value, [CanBeNull] JsonSerializer jsonSerializer) + { + if (extendableObject == null) + { + throw new ArgumentNullException(nameof(extendableObject)); + } + + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + if (jsonSerializer == null) + { + jsonSerializer = JsonSerializer.CreateDefault(); + } + + if (extendableObject.ExtensionData == null) + { + if (EqualityComparer.Default.Equals(value, default(T))) + { + return; + } + + extendableObject.ExtensionData = "{}"; + } + + var json = JObject.Parse(extendableObject.ExtensionData); + + if (value == null || EqualityComparer.Default.Equals(value, default(T))) + { + if (json[name] != null) + { + json.Remove(name); + } + } + else if (TypeHelper.IsPrimitiveExtendedIncludingNullable(value.GetType())) + { + json[name] = new JValue(value); + } + else + { + json[name] = JToken.FromObject(value, jsonSerializer); + } + + var data = json.ToString(Formatting.None); + if (data == "{}") + { + data = null; + } + + extendableObject.ExtensionData = data; + } + + public static bool RemoveData([NotNull] this IExtendableObject extendableObject, string name) + { + if (extendableObject == null) + { + throw new ArgumentNullException(nameof(extendableObject)); + } + + if (extendableObject.ExtensionData == null) + { + return false; + } + + var json = JObject.Parse(extendableObject.ExtensionData); + + var token = json[name]; + if (token == null) + { + return false; + } + + json.Remove(name); + + var data = json.ToString(Formatting.None); + if (data == "{}") + { + data = null; + } + + extendableObject.ExtensionData = data; + + return true; + } + } +} diff --git a/LBON.EntityFrameworkCore/LBON.EntityFrameworkCore.csproj b/LBON.EntityFrameworkCore/LBON.EntityFrameworkCore.csproj new file mode 100644 index 0000000..4bb4557 --- /dev/null +++ b/LBON.EntityFrameworkCore/LBON.EntityFrameworkCore.csproj @@ -0,0 +1,40 @@ + + + + netstandard2.1 + Lu Ban Of .Net - .Net 高可用、高效率的扩展组件 + 2.0.1.4 + false + 2.0.1.4 + CacoCode + LICENSE + https://github.com/CacoCode/LBON + logo.png + https://github.com/CacoCode/LBON + LBON.EntityFrameworkCore + CacoCode + CacoCode + + + + + + True + + + + True + + + + + + + + + + + + + + diff --git a/LBON.EntityFrameworkCore/Repositories/EfRepository.cs b/LBON.EntityFrameworkCore/Repositories/EfRepository.cs new file mode 100644 index 0000000..6eedcfe --- /dev/null +++ b/LBON.EntityFrameworkCore/Repositories/EfRepository.cs @@ -0,0 +1,295 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using LBON.EntityFrameworkCore.Entities; +using LBON.Extensions; +using Microsoft.EntityFrameworkCore; + +namespace LBON.EntityFrameworkCore.Repositories +{ + public class EfRepository : IEfRepository + where TEntity : class, IEntity + where TDbContext : DbContext + { + public virtual DbContext DbContext { private set; get; } + + public virtual DbSet Table => DbContext.Set(); + + public EfRepository(DbContext dbContext) + { + DbContext = dbContext; + } + + public virtual IQueryable GetAll(params Expression>[] propertySelectors) + { + var query = Table.AsNoTracking(); + if (!propertySelectors.IsNullOrEmpty()) + { + foreach (var propertySelector in propertySelectors) + { + query = query.Include(propertySelector); + } + } + return query; + } + + public virtual IQueryable GetAll(Expression> expression, params Expression>[] propertySelectors) + { + var query = Table.Where(expression); + if (!propertySelectors.IsNullOrEmpty()) + { + foreach (var propertySelector in propertySelectors) + { + query = query.Include(propertySelector); + } + } + return query; + } + + public virtual TEntity Find(TPrimaryKey id) + { + return Table.Find(id); + } + + public virtual async Task FindAsync(TPrimaryKey id) + { + return await Table.FindAsync(id); + } + + public virtual TEntity Get(Expression> expression, params Expression>[] propertySelectors) + { + var query = GetAll(); + if (!propertySelectors.IsNullOrEmpty()) + { + foreach (var propertySelector in propertySelectors) + { + query = query.Include(propertySelector); + } + return query.FirstOrDefault(expression); + } + return Table.FirstOrDefault(expression); + } + + public virtual async Task GetAsync(Expression> expression, params Expression>[] propertySelectors) + { + var query = GetAll(); + if (!propertySelectors.IsNullOrEmpty()) + { + foreach (var propertySelector in propertySelectors) + { + query = query.Include(propertySelector); + } + return await query.FirstOrDefaultAsync(expression); + } + return await Table.FirstOrDefaultAsync(expression); + } + + public virtual void Insert(TEntity entity, bool autoSave = true) + { + var hasCreationTime = typeof(IHasCreationTime).IsAssignableFrom(typeof(TEntity)); + if (hasCreationTime) + { + ((IHasCreationTime)entity).CreationTime = DateTime.Now; + } + ((IHasCreationTime)entity).CreationTime = DateTime.Now; + Table.Add(entity); + if (autoSave) + { + DbContext.SaveChanges(); + } + } + + public virtual async Task InsertAsync(TEntity entity, bool autoSave = true) + { + var hasCreationTime = typeof(IHasCreationTime).IsAssignableFrom(typeof(TEntity)); + if (hasCreationTime) + { + ((IHasCreationTime)entity).CreationTime = DateTime.Now; + } + await Table.AddAsync(entity); + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + } + + public virtual async Task InsertAndGetIdAsync(TEntity entity, bool autoSave = true) + { + var hasCreationTime = typeof(IHasCreationTime).IsAssignableFrom(typeof(TEntity)); + if (hasCreationTime) + { + ((IHasCreationTime)entity).CreationTime = DateTime.Now; + } + await Table.AddAsync(entity); + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + return entity.Id; + } + + public virtual void InsertList(List entities, bool autoSave = true) + { + var hasCreationTime = typeof(IHasCreationTime).IsAssignableFrom(typeof(TEntity)); + if (hasCreationTime) + { + foreach (var entity in entities) + { + ((IHasCreationTime)entity).CreationTime = DateTime.Now; + } + } + Table.AddRange(entities); + if (autoSave) + { + DbContext.SaveChanges(); + } + } + + public virtual async Task InsertListAsync(List entities, bool autoSave = true) + { + var hasCreationTime = typeof(IHasCreationTime).IsAssignableFrom(typeof(TEntity)); + if (hasCreationTime) + { + foreach (var entity in entities) + { + ((IHasCreationTime)entity).CreationTime = DateTime.Now; + } + } + await Table.AddRangeAsync(entities); + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + } + + public virtual void Update(TEntity entity, bool autoSave = true) + { + var hasLastModificationTime = typeof(IHasLastModificationTime).IsAssignableFrom(typeof(TEntity)); + if (hasLastModificationTime) + { + ((IHasLastModificationTime)entity).LastModificationTime = DateTime.Now; + } + Table.Update(entity); + if (autoSave) + { + DbContext.SaveChanges(); + } + } + + public virtual async Task UpdateAsync(TEntity entity, bool autoSave = true) + { + var hasLastModificationTime = typeof(IHasLastModificationTime).IsAssignableFrom(typeof(TEntity)); + if (hasLastModificationTime) + { + ((IHasLastModificationTime)entity).LastModificationTime = DateTime.Now; + } + Table.Update(entity); + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + } + + public virtual void UpdateList(IEnumerable entities) + { + Table.UpdateRange(entities); + DbContext.SaveChanges(); + } + + public virtual async Task UpdateListAsync(IEnumerable entities) + { + Table.UpdateRange(entities); + await DbContext.SaveChangesAsync(); + } + + public virtual void Delete(TPrimaryKey id, bool autoSave = true) + { + var entity = Find(id); + if (entity == null) + { + throw new ArgumentNullException(); + } + Delete(entity, autoSave); + } + + public virtual async Task DeleteAsync(TPrimaryKey id, bool autoSave = true) + { + var entity = await FindAsync(id); + if (entity == null) + { + throw new ArgumentNullException(); + } + await DeleteAsync(entity, autoSave); + } + + public virtual void Delete(TEntity entity, bool autoSave = true) + { + var hasDeletionTime = typeof(IHasDeletionTime).IsAssignableFrom(typeof(TEntity)); + if (hasDeletionTime) + { + ((IHasDeletionTime)entity).DeletionTime = DateTime.Now; + } + var isDeleteEntity = typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)); + if (isDeleteEntity) + { + ((ISoftDelete)entity).IsDeleted = true; + } + Update(entity, autoSave); + } + + public virtual async Task DeleteAsync(TEntity entity, bool autoSave = true) + { + var hasDeletionTime = typeof(IHasDeletionTime).IsAssignableFrom(typeof(TEntity)); + if (hasDeletionTime) + { + ((IHasDeletionTime)entity).DeletionTime = DateTime.Now; + } + var isDeleteEntity = typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)); + if (isDeleteEntity) + { + ((ISoftDelete)entity).IsDeleted = true; + } + await UpdateAsync(entity, autoSave); + } + + public virtual void HardDelete(TPrimaryKey id, bool autoSave = true) + { + var entity = Find(id); + if (entity == null) + { + throw new ArgumentNullException(); + } + HardDelete(entity, autoSave); + } + + public async Task HardDeleteAsync(TPrimaryKey id, bool autoSave = true) + { + var entity = await FindAsync(id); + if (entity == null) + { + throw new ArgumentNullException(); + } + await HardDeleteAsync(entity, autoSave); + } + + public virtual void HardDelete(TEntity entity, bool autoSave = true) + { + Table.Remove(entity); + if (autoSave) + { + DbContext.SaveChanges(); + } + } + + public virtual async Task HardDeleteAsync(TEntity entity, bool autoSave = true) + { + Table.Remove(entity); + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + } + } +} diff --git a/LBON.EntityFrameworkCore/Repositories/IEfRepository.cs b/LBON.EntityFrameworkCore/Repositories/IEfRepository.cs new file mode 100644 index 0000000..b901a1d --- /dev/null +++ b/LBON.EntityFrameworkCore/Repositories/IEfRepository.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using LBON.EntityFrameworkCore.Entities; + +namespace LBON.EntityFrameworkCore.Repositories +{ + public interface IEfRepository where TEntity : class, IEntity + { + IQueryable GetAll(params Expression>[] propertySelectors); + + IQueryable GetAll(Expression> expression, + params Expression>[] propertySelectors); + + TEntity Find(TPrimaryKey id); + + Task FindAsync(TPrimaryKey id); + + TEntity Get(Expression> expression, + params Expression>[] propertySelectors); + + Task GetAsync(Expression> expression, + params Expression>[] propertySelectors); + + void Insert(TEntity entity, bool autoSave = true); + + Task InsertAsync(TEntity entity, bool autoSave = true); + + Task InsertAndGetIdAsync(TEntity entity, bool autoSave = true); + + void InsertList(List entities, bool autoSave = true); + + Task InsertListAsync(List entities, bool autoSave = true); + + void Update(TEntity entity, bool autoSave = true); + + Task UpdateAsync(TEntity entity, bool autoSave = true); + + void UpdateList(IEnumerable entities); + + Task UpdateListAsync(IEnumerable entities); + + void Delete(TPrimaryKey id, bool autoSave = true); + + Task DeleteAsync(TPrimaryKey id, bool autoSave = true); + + void Delete(TEntity entity, bool autoSave = true); + + Task DeleteAsync(TEntity entity, bool autoSave = true); + + void HardDelete(TPrimaryKey id, bool autoSave = true); + + Task HardDeleteAsync(TPrimaryKey id, bool autoSave = true); + + void HardDelete(TEntity entity, bool autoSave = true); + + Task HardDeleteAsync(TEntity entity, bool autoSave = true); + } +} diff --git a/LBON.Extensions/LBON.Extensions.csproj b/LBON.Extensions/LBON.Extensions.csproj index cf96eeb..3df687f 100644 --- a/LBON.Extensions/LBON.Extensions.csproj +++ b/LBON.Extensions/LBON.Extensions.csproj @@ -5,7 +5,7 @@ Lu Ban Of .Net - .Net 高可用、高效率的扩展组件 0.0.1 false - 2.0.1.3 + 2.0.1.4 logo.png CacoCode LICENSE @@ -17,8 +17,15 @@ CacoCode + + + + + + + @@ -38,8 +45,4 @@ - - - - diff --git a/LBON.Helper/LBON.Helper.csproj b/LBON.Helper/LBON.Helper.csproj index 7735f6c..0266965 100644 --- a/LBON.Helper/LBON.Helper.csproj +++ b/LBON.Helper/LBON.Helper.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 2.0.1.3 + 2.0.1.4 CacoCode CacoCode Lu Ban Of .Net - .Net 高可用、高效率的扩展组件 diff --git a/LBON.Helper/TypeHelper.cs b/LBON.Helper/TypeHelper.cs new file mode 100644 index 0000000..f39900f --- /dev/null +++ b/LBON.Helper/TypeHelper.cs @@ -0,0 +1,64 @@ +using System; +using System.Reflection; + +namespace LBON.Helper +{ + public static class TypeHelper + { + public static bool IsFunc(object obj) + { + if (obj == null) + { + return false; + } + + var type = obj.GetType(); + if (!type.GetTypeInfo().IsGenericType) + { + return false; + } + + return type.GetGenericTypeDefinition() == typeof(Func<>); + } + + public static bool IsFunc(object obj) + { + return obj != null && obj.GetType() == typeof(Func); + } + + public static bool IsPrimitiveExtendedIncludingNullable(Type type, bool includeEnums = false) + { + if (IsPrimitiveExtended(type, includeEnums)) + { + return true; + } + + if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + return IsPrimitiveExtended(type.GenericTypeArguments[0], includeEnums); + } + + return false; + } + + private static bool IsPrimitiveExtended(Type type, bool includeEnums) + { + if (type.GetTypeInfo().IsPrimitive) + { + return true; + } + + if (includeEnums && type.GetTypeInfo().IsEnum) + { + return true; + } + + return type == typeof(string) || + type == typeof(decimal) || + type == typeof(DateTime) || + type == typeof(DateTimeOffset) || + type == typeof(TimeSpan) || + type == typeof(Guid); + } + } +} diff --git a/LBON.sln b/LBON.sln index 9b4273d..4a8aa31 100644 --- a/LBON.sln +++ b/LBON.sln @@ -9,7 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LBON.Consts", "LBON.Consts\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LBON.Extensions", "LBON.Extensions\LBON.Extensions.csproj", "{1FE8124D-2ADE-44C1-AEA1-C449C3E4534C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LBON.Helper", "LBON.Helper\LBON.Helper.csproj", "{18908BD4-ECE4-4542-B932-26B2A0833E2A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LBON.Helper", "LBON.Helper\LBON.Helper.csproj", "{18908BD4-ECE4-4542-B932-26B2A0833E2A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LBON.EntityFrameworkCore", "LBON.EntityFrameworkCore\LBON.EntityFrameworkCore.csproj", "{BE2D75F8-10A4-4936-86DF-C12F28471102}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LBON.DependencyInjection", "LBON.DependencyInjection\LBON.DependencyInjection.csproj", "{0F35743F-1461-4950-88B7-ED0F7FE425A2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +37,14 @@ Global {18908BD4-ECE4-4542-B932-26B2A0833E2A}.Debug|Any CPU.Build.0 = Debug|Any CPU {18908BD4-ECE4-4542-B932-26B2A0833E2A}.Release|Any CPU.ActiveCfg = Release|Any CPU {18908BD4-ECE4-4542-B932-26B2A0833E2A}.Release|Any CPU.Build.0 = Release|Any CPU + {BE2D75F8-10A4-4936-86DF-C12F28471102}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE2D75F8-10A4-4936-86DF-C12F28471102}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE2D75F8-10A4-4936-86DF-C12F28471102}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE2D75F8-10A4-4936-86DF-C12F28471102}.Release|Any CPU.Build.0 = Release|Any CPU + {0F35743F-1461-4950-88B7-ED0F7FE425A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0F35743F-1461-4950-88B7-ED0F7FE425A2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0F35743F-1461-4950-88B7-ED0F7FE425A2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0F35743F-1461-4950-88B7-ED0F7FE425A2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE