From d755a9a4b89f6cbf4609f6a7d445ff3ac6240119 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Fri, 23 Nov 2018 01:22:05 -0600 Subject: [PATCH] Add project files. --- Data.sln | 37 ++++++++++++++ LICENSE.md | 21 ++++++++ PoweredSoft.Data.Core/IDbContextFactory.cs | 26 ++++++++++ .../IDbContextFactoryProvider.cs | 11 ++++ .../PoweredSoft.Data.Core.csproj | 16 ++++++ .../DbContextFactory.cs | 51 +++++++++++++++++++ .../DbContextFactoryProvider.cs | 26 ++++++++++ ...oweredSoft.Data.EntityFrameworkCore.csproj | 24 +++++++++ .../ServiceCollectionExtensions.cs | 17 +++++++ README.md | 14 +++++ 10 files changed, 243 insertions(+) create mode 100644 Data.sln create mode 100644 LICENSE.md create mode 100644 PoweredSoft.Data.Core/IDbContextFactory.cs create mode 100644 PoweredSoft.Data.Core/IDbContextFactoryProvider.cs create mode 100644 PoweredSoft.Data.Core/PoweredSoft.Data.Core.csproj create mode 100644 PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs create mode 100644 PoweredSoft.Data.EntityFrameworkCore/DbContextFactoryProvider.cs create mode 100644 PoweredSoft.Data.EntityFrameworkCore/PoweredSoft.Data.EntityFrameworkCore.csproj create mode 100644 PoweredSoft.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs create mode 100644 README.md diff --git a/Data.sln b/Data.sln new file mode 100644 index 0000000..6b7c4fa --- /dev/null +++ b/Data.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2046 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoweredSoft.Data.Core", "PoweredSoft.Data.Core\PoweredSoft.Data.Core.csproj", "{6C61F343-9634-40CD-AFC1-7C4C3FB4E524}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoweredSoft.Data.EntityFrameworkCore", "PoweredSoft.Data.EntityFrameworkCore\PoweredSoft.Data.EntityFrameworkCore.csproj", "{78EEC90C-F2C3-4B59-93DA-DE22BFD1FD30}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{40E6D28E-45E6-418F-BACD-954ED03AC4C8}" + ProjectSection(SolutionItems) = preProject + LICENSE.md = LICENSE.md + README.md = README.md + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6C61F343-9634-40CD-AFC1-7C4C3FB4E524}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C61F343-9634-40CD-AFC1-7C4C3FB4E524}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C61F343-9634-40CD-AFC1-7C4C3FB4E524}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C61F343-9634-40CD-AFC1-7C4C3FB4E524}.Release|Any CPU.Build.0 = Release|Any CPU + {78EEC90C-F2C3-4B59-93DA-DE22BFD1FD30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78EEC90C-F2C3-4B59-93DA-DE22BFD1FD30}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78EEC90C-F2C3-4B59-93DA-DE22BFD1FD30}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78EEC90C-F2C3-4B59-93DA-DE22BFD1FD30}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D3AE5F41-FB7B-4EA8-9C97-1D878E91CE2C} + EndGlobalSection +EndGlobal diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..2543517 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Powered Softwares Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/PoweredSoft.Data.Core/IDbContextFactory.cs b/PoweredSoft.Data.Core/IDbContextFactory.cs new file mode 100644 index 0000000..19ee05d --- /dev/null +++ b/PoweredSoft.Data.Core/IDbContextFactory.cs @@ -0,0 +1,26 @@ +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.Data.Core +{ + public interface IDbContextFactory + { + IQueryable GetQueryable() + where T : class; + + IQueryable GetQueryable(Type type); + void Add(object entity); + void Remove(object entity); + int SaveChanges(); + Task SaveChangesAsync(); + + Task FirstOrDefaultAsync(IQueryable queryable, CancellationToken cancellationToken); + Task FirstOrDefaultAsync(IQueryable queryable, Expression> predicate, CancellationToken cancellationToken); + Task> ToListAsync(IQueryable queryable, CancellationToken cancellationToken); + } +} diff --git a/PoweredSoft.Data.Core/IDbContextFactoryProvider.cs b/PoweredSoft.Data.Core/IDbContextFactoryProvider.cs new file mode 100644 index 0000000..15a40c2 --- /dev/null +++ b/PoweredSoft.Data.Core/IDbContextFactoryProvider.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.Data.Core +{ + public interface IDbContextFactoryProvider + { + IDbContextFactory GetContextFactory(Type contextType); + } +} diff --git a/PoweredSoft.Data.Core/PoweredSoft.Data.Core.csproj b/PoweredSoft.Data.Core/PoweredSoft.Data.Core.csproj new file mode 100644 index 0000000..7a2eb81 --- /dev/null +++ b/PoweredSoft.Data.Core/PoweredSoft.Data.Core.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + true + Powered Softwares Inc. + MIT + https://github.com/PoweredSoft/Data + https://github.com/PoweredSoft/Data + github + powered,soft,orm,db,context,ef,ef6,efcore,factory + 1.0.0 + https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&r=g&d=retro + + + diff --git a/PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs b/PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs new file mode 100644 index 0000000..8bc09bf --- /dev/null +++ b/PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs @@ -0,0 +1,51 @@ +using Microsoft.EntityFrameworkCore; +using PoweredSoft.Data.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +namespace PoweredSoft.Data.EntityFrameworkCore +{ + public class DbContextFactory : IDbContextFactory + { + private readonly DbContext _context; + private MethodInfo _setGenericMethod = null; + + public DbContextFactory(DbContext dbContext) + { + _context = dbContext; + } + + public void Add(object entity) => _context.Add(entity); + + + + public IQueryable GetQueryable() where T : class => _context.Set(); + + public IQueryable GetQueryable(Type type) + { + if (_setGenericMethod == null) + _setGenericMethod = typeof(DbContextFactory).GetMethods().FirstOrDefault(t => t.Name == nameof(GetQueryable) && t.ContainsGenericParameters); + + var gm = _setGenericMethod.MakeGenericMethod(type); + var ret = (IQueryable)gm.Invoke(this, new object[] { }); + return ret; + } + + public void Remove(object entity) + { + _context.Remove(entity); + } + + public int SaveChanges() => _context.SaveChanges(); + public async Task SaveChangesAsync() => await _context.SaveChangesAsync(); + + public Task FirstOrDefaultAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)) => queryable.FirstOrDefaultAsync(cancellationToken); + public Task FirstOrDefaultAsync(IQueryable queryable, Expression> predicate, CancellationToken cancellationToken = default(CancellationToken)) => queryable.FirstOrDefaultAsync(predicate, cancellationToken); + public Task> ToListAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)) => queryable.ToListAsync(cancellationToken); + } +} diff --git a/PoweredSoft.Data.EntityFrameworkCore/DbContextFactoryProvider.cs b/PoweredSoft.Data.EntityFrameworkCore/DbContextFactoryProvider.cs new file mode 100644 index 0000000..525f0ec --- /dev/null +++ b/PoweredSoft.Data.EntityFrameworkCore/DbContextFactoryProvider.cs @@ -0,0 +1,26 @@ +using PoweredSoft.Data.Core; +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.EntityFrameworkCore; + +namespace PoweredSoft.Data.EntityFrameworkCore +{ + public class DbContextFactoryProvider : IDbContextFactoryProvider + { + private readonly IServiceProvider serviceProvider; + + public DbContextFactoryProvider(IServiceProvider serviceProvider) + { + this.serviceProvider = serviceProvider; + } + + public IDbContextFactory GetContextFactory(Type contextType) + { + var dbContext = this.serviceProvider.GetRequiredService(contextType) as DbContext; + var dbContextFactory = new DbContextFactory(dbContext); + return dbContextFactory; + } + } +} diff --git a/PoweredSoft.Data.EntityFrameworkCore/PoweredSoft.Data.EntityFrameworkCore.csproj b/PoweredSoft.Data.EntityFrameworkCore/PoweredSoft.Data.EntityFrameworkCore.csproj new file mode 100644 index 0000000..0ff1284 --- /dev/null +++ b/PoweredSoft.Data.EntityFrameworkCore/PoweredSoft.Data.EntityFrameworkCore.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp2.1 + true + Powered Softwares Inc. + MIT + https://github.com/PoweredSoft/Data + https://github.com/PoweredSoft/Data + github + powered,soft,orm,db,context,ef,ef6,efcore,factory + 1.0.0 + https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&r=g&d=retro + + + + + + + + + + + diff --git a/PoweredSoft.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs b/PoweredSoft.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..cfefccd --- /dev/null +++ b/PoweredSoft.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs @@ -0,0 +1,17 @@ +using Microsoft.Extensions.DependencyInjection; +using PoweredSoft.Data.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoweredSoft.Data.EntityFrameworkCore +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddPoweredSoftDataServices(this IServiceCollection services) + { + services.AddTransient(); + return services; + } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..248d3b9 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# IDbContextFactory + +The goal of this project is to help, fill the gap of supporting multiple ORM's in DynamicQuery, and possibly more projects in the future. + +One of the most obvious reasy is to be able to execute async/await operations on the context without, the executing library to be dependant on the ORM Framework such as (EF Core, EF6). + +## Getting Started + +> Install nuget package to your awesome project. + +Full Version | NuGet | NuGet Install +------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------: +PoweredSoft.Data.Core | [![NuGet](https://img.shields.io/nuget/v/PoweredSoft.Data.Core.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/PoweredSoft.Data.Core/) | ```PM> Install-Package PoweredSoft.DynamicQuery``` +PoweredSoft.Data.EntityFrameworkCore | [![NuGet](https://img.shields.io/nuget/v/PoweredSoft.Data.EntityFrameworkCore.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/PoweredSoft.Data.EntityFrameworkCore/) | ```PM> Install-Package PoweredSoft.Data.EntityFrameworkCore``` \ No newline at end of file