Skip to content

Commit

Permalink
generic mvc still borken
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkis117 committed Mar 19, 2017
1 parent d7889d8 commit 4314bf5
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 179 deletions.
37 changes: 8 additions & 29 deletions src/GenericMvc.Repositories/EntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public virtual async Task<T> Create(T entity)

var addedEntity = ContextSet.Add(entity);

if (await this.DataContext.SaveChangesAsync() >= 0)
if (await DataContext.SaveChangesAsync() >= 0)
{
//return true;
return addedEntity.Entity;
Expand All @@ -62,7 +62,7 @@ public virtual async Task<IEnumerable<T>> CreateRange(IEnumerable<T> entities)

ContextSet.AddRange(entities);

if (await this.DataContext.SaveChangesAsync() >= 0)
if (await DataContext.SaveChangesAsync() >= 0)
{
return entities;
}
Expand All @@ -87,7 +87,7 @@ public virtual async Task<T> Update(T entity)

var updatedEntity = ContextSet.Update(entity);

if (await this.DataContext.SaveChangesAsync() >= 0)
if (await DataContext.SaveChangesAsync() >= 0)
{
return updatedEntity.Entity;
}
Expand All @@ -105,7 +105,7 @@ public virtual async Task<IEnumerable<T>> UpdateRange(IEnumerable<T> entities)

ContextSet.UpdateRange(entities);

if (await this.DataContext.SaveChangesAsync() >= 0)
if (await DataContext.SaveChangesAsync() >= 0)
{
return entities;
}
Expand All @@ -129,14 +129,7 @@ public virtual async Task<bool> Delete(T entity)

ContextSet.Remove(entity);

if (await DataContext.SaveChangesAsync() >= 0)
{
return true;
}
else
{
return false;
}
return await DataContext.SaveChangesAsync() >= 0;
}

public virtual async Task<bool> DeleteRange(IEnumerable<T> entities)
Expand All @@ -146,14 +139,7 @@ public virtual async Task<bool> DeleteRange(IEnumerable<T> entities)

ContextSet.RemoveRange(entities);

if (await DataContext.SaveChangesAsync() >= 0)
{
return true;
}
else
{
return false;
}
return await DataContext.SaveChangesAsync() >= 0;
}

//Second make sure the type is present in the data-context
Expand All @@ -172,14 +158,7 @@ public async Task<bool> DeleteChild(object child)
{
DataContext.Remove(child);

if (await DataContext.SaveChangesAsync() >= 0)
{
return true;
}
else
{
return false;
}
return await DataContext.SaveChangesAsync() >= 0;
}
else
{
Expand All @@ -199,7 +178,7 @@ public async Task<bool> DeleteChild(object child)
/// <exception cref="System.Exception">Save Failed: +ex.Message</exception>
public Task<int> Save()
{
return this.DataContext.SaveChangesAsync();
return DataContext.SaveChangesAsync();
}
}
}
10 changes: 5 additions & 5 deletions src/GenericMvc.Repositories/IReadOnlyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ public interface IReadOnlyRepository<TEntity> : IEnumerable<TEntity>

ParameterExpression EntityExpression { get; }

Task<bool> Any(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate);
Task<bool> Any(Expression<Func<TEntity, bool>> predicate);

Task<long> Count();

//enumerable because entire store is not loaded
Task<IEnumerable<TEntity>> GetAll();

Task<TEntity> Get(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate);
Task<TEntity> Get(Expression<Func<TEntity, bool>> predicate);

Task<TEntity> Get(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate, bool WithNestedData = false);
Task<TEntity> Get(Expression<Func<TEntity, bool>> predicate, bool WithNestedData = false);

//list because loaded into memory
Task<IList<TEntity>> GetMany(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate);
Task<IList<TEntity>> GetMany(Expression<Func<TEntity, bool>> predicate);

