Skip to content

Commit

Permalink
(GH-622) cleaned up ef context code
Browse files Browse the repository at this point in the history
  • Loading branch information
johelvisguzman committed Jun 30, 2021
1 parent 0941e52 commit 8659869
Showing 1 changed file with 0 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
using Transactions;
using Utility;

/// <summary>
/// An implementation of <see cref="IEfRepositoryContext" />.
/// </summary>
/// <seealso cref="IEfRepositoryContext" />
internal class EfRepositoryContext : LinqRepositoryContextBaseAsync, IEfRepositoryContext
{
#region Fields
Expand All @@ -30,10 +26,6 @@ internal class EfRepositoryContext : LinqRepositoryContextBaseAsync, IEfReposito

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="EfRepositoryContext" /> class.
/// </summary>
/// <param name="context">The context.</param>
public EfRepositoryContext(DbContext context)
{
Conventions = RepositoryConventions.Default();
Expand All @@ -46,42 +38,22 @@ public EfRepositoryContext(DbContext context)

#region Implementation of IEfRepositoryContext

/// <summary>
/// Gets the underlying context.
/// </summary>
public DbContext UnderlyingContext { get { return _context; } }

#endregion

#region Implementation of IRepositoryContext

/// <summary>
/// Returns the entity's query.
/// </summary>
/// <typeparam name="TEntity">The type of the of the entity.</typeparam>
/// <returns>The entity's query.</returns>
protected override IQueryable<TEntity> AsQueryable<TEntity>()
{
return _context.Set<TEntity>().AsQueryable();
}

/// <summary>
/// Apply a fetching options to the specified entity's query.
/// </summary>
/// <returns>The entity's query with the applied options.</returns>
protected override IQueryable<TEntity> ApplyFetchingOptions<TEntity>(IQueryable<TEntity> query, IQueryOptions<TEntity> options)
{
return query.ApplyFetchingOptions(Conventions, options);
}

/// <summary>
/// Creates a raw SQL query that is executed directly in the database and returns a collection of entities.
/// </summary>
/// <param name="sql">The SQL query string.</param>
/// <param name="cmdType">The command type.</param>
/// <param name="parameters">The parameters to apply to the SQL query string.</param>
/// <param name="projector">A function to project each entity into a new form.</param>
/// <returns>A list which each entity has been projected into a new form.</returns>
public override IEnumerable<TEntity> ExecuteSqlQuery<TEntity>(string sql, CommandType cmdType, Dictionary<string, object> parameters, Func<IDataReader, TEntity> projector)
{
Guard.NotEmpty(sql, nameof(sql));
Expand Down Expand Up @@ -112,13 +84,6 @@ public override IEnumerable<TEntity> ExecuteSqlQuery<TEntity>(string sql, Comman
}
}

/// <summary>
/// Creates a raw SQL query that is executed directly in the database.
/// </summary>
/// <param name="sql">The SQL query string.</param>
/// <param name="cmdType">The command type.</param>
/// <param name="parameters">The parameters to apply to the SQL query string.</param>
/// <returns>The number of rows affected.</returns>
public override int ExecuteSqlCommand(string sql, CommandType cmdType, Dictionary<string, object> parameters)
{
Guard.NotEmpty(sql, nameof(sql));
Expand Down Expand Up @@ -149,32 +114,18 @@ public override int ExecuteSqlCommand(string sql, CommandType cmdType, Dictionar
}
}

/// <summary>
/// Begins the transaction.
/// </summary>
/// <returns>The transaction.</returns>
public override ITransactionManager BeginTransaction()
{
CurrentTransaction = new EfTransactionManager(_context.Database.BeginTransaction());

return CurrentTransaction;
}

/// <summary>
/// Tracks the specified entity in memory and will be inserted into the database when <see cref="M:DotNetToolkit.Repository.IContext.SaveChanges" /> is called.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
public override void Add<TEntity>(TEntity entity)
{
_context.Set<TEntity>().Add(Guard.NotNull(entity, nameof(entity)));
}

/// <summary>
/// Tracks the specified entity in memory and will be updated in the database when <see cref="M:DotNetToolkit.Repository.IContext.SaveChanges" /> is called.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
public override void Update<TEntity>(TEntity entity)
{
Guard.NotNull(entity, nameof(entity));
Expand All @@ -198,11 +149,6 @@ public override void Update<TEntity>(TEntity entity)
}
}

/// <summary>
/// Tracks the specified entity in memory and will be removed from the database when <see cref="M:DotNetToolkit.Repository.IContext.SaveChanges" /> is called.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
public override void Remove<TEntity>(TEntity entity)
{
Guard.NotNull(entity, nameof(entity));
Expand All @@ -224,24 +170,11 @@ public override void Remove<TEntity>(TEntity entity)
}
}

