seperated some logic into two interface contracts.
This commit is contained in:
parent
2ff9af41cd
commit
dfc17a1369
18
PoweredSoft.Data.Core/IAsyncQueryableFactory.cs
Normal file
18
PoweredSoft.Data.Core/IAsyncQueryableFactory.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.Data.Core
|
||||
{
|
||||
public interface IAsyncQueryableFactory
|
||||
{
|
||||
Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<List<T>> ToListAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<int> CountAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<long> LongCountAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken));
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@ using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.Data.Core
|
||||
@ -21,9 +20,5 @@ namespace PoweredSoft.Data.Core
|
||||
Task<int> SaveChangesAsync();
|
||||
IEnumerable<PropertyInfo> GetKeyProperties(Type entityType);
|
||||
IEnumerable<Expression<Func<TEntity, object>>> GetKeyProperties<TEntity>();
|
||||
|
||||
Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken);
|
||||
Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken);
|
||||
Task<List<T>> ToListAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PoweredSoft.Data.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.Data.EntityFrameworkCore
|
||||
{
|
||||
public class AsyncQueryableFactory : IAsyncQueryableFactory
|
||||
{
|
||||
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);
|
||||
public Task<int> CountAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken))
|
||||
=> queryable.CountAsync();
|
||||
public Task<long> LongCountAsync<T>(IQueryable<T> queryable, CancellationToken cancellationToken = default(CancellationToken))
|
||||
=> queryable.LongCountAsync();
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ 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
|
||||
@ -65,30 +64,5 @@ namespace PoweredSoft.Data.EntityFrameworkCore
|
||||
.ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
// FOR EF6 when implemented.
|
||||
public IEnumerable<MemberExpression> GetKeyProperties(Type entityType)
|
||||
{
|
||||
foreach (PropertyInfo prop in entityType.GetProperties())
|
||||
{
|
||||
object[] attrs = prop.GetCustomAttributes(false);
|
||||
|
||||
foreach (object obj in attrs)
|
||||
{
|
||||
if (obj.GetType() == typeof(EdmScalarPropertyAttribute))
|
||||
{
|
||||
EdmScalarPropertyAttribute attr = (EdmScalarPropertyAttribute)obj;
|
||||
if (attr.EntityKeyProperty)
|
||||
keyList.Add(prop.Name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using PoweredSoft.Data.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -10,7 +11,8 @@ namespace PoweredSoft.Data.EntityFrameworkCore
|
||||
{
|
||||
public static IServiceCollection AddPoweredSoftDataServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IDbContextFactoryProvider, DbContextFactoryProvider>();
|
||||
services.TryAddTransient<IDbContextFactoryProvider, DbContextFactoryProvider>();
|
||||
services.TryAddTransient<IAsyncQueryableFactory, AsyncQueryableFactory>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user