Add project files.
This commit is contained in:
@@ -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<T> GetQueryable<T>() where T : class => _context.Set<T>();
|
||||
|
||||
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<int> SaveChangesAsync() => await _context.SaveChangesAsync();
|
||||
|
||||
public Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken)) => queryable.FirstOrDefaultAsync(cancellationToken);
|
||||
public Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken)) => queryable.FirstOrDefaultAsync(predicate, cancellationToken);
|
||||
public Task<List<T>> ToListAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken)) => queryable.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Copyright>Powered Softwares Inc.</Copyright>
|
||||
<PackageLicenseUrl>MIT</PackageLicenseUrl>
|
||||
<PackageProjectUrl>https://github.com/PoweredSoft/Data</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/PoweredSoft/Data</RepositoryUrl>
|
||||
<RepositoryType>github</RepositoryType>
|
||||
<PackageTags>powered,soft,orm,db,context,ef,ef6,efcore,factory</PackageTags>
|
||||
<Version>1.0.0</Version>
|
||||
<PackageIconUrl>https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&amp;r=g&amp;d=retro</PackageIconUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PoweredSoft.Data.Core\PoweredSoft.Data.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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<IDbContextFactoryProvider, DbContextFactoryProvider>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user