/// <summary>
/// Saves all changes made in this context to the database.
/// </summary>
/// <returns>
/// The number of state entries written to the database.
/// </returns>
public override int SaveChanges()
{
return _context.SaveChanges();
}

/// <summary>
/// Finds an entity with the given primary key values in the repository.
/// </summary>
/// <typeparam name="TEntity">The type of the of the entity.</typeparam>
/// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>The entity found in the repository.</returns>
public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy, params object[] keyValues)
{
Guard.NotEmpty(keyValues, nameof(keyValues));
Expand All @@ -260,55 +193,31 @@ public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy

#region Implementation of IRepositoryContextAsync

/// <summary>
/// An overridable method to return the first element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
protected override Task<TSource> FirstOrDefaultAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
{
return source.FirstOrDefaultAsync(cancellationToken);
}

/// <summary>
/// An overridable method to create a <see cref="T:System.Collections.Generic.List`1" /> from an <see cref="T:System.Linq.IQueryable`1" /> by enumerating it asynchronously.
/// </summary>
protected override Task<List<TSource>> ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
{
return source.ToListAsync(cancellationToken);
}

/// <summary>
/// An overridable method to return the number of elements in a sequence.
/// </summary>
protected override Task<int> CountAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
{
return source.CountAsync(cancellationToken);
}

/// <summary>
/// An overridable method to determine whether a sequence contains any elements.
/// </summary>
protected override Task<bool> AnyAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
{
return source.AnyAsync(cancellationToken);
}

/// <summary>
/// An overridable method to create a <see cref="T:System.Collections.Generic.Dictionary`2" /> from an <see cref="T:System.Linq.IQueryable`1" /> by enumerating it asynchronously according to a specified key selector and an element selector function.
/// </summary>
protected override Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(IQueryable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
{
return source.ToDictionaryAsync(keySelector, elementSelector, cancellationToken);
}

/// <summary>
/// Asynchronously creates raw SQL query that is executed directly in the database and returns a collection of entities.
/// </summary>
/// <param name="sql">The SQL query string.</param>
/// <param name="cmdType">The command type.</param>
/// <param name="parameters">The parameters to apply to the SQL query string.</param>
/// <param name="projector">A function to project each entity into a new form.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing a list which each entity has been projected into a new form.</returns>
public override async Task<IEnumerable<TEntity>> ExecuteSqlQueryAsync<TEntity>(string sql, CommandType cmdType, Dictionary<string, object> parameters, Func<IDataReader, TEntity> projector, CancellationToken cancellationToken = new CancellationToken())
{
Guard.NotEmpty(sql, nameof(sql));
Expand Down Expand Up @@ -339,14 +248,6 @@ protected override Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, T
}
}

/// <summary>
/// Asynchronously creates raw SQL query that is executed directly in the database.
/// </summary>
/// <param name="sql">The SQL query string.</param>
/// <param name="cmdType">The command type.</param>
/// <param name="parameters">The parameters to apply to the SQL query string.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the number of rows affected.</returns>
public override async Task<int> ExecuteSqlCommandAsync(string sql, CommandType cmdType, Dictionary<string, object> parameters, CancellationToken cancellationToken = new CancellationToken())
{
Guard.NotEmpty(sql, nameof(sql));
Expand Down Expand Up @@ -377,24 +278,11 @@ protected override Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, T
}
}

/// <summary>
/// Asynchronously saves all changes made in this context to the database.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the number of state entries written to the database.</returns>
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
return _context.SaveChangesAsync(cancellationToken);
}

/// <summary>
/// Asynchronously finds an entity with the given primary key values in the repository.
/// </summary>
/// <typeparam name="TEntity">The type of the of the entity.</typeparam>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the entity found in the repository.</returns>
public override async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToken, IFetchQueryStrategy<TEntity> fetchStrategy, params object[] keyValues)
{
Guard.NotEmpty(keyValues, nameof(keyValues));
Expand All @@ -413,9 +301,6 @@ public override async Task<TEntity> FindAsync<TEntity>(CancellationToken cancell

#region Implementation of IDisposable

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public override void Dispose()
{
_context.Dispose();
Expand Down

0 comments on commit 8659869

Please sign in to comment.