//list because loaded into memory
Task<IList<TEntity>> GetMany(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate, bool WithNestedData = false);
Task<IList<TEntity>> GetMany(Expression<Func<TEntity, bool>> predicate, bool WithNestedData = false);
}
}
131 changes: 40 additions & 91 deletions src/GenericMvc.Repositories/ReadOnlyEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -39,11 +37,11 @@ public class ReadOnlyEntityRepository<T> : IReadOnlyRepository<T>, IDisposable

protected static readonly Type typeofT = typeof(T);

public Type TypeOfEntity { get { return typeofT; } }
public Type TypeOfEntity => typeofT;

private static readonly ParameterExpression expressionOfT = Expression.Parameter(typeofT);

public ParameterExpression EntityExpression { get { return expressionOfT; } }
public ParameterExpression EntityExpression => expressionOfT;

/// <summary>
/// Initializes a new instance of the <see cref="EntityRepository{T}"/> class.
Expand All @@ -55,28 +53,22 @@ public ReadOnlyEntityRepository(DbContext dbContext)
{
try
{
if (dbContext == null)
throw new ArgumentNullException(nameof(dbContext));
DataContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));

if (modelEntityTypes == null)
modelEntityTypes = dbContext.Model.GetEntityTypes();

//check for entity based on one in graph controller
if (hasGenericTypeBeenChecked || IsTypePresentInDataContext(typeofT, EntityTypes))
{
//set data base context
this.DataContext = dbContext;

//set the working set
this.ContextSet = this.DataContext.Set<T>();

if (!hasGenericTypeBeenChecked)
hasGenericTypeBeenChecked = true;
}
else
{
throw new ArgumentException(this.GetType().ToString() + ": Not Member of Current DbContext.");
}

ContextSet = DataContext.Set<T>();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -148,22 +140,30 @@ protected static bool IsTypePresentInModel(Type typeParam, IEntityType entityTyp
return isPresent;
}

protected static CancellationToken NewCancelTokenThrows
{
get
{
var token = new CancellationToken();

token.ThrowIfCancellationRequested();

return token;
}
}

/// <summary>
/// Checks for the existence using the specified predicate.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
/// <exception cref="System.Exception"></exception>
public Task<bool> Any(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
public Task<bool> Any(Expression<Func<T, bool>> predicate)
{
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));

var token = new CancellationToken();

token.ThrowIfCancellationRequested();

return this.ContextSet.AsNoTracking().AnyAsync(predicate, token);
return ContextSet.AsNoTracking().AnyAsync(predicate, NewCancelTokenThrows);
}

/// <summary>
Expand All @@ -172,69 +172,41 @@ public Task<bool> Any(System.Linq.Expressions.Expression<Func<T, bool>> predicat
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
/// <exception cref="System.Exception"></exception>
public bool AnySync(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
public bool AnySync(Expression<Func<T, bool>> predicate)
{
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));

return this.ContextSet.AsNoTracking().Any(predicate);
return ContextSet.AsNoTracking().Any(predicate);
}

public Task<long> Count()
{
var token = new CancellationToken();

token.ThrowIfCancellationRequested();
public Task<long> Count() => ContextSet.AsNoTracking().LongCountAsync(NewCancelTokenThrows);

return ContextSet.AsNoTracking().LongCountAsync(token);
}

public IEnumerator<T> GetEnumerator()
{
return ContextSet.AsEnumerable().GetEnumerator();
}
public IEnumerator<T> GetEnumerator() => ContextSet.AsEnumerable().GetEnumerator();

