From dfc17a1369c34b37e23b1937059bcb324b4f7cc7 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Thu, 6 Dec 2018 23:10:33 -0600 Subject: [PATCH] seperated some logic into two interface contracts. --- .../IAsyncQueryableFactory.cs | 18 +++++++++++++ PoweredSoft.Data.Core/IDbContextFactory.cs | 5 ---- .../ASyncQueryableService.cs | 25 ++++++++++++++++++ .../DbContextFactory.cs | 26 ------------------- .../ServiceCollectionExtensions.cs | 4 ++- 5 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 PoweredSoft.Data.Core/IAsyncQueryableFactory.cs create mode 100644 PoweredSoft.Data.EntityFrameworkCore/ASyncQueryableService.cs diff --git a/PoweredSoft.Data.Core/IAsyncQueryableFactory.cs b/PoweredSoft.Data.Core/IAsyncQueryableFactory.cs new file mode 100644 index 0000000..31aea59 --- /dev/null +++ b/PoweredSoft.Data.Core/IAsyncQueryableFactory.cs @@ -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 FirstOrDefaultAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)); + Task FirstOrDefaultAsync(IQueryable queryable, Expression> predicate, CancellationToken cancellationToken = default(CancellationToken)); + Task> ToListAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)); + Task CountAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)); + Task LongCountAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)); + } +} diff --git a/PoweredSoft.Data.Core/IDbContextFactory.cs b/PoweredSoft.Data.Core/IDbContextFactory.cs index c683249..34f1295 100644 --- a/PoweredSoft.Data.Core/IDbContextFactory.cs +++ b/PoweredSoft.Data.Core/IDbContextFactory.cs @@ -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 SaveChangesAsync(); IEnumerable GetKeyProperties(Type entityType); IEnumerable>> GetKeyProperties(); - - 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.EntityFrameworkCore/ASyncQueryableService.cs b/PoweredSoft.Data.EntityFrameworkCore/ASyncQueryableService.cs new file mode 100644 index 0000000..a34123f --- /dev/null +++ b/PoweredSoft.Data.EntityFrameworkCore/ASyncQueryableService.cs @@ -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 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); + public Task CountAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)) + => queryable.CountAsync(); + public Task LongCountAsync(IQueryable queryable, CancellationToken cancellationToken = default(CancellationToken)) + => queryable.LongCountAsync(); + } +} diff --git a/PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs b/PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs index 82becf9..8e3b2cd 100644 --- a/PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs +++ b/PoweredSoft.Data.EntityFrameworkCore/DbContextFactory.cs @@ -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 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 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/ServiceCollectionExtensions.cs b/PoweredSoft.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs index cfefccd..4f7daf5 100644 --- a/PoweredSoft.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs +++ b/PoweredSoft.Data.EntityFrameworkCore/ServiceCollectionExtensions.cs @@ -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(); + services.TryAddTransient(); + services.TryAddTransient(); return services; } }