From 25f9679e839cc261c42aa0cc40ee822aa5667186 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Mon, 20 Sep 2021 10:21:48 -0400 Subject: [PATCH] db context --- .gitignore | 388 ++++++++++++++++++ .../DbContextQueryableExtensions.cs | 64 +++ .../IContextServiceProvider.cs | 16 + .../IDbContextQueryableExtensionService.cs | 11 + .../IDbContextService.cs | 20 + .../IDbContextTransactionService.cs | 10 + PoweredSoft.DbContext.Core/IDbQueryService.cs | 10 + .../IDbQueryService`.cs | 9 + PoweredSoft.DbContext.Core/IDbSetService.cs | 16 + PoweredSoft.DbContext.Core/IDbSetService`.cs | 17 + .../IQueryableOperationsService.cs | 30 ++ .../IQueryableService.cs | 11 + .../PoweredSoft.DbContext.Core.csproj | 18 + .../DbContextService.cs | 89 ++++ .../DbContextTransactionService.cs | 23 ++ .../DbQueryService.cs | 43 ++ .../DbQueryService`.cs | 60 +++ .../DbSetService.cs | 31 ++ .../DbSetService`.cs | 56 +++ .../EntityFrameworkDbContextServiceFactory.cs | 29 ++ .../EntityFrameworkQueryableService.cs | 81 ++++ ...weredSoft.DbContext.EntityFramework.csproj | 28 ++ .../ServiceCollectionExtensions.cs | 18 + .../DbContextService.cs | 84 ++++ .../DbContextTransactionService.cs | 24 ++ .../DbQueryService`.cs | 61 +++ .../DbSetService`.cs | 55 +++ ...ityFrameworkCoreDbContextServiceFactory.cs | 29 ++ .../EntityFrameworkCoreQueryableService.cs | 93 +++++ ...dSoft.DbContext.EntityFrameworkCore.csproj | 27 ++ .../ServiceCollectionExtensions.cs | 18 + .../DbContextServiceProvider.cs | 32 ++ .../PoweredSoft.DbContext.csproj | 26 ++ .../QueryableExtensionService.cs | 73 ++++ .../ServiceCollectionExtensions.cs | 25 ++ 35 files changed, 1625 insertions(+) create mode 100644 .gitignore create mode 100644 PoweredSoft.DbContext.Core/DbContextQueryableExtensions.cs create mode 100644 PoweredSoft.DbContext.Core/IContextServiceProvider.cs create mode 100644 PoweredSoft.DbContext.Core/IDbContextQueryableExtensionService.cs create mode 100644 PoweredSoft.DbContext.Core/IDbContextService.cs create mode 100644 PoweredSoft.DbContext.Core/IDbContextTransactionService.cs create mode 100644 PoweredSoft.DbContext.Core/IDbQueryService.cs create mode 100644 PoweredSoft.DbContext.Core/IDbQueryService`.cs create mode 100644 PoweredSoft.DbContext.Core/IDbSetService.cs create mode 100644 PoweredSoft.DbContext.Core/IDbSetService`.cs create mode 100644 PoweredSoft.DbContext.Core/IQueryableOperationsService.cs create mode 100644 PoweredSoft.DbContext.Core/IQueryableService.cs create mode 100644 PoweredSoft.DbContext.Core/PoweredSoft.DbContext.Core.csproj create mode 100644 PoweredSoft.DbContext.EntityFramework/DbContextService.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/DbContextTransactionService.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/DbQueryService.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/DbQueryService`.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/DbSetService.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/DbSetService`.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/EntityFrameworkDbContextServiceFactory.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/EntityFrameworkQueryableService.cs create mode 100644 PoweredSoft.DbContext.EntityFramework/PoweredSoft.DbContext.EntityFramework.csproj create mode 100644 PoweredSoft.DbContext.EntityFramework/ServiceCollectionExtensions.cs create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/DbContextService.cs create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/DbContextTransactionService.cs create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/DbQueryService`.cs create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/DbSetService`.cs create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreDbContextServiceFactory.cs create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreQueryableService.cs create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/PoweredSoft.DbContext.EntityFrameworkCore.csproj create mode 100644 PoweredSoft.DbContext.EntityFrameworkCore/ServiceCollectionExtensions.cs create mode 100644 PoweredSoft.DbContext/DbContextServiceProvider.cs create mode 100644 PoweredSoft.DbContext/PoweredSoft.DbContext.csproj create mode 100644 PoweredSoft.DbContext/QueryableExtensionService.cs create mode 100644 PoweredSoft.DbContext/ServiceCollectionExtensions.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34c8dee --- /dev/null +++ b/.gitignore @@ -0,0 +1,388 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Nuget personal access tokens and Credentials +nuget.config + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +.idea/ +*.sln.iml diff --git a/PoweredSoft.DbContext.Core/DbContextQueryableExtensions.cs b/PoweredSoft.DbContext.Core/DbContextQueryableExtensions.cs new file mode 100644 index 0000000..a4edb51 --- /dev/null +++ b/PoweredSoft.DbContext.Core/DbContextQueryableExtensions.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +namespace PoweredSoft.DbContext.Core +{ + public static class DbContextQueryableExtensions + { + private static IQueryableExtensionService service; + + public static void InitializeExtensionService(IQueryableExtensionService queryableService) + { + service = queryableService; + } + + public static IQueryable Include(this IQueryable source, Expression> path) + where T : class + => service.Include(source, path); + + public static IQueryable AsNoTracking(this IQueryable source) + => service.AsNoTracking(source); + + public static IQueryable AsNoTracking(this IQueryable source) where T : class + => service.AsNoTracking(source); + + public static IQueryable Include(this IQueryable source, string path) + where T : class + => service.Include(source, path); + + public static Task FirstOrDefaultAsync(this IQueryable source, + CancellationToken cancellationToken = default) where T : class => + service.FirstOrDefaultAsync(source, cancellationToken); + + public static Task FirstOrDefaultAsync(this IQueryable source, + Expression> predicate, + CancellationToken cancellationToken = default) where T : class => + service.FirstOrDefaultAsync(source, predicate, cancellationToken); + + public static Task FirstAsync(this IQueryable source, + CancellationToken cancellationToken = default) where T : class => + service.FirstAsync(source, cancellationToken); + + public static Task FirstAsync(this IQueryable source, + Expression> predicate, + CancellationToken cancellationToken = default) where T : class => + service.FirstAsync(source, predicate, cancellationToken); + + public static Task AnyAsync(this IQueryable source, + CancellationToken cancellationToken = default) where T : class => + service.AnyAsync(source, cancellationToken); + + public static Task AnyAsync(this IQueryable source, + Expression> predicate, + CancellationToken cancellationToken = default) where T : class => + service.AnyAsync(source, predicate, cancellationToken); + + public static Task> ToListAsync(this IQueryable source, + CancellationToken cancellationToken = default) where T : class => + service.ToListAsync(source, cancellationToken); + } +} diff --git a/PoweredSoft.DbContext.Core/IContextServiceProvider.cs b/PoweredSoft.DbContext.Core/IContextServiceProvider.cs new file mode 100644 index 0000000..8258b23 --- /dev/null +++ b/PoweredSoft.DbContext.Core/IContextServiceProvider.cs @@ -0,0 +1,16 @@ +using System; + +namespace PoweredSoft.DbContext.Core +{ + public interface IDbContextServiceProvider + { + IDbContextService GetServiceForType(); + IDbContextService GetServiceForType(Type contextType); + } + + public interface IContextServiceFactory + { + bool CanService(Type contextType); + IDbContextService Create(Type contextType); + } +} diff --git a/PoweredSoft.DbContext.Core/IDbContextQueryableExtensionService.cs b/PoweredSoft.DbContext.Core/IDbContextQueryableExtensionService.cs new file mode 100644 index 0000000..26e1add --- /dev/null +++ b/PoweredSoft.DbContext.Core/IDbContextQueryableExtensionService.cs @@ -0,0 +1,11 @@ +using System.Linq; + +namespace PoweredSoft.DbContext.Core +{ + + public interface IQueryableExtensionService : IQueryableOperationsService + { + IQueryableService ResolveService(IQueryable queryable); + IQueryableService ResolveService(IQueryable queryable); + } +} diff --git a/PoweredSoft.DbContext.Core/IDbContextService.cs b/PoweredSoft.DbContext.Core/IDbContextService.cs new file mode 100644 index 0000000..bf2d89f --- /dev/null +++ b/PoweredSoft.DbContext.Core/IDbContextService.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using System.Threading.Tasks; + +namespace PoweredSoft.DbContext.Core +{ + public interface IDbContextService + { + IDbSetService Set() where TEntity : class; + IDbSetService Set(Type entityType); + int SaveChanges(); + Task SaveChangesAsync(); + T GetUnderlyingOrm() where T : class; + IEnumerable GetKeyProperties(Type entityType); + IEnumerable>> GetKeyProperties(); + IDbContextTransactionService BeginTransaction(); + } +} diff --git a/PoweredSoft.DbContext.Core/IDbContextTransactionService.cs b/PoweredSoft.DbContext.Core/IDbContextTransactionService.cs new file mode 100644 index 0000000..2242da3 --- /dev/null +++ b/PoweredSoft.DbContext.Core/IDbContextTransactionService.cs @@ -0,0 +1,10 @@ +using System; + +namespace PoweredSoft.DbContext.Core +{ + public interface IDbContextTransactionService : IDisposable + { + void Commit(); + void Rollback(); + } +} diff --git a/PoweredSoft.DbContext.Core/IDbQueryService.cs b/PoweredSoft.DbContext.Core/IDbQueryService.cs new file mode 100644 index 0000000..577e153 --- /dev/null +++ b/PoweredSoft.DbContext.Core/IDbQueryService.cs @@ -0,0 +1,10 @@ +using System.Linq; + +namespace PoweredSoft.DbContext.Core +{ + public interface IDbQueryService : IOrderedQueryable + { + IDbQueryService AsNoTracking(); + IDbQueryService Include(string path); + } +} diff --git a/PoweredSoft.DbContext.Core/IDbQueryService`.cs b/PoweredSoft.DbContext.Core/IDbQueryService`.cs new file mode 100644 index 0000000..42c3f54 --- /dev/null +++ b/PoweredSoft.DbContext.Core/IDbQueryService`.cs @@ -0,0 +1,9 @@ +using System.Linq; + +namespace PoweredSoft.DbContext.Core +{ + public interface IDbQueryService : IOrderedQueryable, IDbQueryService + where TEntity : class + { + } +} diff --git a/PoweredSoft.DbContext.Core/IDbSetService.cs b/PoweredSoft.DbContext.Core/IDbSetService.cs new file mode 100644 index 0000000..2b47384 --- /dev/null +++ b/PoweredSoft.DbContext.Core/IDbSetService.cs @@ -0,0 +1,16 @@ +using System.Collections; + +namespace PoweredSoft.DbContext.Core +{ + public interface IDbSetService : IDbQueryService + { + IList Local { get; } + + object Add(object entity); + void AddRange(IEnumerable entities); + object Remove(object entity); + void RemoveRange(IEnumerable entities); + object Find(params object[] keyValues); + object Attach(object entity); + } +} diff --git a/PoweredSoft.DbContext.Core/IDbSetService`.cs b/PoweredSoft.DbContext.Core/IDbSetService`.cs new file mode 100644 index 0000000..beefc2b --- /dev/null +++ b/PoweredSoft.DbContext.Core/IDbSetService`.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace PoweredSoft.DbContext.Core +{ + public interface IDbSetService : IDbQueryService, IDbSetService + where TEntity : class + { + new ICollection Local { get; } + + TEntity Add(TEntity entity); + TEntity Remove(TEntity entity); + void AddRange(IEnumerable entities); + void RemoveRange(IEnumerable entities); + TEntity Attach(TEntity entity); + } +} diff --git a/PoweredSoft.DbContext.Core/IQueryableOperationsService.cs b/PoweredSoft.DbContext.Core/IQueryableOperationsService.cs new file mode 100644 index 0000000..617c985 --- /dev/null +++ b/PoweredSoft.DbContext.Core/IQueryableOperationsService.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +namespace PoweredSoft.DbContext.Core +{ + public interface IQueryableOperationsService + { + IQueryable Include(IQueryable source, Expression> path) + where T : class; + IQueryable AsNoTracking(IQueryable source) + where T : class; + IQueryable AsNoTracking(IQueryable source); + IQueryable Include(IQueryable source, string path) + where T : class; + Task FirstOrDefaultAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class; + Task FirstOrDefaultAsync(IQueryable source, Expression> predicate, + CancellationToken cancellationToken = default) where T : class; + Task FirstAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class; + Task FirstAsync(IQueryable source, Expression> predicate, + CancellationToken cancellationToken = default) where T : class; + Task> ToListAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class; + + Task AnyAsync(IQueryable source, System.Linq.Expressions.Expression> predicate, CancellationToken cancellationToken) where T : class; + Task AnyAsync(IQueryable source, CancellationToken cancellationToken) where T : class; + } +} diff --git a/PoweredSoft.DbContext.Core/IQueryableService.cs b/PoweredSoft.DbContext.Core/IQueryableService.cs new file mode 100644 index 0000000..b06afb9 --- /dev/null +++ b/PoweredSoft.DbContext.Core/IQueryableService.cs @@ -0,0 +1,11 @@ +using System.Linq; +using System.Threading; + +namespace PoweredSoft.DbContext.Core +{ + public interface IQueryableService : IQueryableOperationsService + { + bool CanHandle(IQueryable queryable); + bool CanHandle(IQueryable queryable); + } +} diff --git a/PoweredSoft.DbContext.Core/PoweredSoft.DbContext.Core.csproj b/PoweredSoft.DbContext.Core/PoweredSoft.DbContext.Core.csproj new file mode 100644 index 0000000..e7758eb --- /dev/null +++ b/PoweredSoft.DbContext.Core/PoweredSoft.DbContext.Core.csproj @@ -0,0 +1,18 @@ + + + + netstandard2.0 + Powered Softwares Inc. + 1.1.0$(VersionSuffix) + https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&r=g&d=retro + $(MSBuildProjectName) + initial + $(MSBuildProjectName) + initial + False + PoweredSoft + PoweredSoft + False + + + diff --git a/PoweredSoft.DbContext.EntityFramework/DbContextService.cs b/PoweredSoft.DbContext.EntityFramework/DbContextService.cs new file mode 100644 index 0000000..c5617db --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/DbContextService.cs @@ -0,0 +1,89 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using System.Threading.Tasks; +using System.Data.Entity; +using System.Data.Entity.Infrastructure; +using System.Data.Entity.Core.Metadata.Edm; +using System.Linq; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class DbContextService : IDbContextService + { + public DbContextService(System.Data.Entity.DbContext dbContext) + { + DbContext = dbContext; + } + + protected System.Data.Entity.DbContext DbContext { get; } + + public IDbContextTransactionService BeginTransaction() + { + return new DbContextTransactionService(DbContext.Database.BeginTransaction()); + } + + public IEnumerable GetKeyProperties(Type entityType) + { + var metadata = ((IObjectContextAdapter)DbContext).ObjectContext.MetadataWorkspace; + + // Get the mapping between CLR types and metadata OSpace + var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace)); + + // Get metadata for given CLR type + var entityMetadata = metadata + .GetItems(DataSpace.OSpace) + .Single(e => objectItemCollection.GetClrType(e) == entityType); + + var ret = entityMetadata.KeyProperties.Select(t => entityType.GetProperty(t.Name)); + return ret; + } + + public IEnumerable>> GetKeyProperties() + { + var keyProps = GetKeyProperties(typeof(TEntity)); + + var parameter = Expression.Parameter(typeof(TEntity)); + var result = keyProps + .Select(keyProp => + { + var property = Expression.Property(parameter, keyProp); + var lambda = Expression.Lambda>(Expression.Convert(property, typeof(Object)), parameter); + return (Expression>)lambda; + }) + .ToList(); + return result; + } + + public T GetUnderlyingOrm() where T : class + { + return this.DbContext as T; + } + + public int SaveChanges() + { + return DbContext.SaveChanges(); + } + + public Task SaveChangesAsync() + { + return DbContext.SaveChangesAsync(); + } + + public IDbSetService Set() where TEntity : class + { + var realDbSet = DbContext.Set(); + var dbSet = new DbSetService(realDbSet); + return dbSet; + } + + public IDbSetService Set(Type entityType) + { + var realDbSet = DbContext.Set(entityType); + var dbSet = new DbSetService(realDbSet); + return dbSet; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/DbContextTransactionService.cs b/PoweredSoft.DbContext.EntityFramework/DbContextTransactionService.cs new file mode 100644 index 0000000..638ae2f --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/DbContextTransactionService.cs @@ -0,0 +1,23 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class DbContextTransactionService : IDbContextTransactionService + { + private readonly System.Data.Entity.DbContextTransaction dbContextTransaction; + + public DbContextTransactionService(System.Data.Entity.DbContextTransaction dbContextTransaction) + { + this.dbContextTransaction = dbContextTransaction; + } + + public void Commit() => dbContextTransaction.Commit(); + + public void Dispose() => dbContextTransaction.Dispose(); + + public void Rollback() => dbContextTransaction.Rollback(); + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/DbQueryService.cs b/PoweredSoft.DbContext.EntityFramework/DbQueryService.cs new file mode 100644 index 0000000..3e4c256 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/DbQueryService.cs @@ -0,0 +1,43 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class DbQueryService : IDbQueryService + { + public DbQueryService(DbQuery dbQuery) + { + DbQuery = dbQuery; + } + + public Expression Expression => ((IQueryable)DbQuery).Expression; + + public Type ElementType => ((IQueryable)DbQuery).ElementType; + + public IQueryProvider Provider => ((IQueryable)DbQuery).Provider; + + protected DbQuery DbQuery { get; } + + public IDbQueryService AsNoTracking() + { + var efQuery = DbQuery.AsNoTracking(); + var ret = new DbQueryService(efQuery); + return ret; + } + + public IEnumerator GetEnumerator() => ((IQueryable)DbQuery).GetEnumerator(); + + public IDbQueryService Include(string path) + { + var efQuery = DbQuery.Include(path); + var ret = new DbQueryService(efQuery); + return ret; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/DbQueryService`.cs b/PoweredSoft.DbContext.EntityFramework/DbQueryService`.cs new file mode 100644 index 0000000..254a88a --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/DbQueryService`.cs @@ -0,0 +1,60 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class DbQueryService : IDbQueryService + where TEntity : class + { + public DbQueryService(DbQuery dbQuery) + { + DbQuery = dbQuery; + } + + public Expression Expression => ((IQueryable)DbQuery).Expression; + + public Type ElementType => ((IQueryable)DbQuery).ElementType; + + public IQueryProvider Provider => ((IQueryable)DbQuery).Provider; + + protected DbQuery DbQuery { get; } + + public IDbQueryService AsNoTracking() + { + var efQuery = DbQuery.AsNoTracking(); + var ret = new DbQueryService(efQuery); + return ret; + } + + public IEnumerator GetEnumerator() => ((IQueryable)DbQuery).GetEnumerator(); + + public IDbQueryService Include(string path) + { + var efQuery = DbQuery.Include(path); + var ret = new DbQueryService(efQuery); + return ret; + } + + IDbQueryService IDbQueryService.AsNoTracking() + { + var efQuery = DbQuery.AsNoTracking(); + var ret = new DbQueryService(efQuery); + return ret; + } + + IEnumerator IEnumerable.GetEnumerator() => ((IQueryable)DbQuery).GetEnumerator(); + + IDbQueryService IDbQueryService.Include(string path) + { + var efQuery = DbQuery.Include(path); + var ret = new DbQueryService(efQuery); + return ret; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/DbSetService.cs b/PoweredSoft.DbContext.EntityFramework/DbSetService.cs new file mode 100644 index 0000000..2f18436 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/DbSetService.cs @@ -0,0 +1,31 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Data.Entity; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class DbSetService : DbQueryService, IDbSetService + { + protected DbSet DbSet { get; } + + public DbSetService(DbSet dbSet) : base(dbSet) + { + DbSet = dbSet; + } + + public IList Local => DbSet.Local; + + public object Add(object entity) => DbSet.Add(entity); + + public object Remove(object entity) => DbSet.Remove(entity); + public void AddRange(IEnumerable entities) => DbSet.AddRange(entities); + public void RemoveRange(IEnumerable entities) => DbSet.RemoveRange(entities); + + public object Find(params object[] keyValues) => DbSet.Find(keyValues); + + public object Attach(object entity) => DbSet.Attach(entity); + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/DbSetService`.cs b/PoweredSoft.DbContext.EntityFramework/DbSetService`.cs new file mode 100644 index 0000000..4323f4f --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/DbSetService`.cs @@ -0,0 +1,56 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class DbSetService : DbQueryService, IDbSetService + where TEntity : class + { + public DbSetService(DbSet dbSet) : base(dbSet) + { + DbSet = dbSet; + } + + protected DbSet DbSet { get; } + + public ICollection Local => DbSet.Local; + + IList IDbSetService.Local => DbSet.Local; + + public TEntity Add(TEntity entity) => DbSet.Add(entity); + + public TEntity Remove(TEntity entity) => DbSet.Remove(entity); + + IEnumerator IEnumerable.GetEnumerator() => ((IQueryable)DbSet).GetEnumerator(); + + public TEntity Create() => DbSet.Create(); + + public TEntity Attach(TEntity entity) => DbSet.Attach(entity); + + public DbSet GetEntityFrameworkSet() => DbSet; + + public TEntity Find(params object[] keyValues) => DbSet.Find(keyValues); + + void IDbSetService.AddRange(IEnumerable entities) => DbSet.AddRange(entities); + + void IDbSetService.RemoveRange(IEnumerable entities) => DbSet.RemoveRange(entities); + + public object Add(object entity) => DbSet.Add((TEntity)entity); + + public void AddRange(IEnumerable entities) => DbSet.AddRange(entities.Cast()); + + public object Remove(object entity) => DbSet.Remove((TEntity)entity); + + public void RemoveRange(IEnumerable entities) => DbSet.RemoveRange(entities.Cast()); + + object IDbSetService.Find(params object[] keyValues) => DbSet.Find(keyValues); + + public object Attach(object entity) => DbSet.Attach((TEntity)entity); + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/EntityFrameworkDbContextServiceFactory.cs b/PoweredSoft.DbContext.EntityFramework/EntityFrameworkDbContextServiceFactory.cs new file mode 100644 index 0000000..258c60a --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/EntityFrameworkDbContextServiceFactory.cs @@ -0,0 +1,29 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class EntityFrameworkDbContextServiceFactory : IContextServiceFactory + { + private readonly IServiceProvider serviceProvider; + + public EntityFrameworkDbContextServiceFactory(IServiceProvider serviceProvider) + { + this.serviceProvider = serviceProvider; + } + + public bool CanService(Type contextType) + { + return typeof(System.Data.Entity.DbContext).IsAssignableFrom(contextType); + } + + public IDbContextService Create(Type contextType) + { + var service = serviceProvider.GetService(contextType) as System.Data.Entity.DbContext; + var contextService = new DbContextService(service); + return contextService; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/EntityFrameworkQueryableService.cs b/PoweredSoft.DbContext.EntityFramework/EntityFrameworkQueryableService.cs new file mode 100644 index 0000000..e950b74 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/EntityFrameworkQueryableService.cs @@ -0,0 +1,81 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public class EntityFrameworkQueryableService : IQueryableService + { + public Task AnyAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken) where T : class + { + return System.Data.Entity.QueryableExtensions.AnyAsync(source, predicate, cancellationToken); + } + + public Task AnyAsync(IQueryable source, CancellationToken cancellationToken) where T : class + { + return System.Data.Entity.QueryableExtensions.AnyAsync(source, cancellationToken); + } + + public IQueryable AsNoTracking(IQueryable source) where T : class + { + return System.Data.Entity.QueryableExtensions.AsNoTracking(source); + } + + public IQueryable AsNoTracking(IQueryable source) + { + return System.Data.Entity.QueryableExtensions.AsNoTracking(source); + } + + public bool CanHandle(IQueryable queryable) + { + return queryable.Provider is System.Data.Entity.Infrastructure.IDbAsyncQueryProvider; + } + + public bool CanHandle(IQueryable queryable) + { + return queryable.Provider is System.Data.Entity.Infrastructure.IDbAsyncQueryProvider; + } + + public Task FirstAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + { + return System.Data.Entity.QueryableExtensions.FirstAsync(source, cancellationToken); + } + + public Task FirstAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) where T : class + { + return System.Data.Entity.QueryableExtensions.FirstAsync(source, predicate, cancellationToken); + } + + public Task FirstOrDefaultAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + { + return System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(source, cancellationToken); + } + + public Task FirstOrDefaultAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) where T : class + { + return System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(source, predicate, cancellationToken); + } + + public IQueryable Include(IQueryable source, Expression> path) + where T : class + { + return System.Data.Entity.QueryableExtensions.Include(source, path); + } + + public IQueryable Include(IQueryable source, string path) + where T : class + { + return System.Data.Entity.QueryableExtensions.Include(source, path); + } + + public Task> ToListAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + { + return System.Data.Entity.QueryableExtensions.ToListAsync(source, cancellationToken); + } + } +} diff --git a/PoweredSoft.DbContext.EntityFramework/PoweredSoft.DbContext.EntityFramework.csproj b/PoweredSoft.DbContext.EntityFramework/PoweredSoft.DbContext.EntityFramework.csproj new file mode 100644 index 0000000..d8d57ec --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/PoweredSoft.DbContext.EntityFramework.csproj @@ -0,0 +1,28 @@ + + + + netstandard2.1;net461 + Powered Softwares Inc. + 1.1.0$(VersionSuffix) + https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&r=g&d=retro + $(MSBuildProjectName) + initial + $(MSBuildProjectName) + initial + False + PoweredSoft + PoweredSoft + False + 8.0 + + + + + + + + + + + + diff --git a/PoweredSoft.DbContext.EntityFramework/ServiceCollectionExtensions.cs b/PoweredSoft.DbContext.EntityFramework/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..0a539ec --- /dev/null +++ b/PoweredSoft.DbContext.EntityFramework/ServiceCollectionExtensions.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.DependencyInjection; +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFramework +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddPoweredSoftEntityFrameworkDbContextServices(this IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + return services; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/DbContextService.cs b/PoweredSoft.DbContext.EntityFrameworkCore/DbContextService.cs new file mode 100644 index 0000000..2121fd5 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/DbContextService.cs @@ -0,0 +1,84 @@ +using PoweredSoft.DbContext.Core; +using PoweredSoft.Reflection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Threading.Tasks; + +namespace PoweredSoft.DbContext.EntityFrameworkCore +{ + public class DbContextService : IDbContextService + { + public DbContextService(Microsoft.EntityFrameworkCore.DbContext dbContext) + { + DbContext = dbContext; + } + + protected Microsoft.EntityFrameworkCore.DbContext DbContext { get; } + + private MethodInfo setGenericMethodInfo = null; + + public IDbContextTransactionService BeginTransaction() + { + return new DbContextTransactionService(DbContext.Database.BeginTransaction()); + } + + public IEnumerable GetKeyProperties(Type entityType) + { + var key = DbContext.Model.GetEntityTypes().First(t => t.ClrType == entityType).FindPrimaryKey(); + var keysProperties = key.Properties.Select(t => t.PropertyInfo); + return keysProperties; + } + + public IEnumerable>> GetKeyProperties() + { + var keyProps = GetKeyProperties(typeof(TEntity)); + + var parameter = Expression.Parameter(typeof(TEntity)); + var result = keyProps + .Select(keyProp => + { + var property = Expression.Property(parameter, keyProp); + var lambda = Expression.Lambda>(Expression.Convert(property, typeof(Object)), parameter); + return (Expression>)lambda; + }) + .ToList(); + return result; + } + + public T GetUnderlyingOrm() where T : class + { + return this.DbContext as T; + } + + public int SaveChanges() + { + return DbContext.SaveChanges(); + } + + public Task SaveChangesAsync() + { + return DbContext.SaveChangesAsync(); + } + + [MethodKey("SetGeneric")] + public IDbSetService Set() where TEntity : class + { + var realDbSet = DbContext.Set(); + var dbSet = new DbSetService(realDbSet); + return dbSet; + } + + public IDbSetService Set(Type entityType) + { + if (setGenericMethodInfo == null) + setGenericMethodInfo = this.GetType().GetMethodByKey("SetGeneric", BindingFlags.Public | BindingFlags.Instance); + + var method = setGenericMethodInfo.MakeGenericMethod(entityType); + var result = method.Invoke(this, new object[] { }) as IDbSetService; + return result; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/DbContextTransactionService.cs b/PoweredSoft.DbContext.EntityFrameworkCore/DbContextTransactionService.cs new file mode 100644 index 0000000..8c6c863 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/DbContextTransactionService.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore.Storage; +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFrameworkCore +{ + public class DbContextTransactionService : IDbContextTransactionService + { + private readonly IDbContextTransaction dbContextTransaction; + + public DbContextTransactionService(IDbContextTransaction dbContextTransaction) + { + this.dbContextTransaction = dbContextTransaction; + } + + public void Commit() => dbContextTransaction.Commit(); + + public void Dispose() => dbContextTransaction.Dispose(); + + public void Rollback() => dbContextTransaction.Rollback(); + } +} diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/DbQueryService`.cs b/PoweredSoft.DbContext.EntityFrameworkCore/DbQueryService`.cs new file mode 100644 index 0000000..fe6e626 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/DbQueryService`.cs @@ -0,0 +1,61 @@ +using Microsoft.EntityFrameworkCore; +using PoweredSoft.DbContext.Core; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFrameworkCore +{ + public class DbQueryService : IDbQueryService + where TEntity : class + { + + public DbQueryService(IQueryable queryable) + { + Queryable = queryable; + } + + public Expression Expression => ((IQueryable)Queryable).Expression; + + public Type ElementType => ((IQueryable)Queryable).ElementType; + + public IQueryProvider Provider => ((IQueryable)Queryable).Provider; + + public IQueryable Queryable { get; } + + public IDbQueryService AsNoTracking() + { + var efQuery = Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTracking(Queryable); + var ret = new DbQueryService(efQuery); + return ret; + } + + public IEnumerator GetEnumerator() => ((IQueryable)Queryable).GetEnumerator(); + + public IDbQueryService Include(string path) + { + var efQuery = Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(Queryable, path); + var ret = new DbQueryService(efQuery); + return ret; + } + + IDbQueryService IDbQueryService.AsNoTracking() + { + var efQuery = Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTracking(Queryable); + var ret = new DbQueryService(efQuery); + return ret; + } + + IEnumerator IEnumerable.GetEnumerator() => ((IQueryable)Queryable).GetEnumerator(); + + IDbQueryService IDbQueryService.Include(string path) + { + var efQuery = Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(Queryable, path); + var ret = new DbQueryService(efQuery); + return ret; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/DbSetService`.cs b/PoweredSoft.DbContext.EntityFrameworkCore/DbSetService`.cs new file mode 100644 index 0000000..c9fbdc5 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/DbSetService`.cs @@ -0,0 +1,55 @@ +using Microsoft.EntityFrameworkCore; +using PoweredSoft.DbContext.Core; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFrameworkCore +{ + public class DbSetService : DbQueryService, IDbSetService + where TEntity : class + { + public DbSetService(DbSet dbSet) : base(dbSet) + { + DbSet = dbSet; + } + + protected DbSet DbSet { get; } + + public ICollection Local => DbSet.Local; + + IList IDbSetService.Local => DbSet.Local.ToList(); + + public TEntity Add(TEntity entity) => DbSet.Add(entity).Entity; + + public TEntity Remove(TEntity entity) => DbSet.Remove(entity).Entity; + + IEnumerator IEnumerable.GetEnumerator() => ((IQueryable)DbSet).GetEnumerator(); + + + public TEntity Attach(TEntity entity) => DbSet.Attach(entity).Entity; + + public void AddRange(IEnumerable entities) => DbSet.AddRange(entities); + + public void RemoveRange(IEnumerable entities) => DbSet.RemoveRange(entities); + + public DbSet GetEntityFrameworkSet() => DbSet; + + public TEntity Find(params object[] keyValues) => DbSet.Find(keyValues); + + public object Add(object entity) => DbSet.Add((TEntity)entity); + + public void AddRange(IEnumerable entities) => DbSet.AddRange(entities.Cast()); + + public object Remove(object entity) => DbSet.Remove((TEntity)entity); + + public void RemoveRange(IEnumerable entities) => DbSet.RemoveRange(entities.Cast()); + + object IDbSetService.Find(params object[] keyValues) => DbSet.Find(keyValues); + + public object Attach(object entity) => DbSet.Attach((TEntity)entity).Entity; + } +} diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreDbContextServiceFactory.cs b/PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreDbContextServiceFactory.cs new file mode 100644 index 0000000..1b5d095 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreDbContextServiceFactory.cs @@ -0,0 +1,29 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFrameworkCore +{ + public class EntityFrameworkCoreDbContextServiceFactory : IContextServiceFactory + { + private readonly IServiceProvider serviceProvider; + + public EntityFrameworkCoreDbContextServiceFactory(IServiceProvider serviceProvider) + { + this.serviceProvider = serviceProvider; + } + + public bool CanService(Type contextType) + { + return typeof(Microsoft.EntityFrameworkCore.DbContext).IsAssignableFrom(contextType); + } + + public IDbContextService Create(Type contextType) + { + var service = serviceProvider.GetService(contextType) as Microsoft.EntityFrameworkCore.DbContext; + var contextService = new DbContextService(service); + return contextService; + } + } +} diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreQueryableService.cs b/PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreQueryableService.cs new file mode 100644 index 0000000..ac2f0e4 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/EntityFrameworkCoreQueryableService.cs @@ -0,0 +1,93 @@ +using Microsoft.EntityFrameworkCore.Query.Internal; +using PoweredSoft.DbContext.Core; +using PoweredSoft.Reflection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace PoweredSoft.DbContext.EntityFrameworkCore +{ + public class EntityFrameworkCoreQueryableService : IQueryableService + { + private MethodInfo asNoTrackingGeneric = null; + + public Task AnyAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AnyAsync(source, predicate, cancellationToken); + } + + public Task AnyAsync(IQueryable source, CancellationToken cancellationToken) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AnyAsync(source, cancellationToken); + } + + [MethodKey("AsNoTrackingGeneric")] + public IQueryable AsNoTracking(IQueryable source) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTracking(source); + } + + public IQueryable AsNoTracking(IQueryable source) + { + if (asNoTrackingGeneric == null) + asNoTrackingGeneric = this.GetType().GetMethodByKey("AsNoTrackingGeneric"); + + var elementType = source.ElementType; + var method = asNoTrackingGeneric.MakeGenericMethod(elementType); + var result = method.Invoke(this, new object[] { source }); + return result as IQueryable; + } + + public bool CanHandle(IQueryable queryable) + { + return queryable.Provider is IAsyncQueryProvider; + } + + public bool CanHandle(IQueryable queryable) + { + return queryable.Provider is IAsyncQueryProvider; + } + + public Task FirstAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstAsync(source, cancellationToken); + } + + public Task FirstAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstAsync(source, predicate, cancellationToken); + } + + public Task FirstOrDefaultAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync(source, cancellationToken); + } + + public Task FirstOrDefaultAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync(source, predicate, cancellationToken); + } + + public IQueryable Include(IQueryable source, Expression> path) + where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(source, path); + } + + public IQueryable Include(IQueryable source, string path) + where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include(source, path); + } + + public Task> ToListAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync(source, cancellationToken); + } + } +} diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/PoweredSoft.DbContext.EntityFrameworkCore.csproj b/PoweredSoft.DbContext.EntityFrameworkCore/PoweredSoft.DbContext.EntityFrameworkCore.csproj new file mode 100644 index 0000000..9f38db5 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/PoweredSoft.DbContext.EntityFrameworkCore.csproj @@ -0,0 +1,27 @@ + + + + netstandard2.1 + Powered Softwares Inc. + 1.1.0$(VersionSuffix) + https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&r=g&d=retro + $(MSBuildProjectName) + initial + $(MSBuildProjectName) + initial + False + PoweredSoft + PoweredSoft + False + 8.0 + + + + + + + + + + + diff --git a/PoweredSoft.DbContext.EntityFrameworkCore/ServiceCollectionExtensions.cs b/PoweredSoft.DbContext.EntityFrameworkCore/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..f53da13 --- /dev/null +++ b/PoweredSoft.DbContext.EntityFrameworkCore/ServiceCollectionExtensions.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.DependencyInjection; +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.DbContext.EntityFrameworkCore +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddPoweredSoftEntityFrameworkCoreDbContextServices(this IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + return services; + } + } +} diff --git a/PoweredSoft.DbContext/DbContextServiceProvider.cs b/PoweredSoft.DbContext/DbContextServiceProvider.cs new file mode 100644 index 0000000..e2c4d21 --- /dev/null +++ b/PoweredSoft.DbContext/DbContextServiceProvider.cs @@ -0,0 +1,32 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace PoweredSoft.DbContext +{ + public class DbContextServiceProvider : IDbContextServiceProvider + { + private readonly IEnumerable factories; + + public DbContextServiceProvider(IEnumerable factories) + { + this.factories = factories; + } + + public IDbContextService GetServiceForType() + { + return GetServiceForType(typeof(TContext)); + } + + public IDbContextService GetServiceForType(Type contextType) + { + var factory = factories.FirstOrDefault(t => t.CanService(contextType)); + if (factory == null) + throw new NotSupportedException($"Could not resolve which factory should create a context service for {contextType}"); + + return factory.Create(contextType); + } + } +} diff --git a/PoweredSoft.DbContext/PoweredSoft.DbContext.csproj b/PoweredSoft.DbContext/PoweredSoft.DbContext.csproj new file mode 100644 index 0000000..e426336 --- /dev/null +++ b/PoweredSoft.DbContext/PoweredSoft.DbContext.csproj @@ -0,0 +1,26 @@ + + + + netstandard2.0 + Powered Softwares Inc. + 1.1.0$(VersionSuffix) + https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&r=g&d=retro + $(MSBuildProjectName) + initial + $(MSBuildProjectName) + initial + False + PoweredSoft + PoweredSoft + False + + + + + + + + + + + diff --git a/PoweredSoft.DbContext/QueryableExtensionService.cs b/PoweredSoft.DbContext/QueryableExtensionService.cs new file mode 100644 index 0000000..f8c62f6 --- /dev/null +++ b/PoweredSoft.DbContext/QueryableExtensionService.cs @@ -0,0 +1,73 @@ +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +namespace PoweredSoft.DbContext +{ + public class QueryableExtensionService : IQueryableExtensionService + { + private readonly IEnumerable services; + + public QueryableExtensionService(IEnumerable services) + { + this.services = services; + } + + public IQueryableService ResolveService(IQueryable queryable) + { + var service = this.services.FirstOrDefault(t => t.CanHandle(queryable)); + if (service == null) + throw new Exception($"Could not resolve any service to execute operation.."); + + return service; + } + + public IQueryableService ResolveService(IQueryable queryable) + { + var service = this.services.FirstOrDefault(t => t.CanHandle(queryable)); + if (service == null) + throw new Exception($"Could not resolve any service to execute operation.."); + + return service; + } + + public IQueryable AsNoTracking(IQueryable source) where T : class + => ResolveService(source).AsNoTracking(source); + + public IQueryable AsNoTracking(IQueryable source) + => ResolveService(source).AsNoTracking(source); + + public Task FirstOrDefaultAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + => ResolveService(source).FirstOrDefaultAsync(source, cancellationToken: cancellationToken); + + public Task FirstOrDefaultAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) where T : class + => ResolveService(source).FirstOrDefaultAsync(source, predicate, cancellationToken: cancellationToken); + + public Task FirstAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + => ResolveService(source).FirstAsync(source, cancellationToken: cancellationToken); + public Task FirstAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) where T : class + => ResolveService(source).FirstAsync(source, predicate, cancellationToken: cancellationToken); + + public Task AnyAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + => ResolveService(source).AnyAsync(source, cancellationToken: cancellationToken); + + public Task AnyAsync(IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) where T : class + => ResolveService(source).AnyAsync(source, predicate, cancellationToken: cancellationToken); + + public IQueryable Include(IQueryable source, Expression> path) + where T : class + => ResolveService(source).Include(source, path); + + public IQueryable Include(IQueryable source, string path) + where T : class + => ResolveService(source).Include(source, path); + + public Task> ToListAsync(IQueryable source, CancellationToken cancellationToken = default) where T : class + => ResolveService(source).ToListAsync(source, cancellationToken: cancellationToken); + + } +} diff --git a/PoweredSoft.DbContext/ServiceCollectionExtensions.cs b/PoweredSoft.DbContext/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..20f2a03 --- /dev/null +++ b/PoweredSoft.DbContext/ServiceCollectionExtensions.cs @@ -0,0 +1,25 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using PoweredSoft.DbContext.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.DbContext +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddPoweredSoftDbContextServices(this IServiceCollection services) + { + services.TryAddSingleton(); + services.TryAddSingleton(); + return services; + } + + public static void InitPoweredSoftDbContextExtensions(this IServiceProvider serviceProvider) + { + var queryableExtensionService = serviceProvider.GetRequiredService(); + DbContextQueryableExtensions.InitializeExtensionService(queryableExtensionService); + } + } +}