IEnumerator IEnumerable.GetEnumerator()
{
return ContextSet.AsEnumerable().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => ContextSet.AsEnumerable().GetEnumerator();

/// <summary>
/// Gets all the entities from the table.
/// </summary>
/// <returns></returns>
/// <exception cref="System.Exception">Get All Failed: + typeof(T).ToString()</exception>
public virtual async Task<IEnumerable<T>> GetAll()
{
CancellationToken token = new CancellationToken();

token.ThrowIfCancellationRequested();

return await ContextSet.AsNoTracking().ToListAsync(token);
}
public virtual async Task<IEnumerable<T>> GetAll() => (await ContextSet.AsNoTracking().ToListAsync(NewCancelTokenThrows));

protected virtual IQueryable<T> GetIncludeQuery()
{
return this.ContextSet;
}
protected virtual IQueryable<T> GetIncludeQuery() => ContextSet;

/// <summary>
/// Gets the specified entity using the predicate.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
/// <exception cref="System.Exception">Get Failed: + typeof(T).ToString()</exception>
public virtual Task<T> Get(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
public virtual Task<T> Get(Expression<Func<T, bool>> predicate)
{
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));

System.Threading.CancellationToken token = new System.Threading.CancellationToken();

//Throw if query is Canceled
token.ThrowIfCancellationRequested();

return ContextSet.AsNoTracking().FirstOrDefaultAsync<T>(predicate, token);
return ContextSet.AsNoTracking().FirstOrDefaultAsync<T>(predicate, NewCancelTokenThrows);
}

/// <summary>
Expand All @@ -243,24 +215,15 @@ public virtual Task<T> Get(System.Linq.Expressions.Expression<Func<T, bool>> pre
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
/// <exception cref="System.Exception">Get Failed: + typeof(T).ToString()</exception>
public virtual Task<T> Get(System.Linq.Expressions.Expression<Func<T, bool>> predicate, bool WithNestedData = false)
public virtual Task<T> Get(Expression<Func<T, bool>> predicate, bool WithNestedData = false)
{
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));

if (WithNestedData)
{
System.Threading.CancellationToken token = new System.Threading.CancellationToken();
return GetIncludeQuery().AsNoTracking().FirstOrDefaultAsync(predicate, NewCancelTokenThrows);

//Throw if query is Canceled
token.ThrowIfCancellationRequested();

return GetIncludeQuery().AsNoTracking().FirstOrDefaultAsync(predicate, token);
}
else
{
return Get(predicate);
}
return Get(predicate);
}

//todo: also give this a boolean argument for using get or getcomplete
Expand All @@ -270,16 +233,12 @@ public virtual Task<T> Get(System.Linq.Expressions.Expression<Func<T, bool>> pre
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
/// <exception cref="System.Exception">Get Multi Failed: + typeof(T).ToString()</exception>
public async virtual Task<IList<T>> GetMany(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
public async virtual Task<IList<T>> GetMany(Expression<Func<T, bool>> predicate)
{
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));

CancellationToken token = new CancellationToken();

token.ThrowIfCancellationRequested();

return (await ContextSet.AsNoTracking().Where(predicate).ToListAsync(token)) as IList<T>;
return (await ContextSet.AsNoTracking().Where(predicate).ToListAsync(NewCancelTokenThrows));
}

public virtual Task<IList<T>> GetMany(Expression<Func<T, bool>> predicate, bool WithNestedData = false)
Expand All @@ -288,13 +247,9 @@ public virtual Task<IList<T>> GetMany(Expression<Func<T, bool>> predicate, bool
throw new ArgumentNullException(nameof(predicate));

if (WithNestedData)
{
return GetIncludeQuery().AsNoTracking().Where(predicate).ToListAsync() as Task<IList<T>>;
}
else
{
return GetMany(predicate);
}

return GetMany(predicate);
}

private bool _disposed = false;
Expand All @@ -308,17 +263,12 @@ protected virtual void Dispose(bool disposing)
{
try
{
if (!this._disposed)
if (disposing && !_disposed)
{
if (disposing)
{
this.DataContext.Dispose();
}
}

//this.ContextSet = null;
DataContext.Dispose();

this._disposed = true;
_disposed = true;
}
}
catch (Exception ex)
{
Expand All @@ -331,8 +281,7 @@ protected virtual void Dispose(bool disposing)
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
Dispose(true);
}
}
}
2 changes: 1 addition & 1 deletion src/GenericMvc.Test.Library/Repository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface IRepository
Task DeleteRange();
}

public interface EntityFrameworkRepository
public interface IEntityFrameworkRepository
{
void GetDataContext();

Expand Down
Loading

0 comments on commit 4314bf5

Please sign in to comment.